Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 1 | import os |
| 2 | import socket |
| 3 | from django.db import models |
| 4 | from core.models import PlCoreBase, Site, Slice, Sliver |
| 5 | from django.contrib.contenttypes.models import ContentType |
| 6 | from django.contrib.contenttypes import generic |
| 7 | |
| 8 | # If true, then IP addresses will be allocated by the model. If false, then |
| 9 | # we will assume the observer handles it. |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 10 | NO_OBSERVER=False |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 11 | |
| 12 | class NetworkTemplate(PlCoreBase): |
| 13 | VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) |
| 14 | |
| 15 | name = models.CharField(max_length=32) |
| 16 | description = models.CharField(max_length=1024, blank=True, null=True) |
| 17 | guaranteedBandwidth = models.IntegerField(default=0) |
| 18 | visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private") |
| 19 | |
| 20 | def __unicode__(self): return u'%s' % (self.name) |
| 21 | |
| 22 | class Network(PlCoreBase): |
| 23 | name = models.CharField(max_length=32) |
| 24 | template = models.ForeignKey(NetworkTemplate) |
| 25 | subnet = models.CharField(max_length=32, blank=True) |
| 26 | ports = models.CharField(max_length=1024, blank=True, null=True) |
| 27 | labels = models.CharField(max_length=1024, blank=True, null=True) |
| 28 | owner = models.ForeignKey(Slice, related_name="ownedNetworks") |
| 29 | |
| 30 | guaranteedBandwidth = models.IntegerField(default=0) |
| 31 | permitAllSlices = models.BooleanField(default=False) |
| 32 | permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
| 33 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks") |
| 34 | slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver") |
| 35 | |
| 36 | def __unicode__(self): return u'%s' % (self.name) |
| 37 | |
| 38 | def save(self, *args, **kwds): |
| 39 | if (not self.subnet) and (NO_OBSERVER): |
| 40 | from util.network_subnet_allocator import find_unused_subnet |
| 41 | self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()]) |
| 42 | super(Network, self).save(*args, **kwds) |
| 43 | |
| 44 | class NetworkSliver(PlCoreBase): |
| 45 | network = models.ForeignKey(Network) |
| 46 | sliver = models.ForeignKey(Sliver) |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 47 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 48 | |
| 49 | def save(self, *args, **kwds): |
| 50 | if (not self.ip) and (NO_OBSERVER): |
| 51 | from util.network_subnet_allocator import find_unused_address |
| 52 | self.ip = find_unused_address(self.network.subnet, |
| 53 | [x.ip for x in self.network.networksliver_set.all()]) |
| 54 | super(NetworkSliver, self).save(*args, **kwds) |
| 55 | |
| 56 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name) |
| 57 | |
| 58 | class Router(PlCoreBase): |
| 59 | name = models.CharField(max_length=32) |
| 60 | owner = models.ForeignKey(Slice, related_name="routers") |
| 61 | permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters") |
| 62 | networks = models.ManyToManyField(Network, blank=True, related_name="routers") |
| 63 | |
| 64 | def __unicode__(self): return u'%s' % (self.name) |
| 65 | |
| 66 | class NetworkParameterType(PlCoreBase): |
| 67 | name = models.SlugField(help_text="The name of this parameter", max_length=128) |
| 68 | description = models.CharField(max_length=1024) |
| 69 | |
| 70 | def __unicode__(self): return u'%s' % (self.name) |
| 71 | |
| 72 | class NetworkParameter(PlCoreBase): |
| 73 | parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter") |
| 74 | value = models.CharField(help_text="The value of this parameter", max_length=1024) |
| 75 | |
| 76 | # The required fields to do a ObjectType lookup, and object_id assignment |
| 77 | content_type = models.ForeignKey(ContentType) |
| 78 | object_id = models.PositiveIntegerField() |
| 79 | content_object = generic.GenericForeignKey('content_type', 'object_id') |
| 80 | |
| 81 | def __unicode__(self): |
| 82 | return self.parameter.name |
| 83 | |
| 84 | |