3 from django.db import models
4 from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
5 from django.contrib.contenttypes.models import ContentType
6 from django.contrib.contenttypes import generic
8 # If true, then IP addresses will be allocated by the model. If false, then
9 # we will assume the observer handles it.
12 class NetworkTemplate(PlCoreBase):
13 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
14 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
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")
20 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
21 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")
24 def __unicode__(self): return u'%s' % (self.name)
26 class Network(PlCoreBase):
27 name = models.CharField(max_length=32)
28 template = models.ForeignKey(NetworkTemplate)
29 subnet = models.CharField(max_length=32, blank=True)
30 ports = models.CharField(max_length=1024, blank=True, null=True)
31 labels = models.CharField(max_length=1024, blank=True, null=True)
32 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
34 guaranteedBandwidth = models.IntegerField(default=0)
35 permitAllSlices = models.BooleanField(default=False)
36 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
37 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
38 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
40 # for observer/manager
41 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
42 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
43 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
45 def __unicode__(self): return u'%s' % (self.name)
47 def save(self, *args, **kwds):
48 if (not self.subnet) and (NO_OBSERVER):
49 from util.network_subnet_allocator import find_unused_subnet
50 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
51 super(Network, self).save(*args, **kwds)
53 def can_update(self, user):
54 return self.owner.can_update(user)
57 def select_by_user(user):
59 qs = Network.objects.all()
61 slices = Slice.select_by_user(user)
62 #slice_ids = [s.id for s in Slice.select_by_user(user)]
63 qs = Network.objects.filter(owner__in=slices)
66 class NetworkDeployments(PlCoreBase):
67 # Stores the openstack ids at various deployments
68 network = models.ForeignKey(Network)
69 deployment = models.ForeignKey(Deployment)
70 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
71 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
72 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
73 subnet = models.CharField(max_length=32, blank=True)
75 def can_update(self, user):
79 def select_by_user(user):
81 qs = NetworkDeployments.objects.all()
83 slices = Slice.select_by_user(user)
84 networks = Network.objects.filter(owner__in=slices)
85 qs = NetworkDeployments.objects.filter(network__in=networks)
88 class NetworkSlice(PlCoreBase):
89 # This object exists solely so we can implement the permission check when
90 # adding slices to networks. It adds no additional fields to the relation.
92 network = models.ForeignKey(Network)
93 slice = models.ForeignKey(Slice)
95 def save(self, *args, **kwds):
97 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
98 # to add a sliver to the network, then one of the following must be true:
99 # 1) sliver's slice is in network's permittedSlices list,
100 # 2) sliver's slice is network's owner, or
101 # 3) network's permitAllSlices is true
102 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
104 super(NetworkSlice, self).save(*args, **kwds)
106 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
108 def can_update(self, user):
109 return self.slice.can_update(user)
112 def select_by_user(user):
114 qs = NetworkSlice.objects.all()
116 slice_ids = [s.id for s in Slice.select_by_user(user)]
117 qs = NetworkSlice.objects.filter(id__in=slice_ids)
120 class NetworkSliver(PlCoreBase):
121 network = models.ForeignKey(Network)
122 sliver = models.ForeignKey(Sliver)
123 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
124 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
126 def save(self, *args, **kwds):
127 slice = self.sliver.slice
128 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
129 # to add a sliver to the network, then one of the following must be true:
130 # 1) sliver's slice is in network's permittedSlices list,
131 # 2) sliver's slice is network's owner, or
132 # 3) network's permitAllSlices is true
133 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
135 if (not self.ip) and (NO_OBSERVER):
136 from util.network_subnet_allocator import find_unused_address
137 self.ip = find_unused_address(self.network.subnet,
138 [x.ip for x in self.network.networksliver_set.all()])
139 super(NetworkSliver, self).save(*args, **kwds)
141 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
143 def can_update(self, user):
144 return self.sliver.can_update(user)
147 def select_by_user(user):
149 qs = NetworkSliver.objects.all()
151 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
152 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
155 class Router(PlCoreBase):
156 name = models.CharField(max_length=32)
157 owner = models.ForeignKey(Slice, related_name="routers")
158 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
159 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
161 def __unicode__(self): return u'%s' % (self.name)
163 class NetworkParameterType(PlCoreBase):
164 name = models.SlugField(help_text="The name of this parameter", max_length=128)
165 description = models.CharField(max_length=1024)
167 def __unicode__(self): return u'%s' % (self.name)
169 class NetworkParameter(PlCoreBase):
170 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
171 value = models.CharField(help_text="The value of this parameter", max_length=1024)
173 # The required fields to do a ObjectType lookup, and object_id assignment
174 content_type = models.ForeignKey(ContentType)
175 object_id = models.PositiveIntegerField()
176 content_object = generic.GenericForeignKey('content_type', 'object_id')
178 def __unicode__(self):
179 return self.parameter.name