go.ligato.io/vpp-agent/v3@v3.5.0/plugins/netalloc/netalloc_api.go (about) 1 package netalloc 2 3 import ( 4 "net" 5 6 kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 7 "go.ligato.io/vpp-agent/v3/proto/ligato/netalloc" 8 ) 9 10 // GwValidityCheck is used in ValidateIPAddress to tell if a GW reference is (un)expected/required. 11 type GwValidityCheck int 12 13 const ( 14 // GWRefAllowed is used when it doesn't matter if reference points to interface 15 // address or GW address. 16 GWRefAllowed GwValidityCheck = iota 17 // GWRefRequired is used when an IP address reference should point to GW address. 18 GWRefRequired 19 // GwRefUnexpected is used when an IP address reference should not point to GW address. 20 GwRefUnexpected 21 ) 22 23 // AddressAllocator provides methods for descriptors of other plugins to reference 24 // and obtain allocated addresses. 25 // 26 // For example, if a model of some configuration item contains field IpAddress 27 // (of type string) which could reference allocated IP address (assigned to non 28 // pre-determined interface) and should be applied without mask, the descriptor 29 // for that item would implement some of the methods as follows: 30 // 31 // func (d *Descriptor) Validate(key string, intf *mymodel.MyModel) error { 32 // err := d.netallocPlugin.ValidateIPAddress(item.IpAddress, "", "IpAddress") 33 // if err != nil { 34 // return err 35 // } 36 // } 37 // 38 // func (d *Descriptor) Dependencies(key string, item *mymodel.MyModel) (dependencies []kvs.Dependency) { 39 // // Note: it is actually preferred to derive the IP address into a separate key-value 40 // // pair and assign the allocation dependency to it rather than to the entire configuration item 41 // dep, hasAllocDep := d.netallocPlugin.GetAddressAllocDep(item.IpAddress, "", "") 42 // if hasAllocDep { 43 // dependencies = append(dependencies, dep) 44 // } 45 // // ... 46 // } 47 // 48 // func (d *Descriptor) Create(key string, item *mymodel.MyModel) (metadata interface{}, err error) { 49 // addr, err := d.netallocPlugin.GetOrParseIPAddress(item.IpAddress, "", netalloc.ADDR_ONLY) 50 // if err != nil { 51 // d.log.Error(err) 52 // return nil, err 53 // } 54 // fmt.Printf("Assign IP address: %v", addr.IP) 55 // ... 56 // } 57 // 58 // func (d *Descriptor) Delete(key string, item *mymodel.MyModel) (err error) { 59 // addr, err := d.netallocPlugin.GetOrParseIPAddress(item.IpAddress, "", netalloc.ADDR_ONLY) 60 // if err != nil { 61 // d.log.Error(err) 62 // return nil, err 63 // } 64 // fmt.Printf("Un-assign IP address: %v", addr.IP) 65 // ... 66 // } 67 // 68 // func (d *Descriptor) Update(key string, oldItem, newItem *mymodel.MyModel, oldMetadata interface{}) (newMetadata interface{}, err error) { { 69 // prevAddr, err := d.netallocPlugin.GetOrParseIPAddress(oldItem.IpAddress, "", netalloc.ADDR_ONLY) 70 // if err != nil { 71 // d.log.Error(err) 72 // return nil, err 73 // } 74 // newAddr, err := d.netallocPlugin.GetOrParseIPAddress(newItem.IpAddress, "", netalloc.ADDR_ONLY) 75 // if err != nil { 76 // d.log.Error(err) 77 // return nil, err 78 // } 79 // fmt.Printf("Changing assigned IP address from %v to %v", prevAddr.IP, newAddr.IP) 80 // ... 81 // } 82 // 83 // func (d *Descriptor) Retrieve(correlate []adapter.MyModelKVWithMetadata) (retrieved []adapter.MyModelKVWithMetadata, err error) { 84 // // Retrieve instances of mymodel.MyModel ... 85 // // Use CorrelateRetrievedIPs to replace actual IP address with reference if it was used. 86 // for _, item := range retrieved { 87 // // get expected item configuration ... (store to expCfg) 88 // item.IpAddress = d.netallocPlugin.CorrelateRetrievedIPs( 89 // []string{expCfg.IpAddress}, []string{item.IpAddress}, "", netalloc.ADDR_ONLY)[0] 90 // } 91 // } 92 // 93 // Also don't forget to include netalloc descriptors in the list of "RetrieveDependencies" 94 // (for IP allocations, the descriptor name is stored in the constant IPAllocDescriptorName 95 // defined in plugins/netalloc/descriptor) 96 type AddressAllocator interface { 97 // CreateAddressAllocRef creates reference to an allocated IP address. 98 CreateAddressAllocRef(network, iface string, getGW bool) string 99 100 // ParseAddressAllocRef parses reference to an allocated IP address. 101 ParseAddressAllocRef(addrAllocRef, expIface string) ( 102 network, iface string, isGW, isRef bool, err error) 103 104 // GetAddressAllocDep reads what can be potentially a reference to an allocated 105 // IP address. If <allocRef> is indeed a reference, the function returns 106 // the corresponding dependency to be passed further into KVScheduler 107 // from the descriptor. Otherwise <hasAllocDep> is returned as false, and 108 // <allocRef> should be an actual address and not a reference. 109 GetAddressAllocDep(addrOrAllocRef, expIface, depLabelPrefix string) ( 110 dep kvs.Dependency, hasAllocDep bool) 111 112 // ValidateIPAddress checks validity of address reference or, if <addrOrAllocRef> 113 // already contains an actual IP address, it tries to parse it. 114 ValidateIPAddress(addrOrAllocRef, expIface, fieldName string, gwCheck GwValidityCheck) error 115 116 // GetOrParseIPAddress tries to get allocated interface (or GW) IP address 117 // referenced by <addrOrAllocRef> in the requested form. But if the string 118 // contains/ an actual IP address instead of a reference, the address is parsed 119 // using methods from the net package and returned in the requested form. 120 // For ADDR_ONLY address form, the returned <addr> will have the mask unset 121 // and the IP address should be accessed as <addr>.IP 122 GetOrParseIPAddress(addrOrAllocRef string, expIface string, addrForm netalloc.IPAddressForm) ( 123 addr *net.IPNet, err error) 124 125 // CorrelateRetrievedIPs should be used in Retrieve to correlate one or group 126 // of (model-wise indistinguishable) retrieved interface or GW IP addresses 127 // with the expected configuration. The method will replace retrieved addresses 128 // with the corresponding allocation references from the expected configuration 129 // if there are any. 130 // The method returns one IP address or address-allocation reference for every 131 // address from <retrievedAddrs>. 132 CorrelateRetrievedIPs(expAddrsOrRefs []string, retrievedAddrs []string, expIface string, 133 addrForm netalloc.IPAddressForm) []string 134 }