blob: 63a4191b09b29a9edd7cba78c9513bf8b95f3c5a [file] [log] [blame]
Scott Baker58a9c7a2013-07-29 15:43:07 -07001import os
2import socket
3from django.db import models
Andy Bavierfe81aa42013-11-21 14:11:48 -05004from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
Scott Baker58a9c7a2013-07-29 15:43:07 -07005from django.contrib.contenttypes.models import ContentType
6from 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 Baker026bfe72013-07-29 16:03:50 -070010NO_OBSERVER=False
Scott Baker58a9c7a2013-07-29 15:43:07 -070011
12class NetworkTemplate(PlCoreBase):
13 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
Scott Baker87e5e092013-08-07 18:58:10 -070014 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
Scott Baker58a9c7a2013-07-29 15:43:07 -070015
16 name = models.CharField(max_length=32)
17 description = models.CharField(max_length=1024, blank=True, null=True)
18 guaranteedBandwidth = models.IntegerField(default=0)
19 visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
Scott Baker87e5e092013-08-07 18:58:10 -070020 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
Scott Baker5f814b52013-08-09 14:51:21 -070021 sharedNetworkName = models.CharField(max_length=30, blank=True, null=True)
22 sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070023
24 def __unicode__(self): return u'%s' % (self.name)
25
26class Network(PlCoreBase):
27 name = models.CharField(max_length=32)
28 template = models.ForeignKey(NetworkTemplate)
Andy Bavierfe81aa42013-11-21 14:11:48 -050029 deployment = models.ForeignKey(Deployment, related_name="networks", help_text="Deployment this Network belongs to")
30 site = models.ForeignKey(Site, blank=True, null=True, default=None, related_name="networks", help_text="Is this an infrastructure Network at a single Site?")
Scott Baker58a9c7a2013-07-29 15:43:07 -070031 subnet = models.CharField(max_length=32, blank=True)
32 ports = models.CharField(max_length=1024, blank=True, null=True)
33 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -040034 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070035
36 guaranteedBandwidth = models.IntegerField(default=0)
37 permitAllSlices = models.BooleanField(default=False)
38 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070039 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070040 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
41
Scott Baker87191e72013-08-06 08:55:07 -070042 # for observer/manager
43 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
44 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
45 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
46
Scott Baker58a9c7a2013-07-29 15:43:07 -070047 def __unicode__(self): return u'%s' % (self.name)
48
49 def save(self, *args, **kwds):
50 if (not self.subnet) and (NO_OBSERVER):
51 from util.network_subnet_allocator import find_unused_subnet
52 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
53 super(Network, self).save(*args, **kwds)
54
Scott Baker87191e72013-08-06 08:55:07 -070055class NetworkSlice(PlCoreBase):
56 # This object exists solely so we can implement the permission check when
57 # adding slices to networks. It adds no additional fields to the relation.
58
59 network = models.ForeignKey(Network)
60 slice = models.ForeignKey(Slice)
61
62 def save(self, *args, **kwds):
63 slice = self.slice
64 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
65 # to add a sliver to the network, then one of the following must be true:
66 # 1) sliver's slice is in network's permittedSlices list,
67 # 2) sliver's slice is network's owner, or
68 # 3) network's permitAllSlices is true
69 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
70
71 super(NetworkSlice, self).save(*args, **kwds)
72
73 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
74
Scott Baker58a9c7a2013-07-29 15:43:07 -070075class NetworkSliver(PlCoreBase):
76 network = models.ForeignKey(Network)
77 sliver = models.ForeignKey(Sliver)
Scott Baker026bfe72013-07-29 16:03:50 -070078 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -070079 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -070080
81 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -070082 slice = self.sliver.slice
83 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
84 # to add a sliver to the network, then one of the following must be true:
85 # 1) sliver's slice is in network's permittedSlices list,
86 # 2) sliver's slice is network's owner, or
87 # 3) network's permitAllSlices is true
88 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
89
Scott Baker58a9c7a2013-07-29 15:43:07 -070090 if (not self.ip) and (NO_OBSERVER):
91 from util.network_subnet_allocator import find_unused_address
92 self.ip = find_unused_address(self.network.subnet,
93 [x.ip for x in self.network.networksliver_set.all()])
94 super(NetworkSliver, self).save(*args, **kwds)
95
96 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
97
98class Router(PlCoreBase):
99 name = models.CharField(max_length=32)
100 owner = models.ForeignKey(Slice, related_name="routers")
101 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
102 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
103
104 def __unicode__(self): return u'%s' % (self.name)
105
106class NetworkParameterType(PlCoreBase):
107 name = models.SlugField(help_text="The name of this parameter", max_length=128)
108 description = models.CharField(max_length=1024)
109
110 def __unicode__(self): return u'%s' % (self.name)
111
112class NetworkParameter(PlCoreBase):
113 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
114 value = models.CharField(help_text="The value of this parameter", max_length=1024)
115
116 # The required fields to do a ObjectType lookup, and object_id assignment
117 content_type = models.ForeignKey(ContentType)
118 object_id = models.PositiveIntegerField()
119 content_object = generic.GenericForeignKey('content_type', 'object_id')
120
121 def __unicode__(self):
122 return self.parameter.name
123
124