From: Scott Baker Date: Mon, 29 Jul 2013 17:29:26 +0000 (-0700) Subject: move out the testing code X-Git-Url: http://git.onelab.eu/?p=plstackapi.git;a=commitdiff_plain;h=c26cf3e1b01e34913cd4376f0218262068239fe4 move out the testing code --- diff --git a/planetstack/core/models/network.py b/planetstack/core/models/network.py index d19c050..da41133 100644 --- a/planetstack/core/models/network.py +++ b/planetstack/core/models/network.py @@ -5,42 +5,9 @@ from core.models import PlCoreBase, Site, Slice, Sliver from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic -# Create your models here. - -SUBNET_BASE = "10.0.0.0" -SUBNET_NODE_BITS = 12 # enough for 4096 bits per subnet -SUBNET_SUBNET_BITS = 12 # enough for 4096 private networks - -def ip_to_int(ip): - return int(socket.inet_aton(ip).encode('hex'),16) - -def int_to_ip(i): - return socket.inet_ntoa(hex(i)[2:].zfill(8).decode('hex')) - -def find_unused_subnet(base, subnet_bits, node_bits, existing_subnets): - # enumerate possible subnets until we find one that isn't used - i=1 - while True: - subnet_i = ip_to_int(base) | (i<=max_addr: - raise ValueError("No more ips available") - ip = int_to_ip(network_i | i) - if not (ip in existingAddresses): - return ip - i=i+1 +# If true, then IP addresses will be allocated by the model. If false, then +# we will assume the observer handles it. +NO_OBSERVER=True class NetworkTemplate(PlCoreBase): VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) @@ -65,17 +32,10 @@ class Network(PlCoreBase): def __unicode__(self): return u'%s' % (self.name) - def allocateSubnet(self): - existingSubnets = [x.subnet for x in Network.objects.all()] - return find_unused_subnet(SUBNET_BASE, SUBNET_SUBNET_BITS, SUBNET_NODE_BITS, existingSubnets) - - def allocateAddress(self): - existingAddresses = [x.ip for x in self.networksliver_set.all()] - return find_unused_address(self.subnet, existingAddresses) - def save(self, *args, **kwds): - if not self.subnet: - self.subnet = self.allocateSubnet() + if (not self.subnet) and (NO_OBSERVER): + from util.network_subnet_allocator import find_unused_subnet + self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()]) super(Network, self).save(*args, **kwds) class NetworkSliver(PlCoreBase): @@ -84,8 +44,10 @@ class NetworkSliver(PlCoreBase): ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True) def save(self, *args, **kwds): - if not self.ip: - self.ip = self.network.allocateAddress() + if (not self.ip) and (NO_OBSERVER): + from util.network_subnet_allocator import find_unused_address + self.ip = find_unused_address(self.network.subnet, + [x.ip for x in self.network.networksliver_set.all()]) super(NetworkSliver, self).save(*args, **kwds) def __unicode__(self): return u'foo!' diff --git a/planetstack/util/network_subnet_allocator.py b/planetstack/util/network_subnet_allocator.py new file mode 100644 index 0000000..e5530a1 --- /dev/null +++ b/planetstack/util/network_subnet_allocator.py @@ -0,0 +1,40 @@ +import socket + +""" Some utilities to simulate allocating networks/addresses when there is no + observer. +""" + +SUBNET_BASE = "10.0.0.0" +SUBNET_NODE_BITS = 12 # enough for 4096 bits per subnet +SUBNET_SUBNET_BITS = 12 # enough for 4096 private networks + +def ip_to_int(ip): + return int(socket.inet_aton(ip).encode('hex'),16) + +def int_to_ip(i): + return socket.inet_ntoa(hex(i)[2:].zfill(8).decode('hex')) + +def find_unused_subnet(base=SUBNET_BASE, subnet_bits=SUBNET_SUBNET_BITS, node_bits=SUBNET_NODE_BITS, existing_subnets=[]): + # enumerate possible subnets until we find one that isn't used + i=1 + while True: + subnet_i = ip_to_int(base) | (i<=max_addr: + raise ValueError("No more ips available") + ip = int_to_ip(network_i | i) + if not (ip in existingAddresses): + return ip + i=i+1