blob: c9ae2141adf660d997f55843f6e0ffc93163cee7 [file] [log] [blame]
Scott Baker58a9c7a2013-07-29 15:43:07 -07001import os
2import socket
Scott Bakerbf7bc4a2015-01-06 15:11:29 -08003import sys
Scott Baker58a9c7a2013-07-29 15:43:07 -07004from django.db import models
Tony Mackf3bbe472014-11-30 15:33:35 -05005from core.models import PlCoreBase, Site, Slice, Sliver, Controller
6from core.models import ControllerLinkManager,ControllerLinkDeletionManager
Scott Baker58a9c7a2013-07-29 15:43:07 -07007from django.contrib.contenttypes.models import ContentType
8from django.contrib.contenttypes import generic
Scott Baker198fda12014-10-17 16:22:20 -07009from django.core.exceptions import ValidationError
Scott Baker58a9c7a2013-07-29 15:43:07 -070010
11# If true, then IP addresses will be allocated by the model. If false, then
12# we will assume the observer handles it.
Scott Baker026bfe72013-07-29 16:03:50 -070013NO_OBSERVER=False
Scott Baker58a9c7a2013-07-29 15:43:07 -070014
Scott Baker198fda12014-10-17 16:22:20 -070015def ParseNatList(ports):
16 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
17 examples:
18 tcp 123
19 tcp 123:133
20 tcp 123, tcp 124, tcp 125, udp 201, udp 202
21
22 User can put either a "/" or a " " between protocol and ports
23 Port ranges can be specified with "-" or ":"
24 """
25 nats = []
26 if ports:
27 parts = ports.split(",")
28 for part in parts:
29 part = part.strip()
30 if "/" in part:
31 (protocol, ports) = part.split("/",1)
32 elif " " in part:
33 (protocol, ports) = part.split(None,1)
34 else:
35 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
36
37 protocol = protocol.strip()
38 ports = ports.strip()
39
40 if not (protocol in ["udp", "tcp"]):
41 raise ValueError('unknown protocol %s' % protocol)
42
43 if "-" in ports:
44 (first, last) = ports.split("-")
45 first = int(first.strip())
46 last = int(last.strip())
47 portStr = "%d:%d" % (first, last)
48 elif ":" in ports:
49 (first, last) = ports.split(":")
50 first = int(first.strip())
51 last = int(last.strip())
52 portStr = "%d:%d" % (first, last)
53 else:
54 portStr = "%d" % int(ports)
55
56 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
57
58 return nats
59
60def ValidateNatList(ports):
61 try:
62 ParseNatList(ports)
63 except Exception,e:
64 raise ValidationError(str(e))
65
Scott Baker58a9c7a2013-07-29 15:43:07 -070066class NetworkTemplate(PlCoreBase):
67 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
Scott Baker87e5e092013-08-07 18:58:10 -070068 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
Scott Bakerf2e0cfc2014-11-17 16:03:49 -080069 TOPOLOGY_CHOICES = (('bigswitch', 'BigSwitch'), ('physical', 'Physical'), ('custom', 'Custom'))
70 CONTROLLER_CHOICES = ((None, 'None'), ('onos', 'ONOS'), ('custom', 'Custom'))
Scott Baker58a9c7a2013-07-29 15:43:07 -070071
72 name = models.CharField(max_length=32)
73 description = models.CharField(max_length=1024, blank=True, null=True)
Scott Baker81fa17f2015-01-03 12:03:38 -080074 guaranteed_bandwidth = models.IntegerField(default=0)
Scott Baker58a9c7a2013-07-29 15:43:07 -070075 visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
Scott Baker87e5e092013-08-07 18:58:10 -070076 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
Scott Baker81fa17f2015-01-03 12:03:38 -080077 shared_network_name = models.CharField(max_length=30, blank=True, null=True)
78 shared_network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
79 topology_kind = models.CharField(null=False, blank=False, max_length=30, choices=TOPOLOGY_CHOICES, default="BigSwitch")
80 controller_kind = models.CharField(null=True, blank=True, max_length=30, choices=CONTROLLER_CHOICES, default=None)
Scott Baker58a9c7a2013-07-29 15:43:07 -070081
Scott Bakera3134fe2014-12-23 10:56:06 -080082 def __init__(self, *args, **kwargs):
83 super(NetworkTemplate, self).__init__(*args, **kwargs)
84
85 # somehow these got set wrong inside of the live database. Remove this
86 # code after all is well...
Scott Baker81fa17f2015-01-03 12:03:38 -080087 if (self.topology_kind=="BigSwitch"):
Scott Bakerbf7bc4a2015-01-06 15:11:29 -080088 print >> sys.stderr, "XXX warning: topology_kind invalid case"
Scott Baker81fa17f2015-01-03 12:03:38 -080089 self.topology_kind="bigswitch"
90 elif (self.topology_kind=="Physical"):
Scott Bakerbf7bc4a2015-01-06 15:11:29 -080091 print >> sys.stderr, "XXX warning: topology_kind invalid case"
Scott Baker81fa17f2015-01-03 12:03:38 -080092 self.topology_kind="physical"
93 elif (self.topology_kind=="Custom"):
Scott Bakerbf7bc4a2015-01-06 15:11:29 -080094 print >> sys.stderr, "XXX warning: topology_kind invalid case"
Scott Baker81fa17f2015-01-03 12:03:38 -080095 self.toplogy_kind="custom"
Scott Bakera3134fe2014-12-23 10:56:06 -080096
Scott Baker58a9c7a2013-07-29 15:43:07 -070097 def __unicode__(self): return u'%s' % (self.name)
98
99class Network(PlCoreBase):
100 name = models.CharField(max_length=32)
101 template = models.ForeignKey(NetworkTemplate)
102 subnet = models.CharField(max_length=32, blank=True)
Scott Baker198fda12014-10-17 16:22:20 -0700103 ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList])
Scott Baker58a9c7a2013-07-29 15:43:07 -0700104 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -0400105 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700106
Scott Baker0451fb62015-01-03 12:29:29 -0800107 guaranteed_bandwidth = models.IntegerField(default=0)
108 permit_all_slices = models.BooleanField(default=False)
109 permitted_slices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -0700110 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700111 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
112
Scott Baker0451fb62015-01-03 12:29:29 -0800113 topology_parameters = models.TextField(null=True, blank=True)
114 controller_url = models.CharField(null=True, blank=True, max_length=1024)
115 controller_parameters = models.TextField(null=True, blank=True)
Scott Bakerf2e0cfc2014-11-17 16:03:49 -0800116
Scott Baker87191e72013-08-06 08:55:07 -0700117 # for observer/manager
118 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
119 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
120 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
121
Scott Baker58a9c7a2013-07-29 15:43:07 -0700122 def __unicode__(self): return u'%s' % (self.name)
123
124 def save(self, *args, **kwds):
125 if (not self.subnet) and (NO_OBSERVER):
126 from util.network_subnet_allocator import find_unused_subnet
127 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
128 super(Network, self).save(*args, **kwds)
129
Tony Mack5b061472014-02-04 07:57:10 -0500130 def can_update(self, user):
Tony Mack5ff90fc2015-02-08 21:38:41 -0500131 return user.can_update_slice(self.owner)
Tony Mack5b061472014-02-04 07:57:10 -0500132
Scott Baker5bbaa232014-08-14 17:23:15 -0700133 @property
134 def nat_list(self):
Scott Baker198fda12014-10-17 16:22:20 -0700135 return ParseNatList(self.ports)
Scott Baker5bbaa232014-08-14 17:23:15 -0700136
Tony Mack5b061472014-02-04 07:57:10 -0500137 @staticmethod
138 def select_by_user(user):
139 if user.is_admin:
140 qs = Network.objects.all()
141 else:
Tony Mack5efa1332014-04-02 15:45:48 -0400142 slices = Slice.select_by_user(user)
143 #slice_ids = [s.id for s in Slice.select_by_user(user)]
144 qs = Network.objects.filter(owner__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500145 return qs
146
Tony Macka7dbd422015-01-05 22:48:11 -0500147class ControllerNetwork(PlCoreBase):
Tony Mackf3bbe472014-11-30 15:33:35 -0500148 objects = ControllerLinkManager()
149 deleted_objects = ControllerLinkDeletionManager()
Sapan Bhatia6df56512014-09-22 14:52:59 -0400150
Tony Mackf3bbe472014-11-30 15:33:35 -0500151 # Stores the openstack ids at various controllers
152 network = models.ForeignKey(Network, related_name='controllernetworks')
153 controller = models.ForeignKey(Controller, related_name='controllernetworks')
Tony Mack457c84c2014-04-08 16:37:56 -0400154 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Tony Macke9b08692014-04-07 19:38:28 -0400155 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
Scott Baker95d81c72014-08-12 18:29:27 -0700156 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
157 subnet = models.CharField(max_length=32, blank=True)
Tony Macke9b08692014-04-07 19:38:28 -0400158
Tony Macke9b08692014-04-07 19:38:28 -0400159 @staticmethod
160 def select_by_user(user):
161 if user.is_admin:
Tony Mackf3bbe472014-11-30 15:33:35 -0500162 qs = NetworkControllers.objects.all()
Tony Macke9b08692014-04-07 19:38:28 -0400163 else:
164 slices = Slice.select_by_user(user)
165 networks = Network.objects.filter(owner__in=slices)
Tony Mackf3bbe472014-11-30 15:33:35 -0500166 qs = NetworkControllers.objects.filter(network__in=networks)
Scott Baker95d81c72014-08-12 18:29:27 -0700167 return qs
Tony Macke9b08692014-04-07 19:38:28 -0400168
Scott Baker87191e72013-08-06 08:55:07 -0700169class NetworkSlice(PlCoreBase):
170 # This object exists solely so we can implement the permission check when
171 # adding slices to networks. It adds no additional fields to the relation.
172
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500173 network = models.ForeignKey(Network,related_name='networkslices')
174 slice = models.ForeignKey(Slice,related_name='networkslices')
Scott Baker87191e72013-08-06 08:55:07 -0700175
176 def save(self, *args, **kwds):
177 slice = self.slice
Scott Baker0451fb62015-01-03 12:29:29 -0800178 if (slice not in self.network.permitted_slices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
Scott Baker87191e72013-08-06 08:55:07 -0700179 # to add a sliver to the network, then one of the following must be true:
180 # 1) sliver's slice is in network's permittedSlices list,
181 # 2) sliver's slice is network's owner, or
182 # 3) network's permitAllSlices is true
183 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
184
185 super(NetworkSlice, self).save(*args, **kwds)
186
187 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
188
Tony Mack5b061472014-02-04 07:57:10 -0500189 def can_update(self, user):
Tony Mack5ff90fc2015-02-08 21:38:41 -0500190 return user.can_update_slice(self.slice)
Tony Mack5b061472014-02-04 07:57:10 -0500191
Tony Mack5b061472014-02-04 07:57:10 -0500192 @staticmethod
193 def select_by_user(user):
194 if user.is_admin:
195 qs = NetworkSlice.objects.all()
196 else:
197 slice_ids = [s.id for s in Slice.select_by_user(user)]
198 qs = NetworkSlice.objects.filter(id__in=slice_ids)
199 return qs
200
Scott Baker58a9c7a2013-07-29 15:43:07 -0700201class NetworkSliver(PlCoreBase):
Sapan Bhatiaab9f84b2014-11-11 22:01:30 -0500202 network = models.ForeignKey(Network,related_name='networkslivers')
203 sliver = models.ForeignKey(Sliver,related_name='networkslivers')
Scott Baker026bfe72013-07-29 16:03:50 -0700204 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -0700205 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700206
207 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -0700208 slice = self.sliver.slice
Scott Baker0451fb62015-01-03 12:29:29 -0800209 if (slice not in self.network.permitted_slices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
Scott Baker87191e72013-08-06 08:55:07 -0700210 # to add a sliver to the network, then one of the following must be true:
211 # 1) sliver's slice is in network's permittedSlices list,
212 # 2) sliver's slice is network's owner, or
213 # 3) network's permitAllSlices is true
214 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
215
Scott Baker58a9c7a2013-07-29 15:43:07 -0700216 if (not self.ip) and (NO_OBSERVER):
217 from util.network_subnet_allocator import find_unused_address
218 self.ip = find_unused_address(self.network.subnet,
219 [x.ip for x in self.network.networksliver_set.all()])
220 super(NetworkSliver, self).save(*args, **kwds)
221
222 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
223
Tony Mack5b061472014-02-04 07:57:10 -0500224 def can_update(self, user):
Tony Mack5ff90fc2015-02-08 21:38:41 -0500225 return user.can_update_slice(self.sliver.slice)
Tony Mack5b061472014-02-04 07:57:10 -0500226
Tony Mack5b061472014-02-04 07:57:10 -0500227 @staticmethod
228 def select_by_user(user):
229 if user.is_admin:
230 qs = NetworkSliver.objects.all()
231 else:
232 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
233 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
234 return qs
235
Scott Baker58a9c7a2013-07-29 15:43:07 -0700236class Router(PlCoreBase):
237 name = models.CharField(max_length=32)
238 owner = models.ForeignKey(Slice, related_name="routers")
239 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
240 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
241
242 def __unicode__(self): return u'%s' % (self.name)
243
Tony Mack5ff90fc2015-02-08 21:38:41 -0500244 def can_update(self, user):
245 return user.can_update_slice(self.owner)
246
Scott Baker58a9c7a2013-07-29 15:43:07 -0700247class NetworkParameterType(PlCoreBase):
248 name = models.SlugField(help_text="The name of this parameter", max_length=128)
249 description = models.CharField(max_length=1024)
250
251 def __unicode__(self): return u'%s' % (self.name)
252
253class NetworkParameter(PlCoreBase):
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500254 parameter = models.ForeignKey(NetworkParameterType, related_name="networkparameters", help_text="The type of the parameter")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700255 value = models.CharField(help_text="The value of this parameter", max_length=1024)
256
257 # The required fields to do a ObjectType lookup, and object_id assignment
258 content_type = models.ForeignKey(ContentType)
259 object_id = models.PositiveIntegerField()
260 content_object = generic.GenericForeignKey('content_type', 'object_id')
261
262 def __unicode__(self):
263 return self.parameter.name
264
265