summaryrefslogtreecommitdiffstats
path: root/extras/hs-test/address_allocator.go
diff options
context:
space:
mode:
authorFilip Tehlar <ftehlar@cisco.com>2023-06-08 17:39:39 +0200
committerFlorin Coras <florin.coras@gmail.com>2023-06-08 17:04:36 +0000
commit3a910ab76800b9608b05e6b188730a2290b51ae0 (patch)
tree574fb81d426eb670cbfa22a764b1bee0cba6e7fc /extras/hs-test/address_allocator.go
parentd5b6f9c0ef98f148b0e728fc1e8ccd8ba3faa806 (diff)
hs-test: rename address allocator
Type: test Change-Id: I1745719315d4e0785df5a03aa4312f84c40cb18f Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
Diffstat (limited to 'extras/hs-test/address_allocator.go')
-rw-r--r--extras/hs-test/address_allocator.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/extras/hs-test/address_allocator.go b/extras/hs-test/address_allocator.go
new file mode 100644
index 00000000000..c08f88958dc
--- /dev/null
+++ b/extras/hs-test/address_allocator.go
@@ -0,0 +1,42 @@
+package main
+
+import "fmt"
+
+type AddressCounter = int
+
+type Ip4AddressAllocator struct {
+ networks map[int]AddressCounter
+}
+
+func (a *Ip4AddressAllocator) AddNetwork(networkNumber int) {
+ a.networks[networkNumber] = 1
+}
+
+func (a *Ip4AddressAllocator) NewIp4InterfaceAddress(inputNetworkNumber ...int) (string, error) {
+ var networkNumber int = 0
+ if len(inputNetworkNumber) > 0 {
+ networkNumber = inputNetworkNumber[0]
+ }
+
+ if _, ok := a.networks[networkNumber]; !ok {
+ a.AddNetwork(networkNumber)
+ }
+
+ numberOfAddresses := a.networks[networkNumber]
+
+ if numberOfAddresses == 254 {
+ return "", fmt.Errorf("no available IPv4 addresses")
+ }
+
+ address := fmt.Sprintf("10.10.%v.%v/24", networkNumber, numberOfAddresses)
+ a.networks[networkNumber] = numberOfAddresses + 1
+
+ return address, nil
+}
+
+func NewIp4AddressAllocator() *Ip4AddressAllocator {
+ var ip4AddrAllocator = new(Ip4AddressAllocator)
+ ip4AddrAllocator.networks = make(map[int]AddressCounter)
+ ip4AddrAllocator.AddNetwork(0)
+ return ip4AddrAllocator
+}