3 from django.db import models
4 from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
5 from core.models import DeploymentLinkManager,DeploymentLinkDeletionManager
6 from django.contrib.contenttypes.models import ContentType
7 from django.contrib.contenttypes import generic
8 from django.core.exceptions import ValidationError
10 # If true, then IP addresses will be allocated by the model. If false, then
11 # we will assume the observer handles it.
14 def ParseNatList(ports):
15 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
19 tcp 123, tcp 124, tcp 125, udp 201, udp 202
21 User can put either a "/" or a " " between protocol and ports
22 Port ranges can be specified with "-" or ":"
26 parts = ports.split(",")
30 (protocol, ports) = part.split("/",1)
32 (protocol, ports) = part.split(None,1)
34 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
36 protocol = protocol.strip()
39 if not (protocol in ["udp", "tcp"]):
40 raise ValueError('unknown protocol %s' % protocol)
43 (first, last) = ports.split("-")
44 first = int(first.strip())
45 last = int(last.strip())
46 portStr = "%d:%d" % (first, last)
48 (first, last) = ports.split(":")
49 first = int(first.strip())
50 last = int(last.strip())
51 portStr = "%d:%d" % (first, last)
53 portStr = "%d" % int(ports)
55 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
59 def ValidateNatList(ports):
63 raise ValidationError(str(e))
65 class NetworkTemplate(PlCoreBase):
66 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
67 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
68 TOPOLOGY_CHOICES = (('bigswitch', 'BigSwitch'), ('physical', 'Physical'), ('custom', 'Custom'))
69 CONTROLLER_CHOICES = ((None, 'None'), ('onos', 'ONOS'), ('custom', 'Custom'))
71 name = models.CharField(max_length=32)
72 description = models.CharField(max_length=1024, blank=True, null=True)
73 guaranteedBandwidth = models.IntegerField(default=0)
74 visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
75 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
76 sharedNetworkName = models.CharField(max_length=30, blank=True, null=True)
77 sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
78 topologyKind = models.CharField(null=False, blank=False, max_length=30, choices=TOPOLOGY_CHOICES, default="BigSwitch")
79 controllerKind = models.CharField(null=True, blank=True, max_length=30, choices=CONTROLLER_CHOICES, default=None)
81 def __unicode__(self): return u'%s' % (self.name)
83 class Network(PlCoreBase):
84 name = models.CharField(max_length=32)
85 template = models.ForeignKey(NetworkTemplate)
86 subnet = models.CharField(max_length=32, blank=True)
87 ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList])
88 labels = models.CharField(max_length=1024, blank=True, null=True)
89 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
91 guaranteedBandwidth = models.IntegerField(default=0)
92 permitAllSlices = models.BooleanField(default=False)
93 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
94 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
95 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
97 topologyParameters = models.TextField(null=True, blank=True)
98 controllerParameters = models.TextField(null=True, blank=True)
100 # for observer/manager
101 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
102 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
103 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
105 def __unicode__(self): return u'%s' % (self.name)
107 def save(self, *args, **kwds):
108 if (not self.subnet) and (NO_OBSERVER):
109 from util.network_subnet_allocator import find_unused_subnet
110 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
111 super(Network, self).save(*args, **kwds)
113 def can_update(self, user):
114 return self.owner.can_update(user)
118 return ParseNatList(self.ports)
121 def select_by_user(user):
123 qs = Network.objects.all()
125 slices = Slice.select_by_user(user)
126 #slice_ids = [s.id for s in Slice.select_by_user(user)]
127 qs = Network.objects.filter(owner__in=slices)
130 class NetworkDeployments(PlCoreBase):
131 objects = DeploymentLinkManager()
132 deleted_objects = DeploymentLinkDeletionManager()
134 # Stores the openstack ids at various deployments
135 network = models.ForeignKey(Network, related_name='networkdeployments')
136 deployment = models.ForeignKey(Deployment, related_name='networkdeployments')
137 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
138 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
139 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
140 subnet = models.CharField(max_length=32, blank=True)
142 def can_update(self, user):
146 def select_by_user(user):
148 qs = NetworkDeployments.objects.all()
150 slices = Slice.select_by_user(user)
151 networks = Network.objects.filter(owner__in=slices)
152 qs = NetworkDeployments.objects.filter(network__in=networks)
155 class NetworkSlice(PlCoreBase):
156 # This object exists solely so we can implement the permission check when
157 # adding slices to networks. It adds no additional fields to the relation.
159 network = models.ForeignKey(Network,related_name='networkslices')
160 slice = models.ForeignKey(Slice,related_name='networkslices')
162 def save(self, *args, **kwds):
164 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
165 # to add a sliver to the network, then one of the following must be true:
166 # 1) sliver's slice is in network's permittedSlices list,
167 # 2) sliver's slice is network's owner, or
168 # 3) network's permitAllSlices is true
169 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
171 super(NetworkSlice, self).save(*args, **kwds)
173 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
175 def can_update(self, user):
176 return self.slice.can_update(user)
179 def select_by_user(user):
181 qs = NetworkSlice.objects.all()
183 slice_ids = [s.id for s in Slice.select_by_user(user)]
184 qs = NetworkSlice.objects.filter(id__in=slice_ids)
187 class NetworkSliver(PlCoreBase):
188 network = models.ForeignKey(Network,related_name='networkslivers')
189 sliver = models.ForeignKey(Sliver,related_name='networkslivers')
190 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
191 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
193 def save(self, *args, **kwds):
194 slice = self.sliver.slice
195 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
196 # to add a sliver to the network, then one of the following must be true:
197 # 1) sliver's slice is in network's permittedSlices list,
198 # 2) sliver's slice is network's owner, or
199 # 3) network's permitAllSlices is true
200 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
202 if (not self.ip) and (NO_OBSERVER):
203 from util.network_subnet_allocator import find_unused_address
204 self.ip = find_unused_address(self.network.subnet,
205 [x.ip for x in self.network.networksliver_set.all()])
206 super(NetworkSliver, self).save(*args, **kwds)
208 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
210 def can_update(self, user):
211 return self.sliver.can_update(user)
214 def select_by_user(user):
216 qs = NetworkSliver.objects.all()
218 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
219 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
222 class Router(PlCoreBase):
223 name = models.CharField(max_length=32)
224 owner = models.ForeignKey(Slice, related_name="routers")
225 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
226 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
228 def __unicode__(self): return u'%s' % (self.name)
230 class NetworkParameterType(PlCoreBase):
231 name = models.SlugField(help_text="The name of this parameter", max_length=128)
232 description = models.CharField(max_length=1024)
234 def __unicode__(self): return u'%s' % (self.name)
236 class NetworkParameter(PlCoreBase):
237 parameter = models.ForeignKey(NetworkParameterType, related_name="networkparameters", help_text="The type of the parameter")
238 value = models.CharField(help_text="The value of this parameter", max_length=1024)
240 # The required fields to do a ObjectType lookup, and object_id assignment
241 content_type = models.ForeignKey(ContentType)
242 object_id = models.PositiveIntegerField()
243 content_object = generic.GenericForeignKey('content_type', 'object_id')
245 def __unicode__(self):
246 return self.parameter.name