blob: 1010caeb402ef3384e3690a105b7d038497ddc12 [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 Bhatiaed7b83b2014-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
8
9# If true, then IP addresses will be allocated by the model. If false, then
10# we will assume the observer handles it.
Scott Baker026bfe72013-07-29 16:03:50 -070011NO_OBSERVER=False
Scott Baker58a9c7a2013-07-29 15:43:07 -070012
13class NetworkTemplate(PlCoreBase):
14 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
Scott Baker87e5e092013-08-07 18:58:10 -070015 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
Scott Baker58a9c7a2013-07-29 15:43:07 -070016
17 name = models.CharField(max_length=32)
18 description = models.CharField(max_length=1024, blank=True, null=True)
19 guaranteedBandwidth = models.IntegerField(default=0)
20 visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
Scott Baker87e5e092013-08-07 18:58:10 -070021 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
Scott Baker5f814b52013-08-09 14:51:21 -070022 sharedNetworkName = models.CharField(max_length=30, blank=True, null=True)
23 sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070024
25 def __unicode__(self): return u'%s' % (self.name)
26
Scott Baker95d81c72014-08-12 18:29:27 -070027 @staticmethod
28 def select_by_user(user):
29 return NetworkTemplate.objects.all()
30
Scott Baker58a9c7a2013-07-29 15:43:07 -070031class Network(PlCoreBase):
32 name = models.CharField(max_length=32)
33 template = models.ForeignKey(NetworkTemplate)
34 subnet = models.CharField(max_length=32, blank=True)
35 ports = models.CharField(max_length=1024, blank=True, null=True)
36 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -040037 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070038
39 guaranteedBandwidth = models.IntegerField(default=0)
40 permitAllSlices = models.BooleanField(default=False)
41 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070042 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070043 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
44
Scott Baker87191e72013-08-06 08:55:07 -070045 # for observer/manager
46 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
47 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
48 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
49
Scott Baker58a9c7a2013-07-29 15:43:07 -070050 def __unicode__(self): return u'%s' % (self.name)
51
52 def save(self, *args, **kwds):
53 if (not self.subnet) and (NO_OBSERVER):
54 from util.network_subnet_allocator import find_unused_subnet
55 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
56 super(Network, self).save(*args, **kwds)
57
Tony Mack5b061472014-02-04 07:57:10 -050058 def can_update(self, user):
Tony Mack31683c82014-04-02 15:39:32 -040059 return self.owner.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -050060
Scott Baker5bbaa232014-08-14 17:23:15 -070061 @property
62 def nat_list(self):
63 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
64 examples:
65 tcp 123
66 tcp 123:133
67 tcp 123, tcp 124, tcp 125, udp 201, udp 202
68
69 User can put either a "/" or a " " between protocol and ports
70 Port ranges can be specified with "-" or ":"
71 """
72 nats = []
73 if self.ports:
74 parts = self.ports.split(",")
75 for part in parts:
76 part = part.strip()
77 if "/" in part:
78 (protocol, ports) = part.split("/",1)
79 elif " " in part:
80 (protocol, ports) = part.split(None,1)
81 else:
82 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
83
84 protocol = protocol.strip()
85 ports = ports.strip()
86
87 if not (protocol in ["udp", "tcp"]):
88 raise TypeError('unknown protocol %s' % protocol)
89
90 if "-" in ports:
91 (first, last) = ports.split("-")
92 first = int(first.strip())
93 last = int(last.strip())
94 portStr = "%d:%d" % (first, last)
95 elif ":" in ports:
96 (first, last) = ports.split(":")
97 first = int(first.strip())
98 last = int(last.strip())
99 portStr = "%d:%d" % (first, last)
100 else:
101 portStr = "%d" % int(ports)
102
103 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
104
105 return nats
106
Tony Mack5b061472014-02-04 07:57:10 -0500107 @staticmethod
108 def select_by_user(user):
109 if user.is_admin:
110 qs = Network.objects.all()
111 else:
Tony Mack5efa1332014-04-02 15:45:48 -0400112 slices = Slice.select_by_user(user)
113 #slice_ids = [s.id for s in Slice.select_by_user(user)]
114 qs = Network.objects.filter(owner__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500115 return qs
116
Tony Macke9b08692014-04-07 19:38:28 -0400117class NetworkDeployments(PlCoreBase):
Sapan Bhatiaed7b83b2014-09-22 14:52:59 -0400118 objects = DeploymentLinkManager()
119 deleted_objects = DeploymentLinkDeletionManager()
120
Tony Macke9b08692014-04-07 19:38:28 -0400121 # Stores the openstack ids at various deployments
Scott Baker5bbaa232014-08-14 17:23:15 -0700122 network = models.ForeignKey(Network)
Tony Macke9b08692014-04-07 19:38:28 -0400123 deployment = models.ForeignKey(Deployment)
Tony Mack457c84c2014-04-08 16:37:56 -0400124 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Tony Macke9b08692014-04-07 19:38:28 -0400125 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
Scott Baker95d81c72014-08-12 18:29:27 -0700126 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
127 subnet = models.CharField(max_length=32, blank=True)
Tony Macke9b08692014-04-07 19:38:28 -0400128
129 def can_update(self, user):
130 return user.is_admin
131
132 @staticmethod
133 def select_by_user(user):
134 if user.is_admin:
135 qs = NetworkDeployments.objects.all()
136 else:
137 slices = Slice.select_by_user(user)
138 networks = Network.objects.filter(owner__in=slices)
139 qs = NetworkDeployments.objects.filter(network__in=networks)
Scott Baker95d81c72014-08-12 18:29:27 -0700140 return qs
Tony Macke9b08692014-04-07 19:38:28 -0400141
Scott Baker87191e72013-08-06 08:55:07 -0700142class NetworkSlice(PlCoreBase):
143 # This object exists solely so we can implement the permission check when
144 # adding slices to networks. It adds no additional fields to the relation.
145
146 network = models.ForeignKey(Network)
147 slice = models.ForeignKey(Slice)
148
149 def save(self, *args, **kwds):
150 slice = self.slice
151 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
152 # to add a sliver to the network, then one of the following must be true:
153 # 1) sliver's slice is in network's permittedSlices list,
154 # 2) sliver's slice is network's owner, or
155 # 3) network's permitAllSlices is true
156 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
157
158 super(NetworkSlice, self).save(*args, **kwds)
159
160 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
161
Tony Mack5b061472014-02-04 07:57:10 -0500162 def can_update(self, user):
163 return self.slice.can_update(user)
164
Tony Mack5b061472014-02-04 07:57:10 -0500165 @staticmethod
166 def select_by_user(user):
167 if user.is_admin:
168 qs = NetworkSlice.objects.all()
169 else:
170 slice_ids = [s.id for s in Slice.select_by_user(user)]
171 qs = NetworkSlice.objects.filter(id__in=slice_ids)
172 return qs
173
Scott Baker58a9c7a2013-07-29 15:43:07 -0700174class NetworkSliver(PlCoreBase):
175 network = models.ForeignKey(Network)
176 sliver = models.ForeignKey(Sliver)
Scott Baker026bfe72013-07-29 16:03:50 -0700177 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -0700178 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700179
180 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -0700181 slice = self.sliver.slice
182 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
183 # to add a sliver to the network, then one of the following must be true:
184 # 1) sliver's slice is in network's permittedSlices list,
185 # 2) sliver's slice is network's owner, or
186 # 3) network's permitAllSlices is true
187 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
188
Scott Baker58a9c7a2013-07-29 15:43:07 -0700189 if (not self.ip) and (NO_OBSERVER):
190 from util.network_subnet_allocator import find_unused_address
191 self.ip = find_unused_address(self.network.subnet,
192 [x.ip for x in self.network.networksliver_set.all()])
193 super(NetworkSliver, self).save(*args, **kwds)
194
195 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
196
Tony Mack5b061472014-02-04 07:57:10 -0500197 def can_update(self, user):
198 return self.sliver.can_update(user)
199
Tony Mack5b061472014-02-04 07:57:10 -0500200 @staticmethod
201 def select_by_user(user):
202 if user.is_admin:
203 qs = NetworkSliver.objects.all()
204 else:
205 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
206 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
207 return qs
208
Scott Baker58a9c7a2013-07-29 15:43:07 -0700209class Router(PlCoreBase):
210 name = models.CharField(max_length=32)
211 owner = models.ForeignKey(Slice, related_name="routers")
212 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
213 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
214
215 def __unicode__(self): return u'%s' % (self.name)
216
217class NetworkParameterType(PlCoreBase):
218 name = models.SlugField(help_text="The name of this parameter", max_length=128)
219 description = models.CharField(max_length=1024)
220
221 def __unicode__(self): return u'%s' % (self.name)
222
223class NetworkParameter(PlCoreBase):
224 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
225 value = models.CharField(help_text="The value of this parameter", max_length=1024)
226
227 # The required fields to do a ObjectType lookup, and object_id assignment
228 content_type = models.ForeignKey(ContentType)
229 object_id = models.PositiveIntegerField()
230 content_object = generic.GenericForeignKey('content_type', 'object_id')
231
232 def __unicode__(self):
233 return self.parameter.name
234
235