go.ligato.io/vpp-agent/v3@v3.5.0/plugins/netalloc/mock/mock_netplugin.go (about)

     1  package mock
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  
     7  	"go.ligato.io/vpp-agent/v3/pkg/models"
     8  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
     9  	plugin "go.ligato.io/vpp-agent/v3/plugins/netalloc"
    10  	"go.ligato.io/vpp-agent/v3/plugins/netalloc/utils"
    11  	"go.ligato.io/vpp-agent/v3/proto/ligato/netalloc"
    12  )
    13  
    14  // NetAlloc is a mock version of the netplugin, suitable for unit testing.
    15  type NetAlloc struct {
    16  	realNetAlloc *plugin.Plugin
    17  	allocated    map[string]*netalloc.IPAllocMetadata // allocation name -> parsed address
    18  }
    19  
    20  // NewMockNetAlloc is a constructor for mock netalloc plugin.
    21  func NewMockNetAlloc() *NetAlloc {
    22  	return &NetAlloc{
    23  		realNetAlloc: &plugin.Plugin{},
    24  		allocated:    make(map[string]*netalloc.IPAllocMetadata),
    25  	}
    26  }
    27  
    28  // Allocate simulates allocation of an IP address.
    29  func (p *NetAlloc) Allocate(network, ifaceName, address, gw string) {
    30  	var (
    31  		gwAddr *net.IPNet
    32  		err    error
    33  	)
    34  	addrAlloc := &netalloc.IPAllocation{
    35  		NetworkName:   network,
    36  		InterfaceName: ifaceName,
    37  	}
    38  	allocName := models.Name(addrAlloc)
    39  
    40  	ifaceAddr, _, err := utils.ParseIPAddr(address, nil)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	if gw != "" {
    45  		gwAddr, _, err = utils.ParseIPAddr(gw, ifaceAddr)
    46  		if err != nil {
    47  			panic(err)
    48  		}
    49  	}
    50  	p.allocated[allocName] = &netalloc.IPAllocMetadata{IfaceAddr: ifaceAddr, GwAddr: gwAddr}
    51  }
    52  
    53  // Deallocate simulates de-allocation of an IP address.
    54  func (p *NetAlloc) Deallocate(network, ifaceName string) {
    55  	addrAlloc := &netalloc.IPAllocation{
    56  		NetworkName:   network,
    57  		InterfaceName: ifaceName,
    58  	}
    59  	allocName := models.Name(addrAlloc)
    60  	delete(p.allocated, allocName)
    61  }
    62  
    63  // CreateAddressAllocRef creates reference to an allocated IP address.
    64  func (p *NetAlloc) CreateAddressAllocRef(network, iface string, getGW bool) string {
    65  	return p.realNetAlloc.CreateAddressAllocRef(network, iface, getGW)
    66  }
    67  
    68  // ParseAddressAllocRef parses reference to an allocated IP address.
    69  func (p *NetAlloc) ParseAddressAllocRef(addrAllocRef, expIface string) (
    70  	network, iface string, isGW, isRef bool, err error) {
    71  	return p.realNetAlloc.ParseAddressAllocRef(addrAllocRef, expIface)
    72  }
    73  
    74  // GetAddressAllocDep is not implemented here.
    75  func (p *NetAlloc) GetAddressAllocDep(addrOrAllocRef, ifaceName, depLabelPrefix string) (
    76  	dep kvs.Dependency, hasAllocDep bool) {
    77  	return kvs.Dependency{}, false
    78  }
    79  
    80  // ValidateIPAddress checks validity of address reference or, if <addrOrAllocRef>
    81  // already contains an actual IP address, it tries to parse it.
    82  func (p *NetAlloc) ValidateIPAddress(addrOrAllocRef, ifaceName, fieldName string, gwCheck plugin.GwValidityCheck) error {
    83  	return p.realNetAlloc.ValidateIPAddress(addrOrAllocRef, ifaceName, fieldName, gwCheck)
    84  }
    85  
    86  // GetOrParseIPAddress tries to get allocated interface (or GW) IP address
    87  // referenced by <addrOrAllocRef> in the requested form. But if the string
    88  // contains/ an actual IP address instead of a reference, the address is parsed
    89  // using methods from the net package and returned in the requested form.
    90  // For ADDR_ONLY address form, the returned <addr> will have the mask unset
    91  // and the IP address should be accessed as <addr>.IP
    92  func (p *NetAlloc) GetOrParseIPAddress(addrOrAllocRef string, ifaceName string,
    93  	addrForm netalloc.IPAddressForm) (addr *net.IPNet, err error) {
    94  
    95  	network, iface, getGW, isRef, err := utils.ParseAddrAllocRef(addrOrAllocRef, ifaceName)
    96  	if isRef && err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	if isRef {
   101  		// de-reference
   102  		allocName := models.Name(&netalloc.IPAllocation{
   103  			NetworkName:   network,
   104  			InterfaceName: iface,
   105  		})
   106  		allocation, found := p.allocated[allocName]
   107  		if !found {
   108  			return nil, errors.New("address is not allocated")
   109  		}
   110  		if getGW {
   111  			if allocation.GwAddr == nil {
   112  				return nil, errors.New("gw address is not defined")
   113  			}
   114  			return utils.GetIPAddrInGivenForm(allocation.GwAddr, addrForm), nil
   115  		}
   116  		return utils.GetIPAddrInGivenForm(allocation.IfaceAddr, addrForm), nil
   117  	}
   118  
   119  	// try to parse the address
   120  	ipAddr, _, err := utils.ParseIPAddr(addrOrAllocRef, nil)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return utils.GetIPAddrInGivenForm(ipAddr, addrForm), nil
   125  }
   126  
   127  // CorrelateRetrievedIPs is not implemented here.
   128  func (p *NetAlloc) CorrelateRetrievedIPs(expAddrsOrRefs []string, retrievedAddrs []string,
   129  	ifaceName string, addrForm netalloc.IPAddressForm) (correlated []string) {
   130  	return retrievedAddrs
   131  }