blob: 0b3400a5136d62f8814e2c95d5c7ba18284aaa88 [file] [log] [blame]
Scott Baker58a9c7a2013-07-29 15:43:07 -07001import os
2import socket
3from django.db import models
Tony Macke9b08692014-04-07 19:38:28 -04004from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
Sapan Bhatia6df56512014-09-22 14:52:59 -04005from core.models import DeploymentLinkManager,DeploymentLinkDeletionManager
Scott Baker58a9c7a2013-07-29 15:43:07 -07006from django.contrib.contenttypes.models import ContentType
7from django.contrib.contenttypes import generic
Scott Baker198fda12014-10-17 16:22:20 -07008from django.core.exceptions import ValidationError
Scott Baker58a9c7a2013-07-29 15:43:07 -07009
10# If true, then IP addresses will be allocated by the model. If false, then
11# we will assume the observer handles it.
Scott Baker026bfe72013-07-29 16:03:50 -070012NO_OBSERVER=False
Scott Baker58a9c7a2013-07-29 15:43:07 -070013
Scott Baker198fda12014-10-17 16:22:20 -070014def ParseNatList(ports):
15 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
16 examples:
17 tcp 123
18 tcp 123:133
19 tcp 123, tcp 124, tcp 125, udp 201, udp 202
20
21 User can put either a "/" or a " " between protocol and ports
22 Port ranges can be specified with "-" or ":"
23 """
24 nats = []
25 if ports:
26 parts = ports.split(",")
27 for part in parts:
28 part = part.strip()
29 if "/" in part:
30 (protocol, ports) = part.split("/",1)
31 elif " " in part:
32 (protocol, ports) = part.split(None,1)
33 else:
34 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
35
36 protocol = protocol.strip()
37 ports = ports.strip()
38
39 if not (protocol in ["udp", "tcp"]):
40 raise ValueError('unknown protocol %s' % protocol)
41
42 if "-" in ports:
43 (first, last) = ports.split("-")
44 first = int(first.strip())
45 last = int(last.strip())
46 portStr = "%d:%d" % (first, last)
47 elif ":" in ports:
48 (first, last) = ports.split(":")
49 first = int(first.strip())
50 last = int(last.strip())
51 portStr = "%d:%d" % (first, last)
52 else:
53 portStr = "%d" % int(ports)
54
55 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
56
57 return nats
58
59def ValidateNatList(ports):
60 try:
61 ParseNatList(ports)
62 except Exception,e:
63 raise ValidationError(str(e))
64
Scott Baker58a9c7a2013-07-29 15:43:07 -070065class NetworkTemplate(PlCoreBase):
66 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
Scott Baker87e5e092013-08-07 18:58:10 -070067 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
Scott Bakerf2e0cfc2014-11-17 16:03:49 -080068 TOPOLOGY_CHOICES = (('bigswitch', 'BigSwitch'), ('physical', 'Physical'), ('custom', 'Custom'))
69 CONTROLLER_CHOICES = ((None, 'None'), ('onos', 'ONOS'), ('custom', 'Custom'))
Scott Baker58a9c7a2013-07-29 15:43:07 -070070
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")
Scott Baker87e5e092013-08-07 18:58:10 -070075 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
Scott Baker5f814b52013-08-09 14:51:21 -070076 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")
Scott Bakerf2e0cfc2014-11-17 16:03:49 -080078 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)
Scott Baker58a9c7a2013-07-29 15:43:07 -070080
81 def __unicode__(self): return u'%s' % (self.name)
82
83class Network(PlCoreBase):
84 name = models.CharField(max_length=32)
85 template = models.ForeignKey(NetworkTemplate)
86 subnet = models.CharField(max_length=32, blank=True)
Scott Baker198fda12014-10-17 16:22:20 -070087 ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList])
Scott Baker58a9c7a2013-07-29 15:43:07 -070088 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -040089 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070090
91 guaranteedBandwidth = models.IntegerField(default=0)
92 permitAllSlices = models.BooleanField(default=False)
93 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070094 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070095 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
96
Scott Bakerf2e0cfc2014-11-17 16:03:49 -080097 topologyParameters = models.TextField(null=True, blank=True)
Scott Baker7b6722e2014-11-17 16:18:00 -080098 controllerUrl = models.CharField(null=True, blank=True, max_length=1024)
Scott Bakerf2e0cfc2014-11-17 16:03:49 -080099 controllerParameters = models.TextField(null=True, blank=True)
100
Scott Baker87191e72013-08-06 08:55:07 -0700101 # for observer/manager
102 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
103 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
104 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
105
Scott Baker58a9c7a2013-07-29 15:43:07 -0700106 def __unicode__(self): return u'%s' % (self.name)
107
108 def save(self, *args, **kwds):
109 if (not self.subnet) and (NO_OBSERVER):
110 from util.network_subnet_allocator import find_unused_subnet
111 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
112 super(Network, self).save(*args, **kwds)
113
Tony Mack5b061472014-02-04 07:57:10 -0500114 def can_update(self, user):
Tony Mack31683c82014-04-02 15:39:32 -0400115 return self.owner.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500116
Scott Baker5bbaa232014-08-14 17:23:15 -0700117 @property
118 def nat_list(self):
Scott Baker198fda12014-10-17 16:22:20 -0700119 return ParseNatList(self.ports)
Scott Baker5bbaa232014-08-14 17:23:15 -0700120
Tony Mack5b061472014-02-04 07:57:10 -0500121 @staticmethod
122 def select_by_user(user):
123 if user.is_admin:
124 qs = Network.objects.all()
125 else:
Tony Mack5efa1332014-04-02 15:45:48 -0400126 slices = Slice.select_by_user(user)
127 #slice_ids = [s.id for s in Slice.select_by_user(user)]
128 qs = Network.objects.filter(owner__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500129 return qs
130
Tony Macke9b08692014-04-07 19:38:28 -0400131class NetworkDeployments(PlCoreBase):
Sapan Bhatia6df56512014-09-22 14:52:59 -0400132 objects = DeploymentLinkManager()
133 deleted_objects = DeploymentLinkDeletionManager()
134
Tony Macke9b08692014-04-07 19:38:28 -0400135 # Stores the openstack ids at various deployments
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500136 network = models.ForeignKey(Network, related_name='networkdeployments')
137 deployment = models.ForeignKey(Deployment, related_name='networkdeployments')
Tony Mack457c84c2014-04-08 16:37:56 -0400138 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Tony Macke9b08692014-04-07 19:38:28 -0400139 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
Scott Baker95d81c72014-08-12 18:29:27 -0700140 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
141 subnet = models.CharField(max_length=32, blank=True)
Tony Macke9b08692014-04-07 19:38:28 -0400142
143 def can_update(self, user):
144 return user.is_admin
145
146 @staticmethod
147 def select_by_user(user):
148 if user.is_admin:
149 qs = NetworkDeployments.objects.all()
150 else:
151 slices = Slice.select_by_user(user)
152 networks = Network.objects.filter(owner__in=slices)
153 qs = NetworkDeployments.objects.filter(network__in=networks)
Scott Baker95d81c72014-08-12 18:29:27 -0700154 return qs
Tony Macke9b08692014-04-07 19:38:28 -0400155
Scott Baker87191e72013-08-06 08:55:07 -0700156class NetworkSlice(PlCoreBase):
157 # This object exists solely so we can implement the permission check when
158 # adding slices to networks. It adds no additional fields to the relation.
159
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500160 network = models.ForeignKey(Network,related_name='networkslices')
161 slice = models.ForeignKey(Slice,related_name='networkslices')
Scott Baker87191e72013-08-06 08:55:07 -0700162
163 def save(self, *args, **kwds):
164 slice = self.slice
165 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
166 # to add a sliver to the network, then one of the following must be true:
167 # 1) sliver's slice is in network's permittedSlices list,
168 # 2) sliver's slice is network's owner, or
169 # 3) network's permitAllSlices is true
170 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
171
172 super(NetworkSlice, self).save(*args, **kwds)
173
174 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
175
Tony Mack5b061472014-02-04 07:57:10 -0500176 def can_update(self, user):
177 return self.slice.can_update(user)
178
Tony Mack5b061472014-02-04 07:57:10 -0500179 @staticmethod
180 def select_by_user(user):
181 if user.is_admin:
182 qs = NetworkSlice.objects.all()
183 else:
184 slice_ids = [s.id for s in Slice.select_by_user(user)]
185 qs = NetworkSlice.objects.filter(id__in=slice_ids)
186 return qs
187
Scott Baker58a9c7a2013-07-29 15:43:07 -0700188class NetworkSliver(PlCoreBase):
Sapan Bhatiaab9f84b2014-11-11 22:01:30 -0500189 network = models.ForeignKey(Network,related_name='networkslivers')
190 sliver = models.ForeignKey(Sliver,related_name='networkslivers')
Scott Baker026bfe72013-07-29 16:03:50 -0700191 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -0700192 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700193
194 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -0700195 slice = self.sliver.slice
196 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
197 # to add a sliver to the network, then one of the following must be true:
198 # 1) sliver's slice is in network's permittedSlices list,
199 # 2) sliver's slice is network's owner, or
200 # 3) network's permitAllSlices is true
201 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
202
Scott Baker58a9c7a2013-07-29 15:43:07 -0700203 if (not self.ip) and (NO_OBSERVER):
204 from util.network_subnet_allocator import find_unused_address
205 self.ip = find_unused_address(self.network.subnet,
206 [x.ip for x in self.network.networksliver_set.all()])
207 super(NetworkSliver, self).save(*args, **kwds)
208
209 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
210
Tony Mack5b061472014-02-04 07:57:10 -0500211 def can_update(self, user):
212 return self.sliver.can_update(user)
213
Tony Mack5b061472014-02-04 07:57:10 -0500214 @staticmethod
215 def select_by_user(user):
216 if user.is_admin:
217 qs = NetworkSliver.objects.all()
218 else:
219 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
220 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
221 return qs
222
Scott Baker58a9c7a2013-07-29 15:43:07 -0700223class Router(PlCoreBase):
224 name = models.CharField(max_length=32)
225 owner = models.ForeignKey(Slice, related_name="routers")
226 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
227 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
228
229 def __unicode__(self): return u'%s' % (self.name)
230
231class NetworkParameterType(PlCoreBase):
232 name = models.SlugField(help_text="The name of this parameter", max_length=128)
233 description = models.CharField(max_length=1024)
234
235 def __unicode__(self): return u'%s' % (self.name)
236
237class NetworkParameter(PlCoreBase):
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500238 parameter = models.ForeignKey(NetworkParameterType, related_name="networkparameters", help_text="The type of the parameter")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700239 value = models.CharField(help_text="The value of this parameter", max_length=1024)
240
241 # The required fields to do a ObjectType lookup, and object_id assignment
242 content_type = models.ForeignKey(ContentType)
243 object_id = models.PositiveIntegerField()
244 content_object = generic.GenericForeignKey('content_type', 'object_id')
245
246 def __unicode__(self):
247 return self.parameter.name
248
249