blob: 51cc0c8708de57ec6630c1e0eff8aa3d77d31b3c [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
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
27class Network(PlCoreBase):
28 name = models.CharField(max_length=32)
29 template = models.ForeignKey(NetworkTemplate)
30 subnet = models.CharField(max_length=32, blank=True)
31 ports = models.CharField(max_length=1024, blank=True, null=True)
32 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -040033 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070034
35 guaranteedBandwidth = models.IntegerField(default=0)
36 permitAllSlices = models.BooleanField(default=False)
37 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070038 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070039 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
40
Scott Baker87191e72013-08-06 08:55:07 -070041 # for observer/manager
42 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
43 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
44 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
45
Scott Baker58a9c7a2013-07-29 15:43:07 -070046 def __unicode__(self): return u'%s' % (self.name)
47
48 def save(self, *args, **kwds):
49 if (not self.subnet) and (NO_OBSERVER):
50 from util.network_subnet_allocator import find_unused_subnet
51 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
52 super(Network, self).save(*args, **kwds)
53
Tony Mack5b061472014-02-04 07:57:10 -050054 def can_update(self, user):
Tony Mack31683c82014-04-02 15:39:32 -040055 return self.owner.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -050056
Scott Baker5bbaa232014-08-14 17:23:15 -070057 @property
58 def nat_list(self):
59 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
60 examples:
61 tcp 123
62 tcp 123:133
63 tcp 123, tcp 124, tcp 125, udp 201, udp 202
64
65 User can put either a "/" or a " " between protocol and ports
66 Port ranges can be specified with "-" or ":"
67 """
68 nats = []
69 if self.ports:
70 parts = self.ports.split(",")
71 for part in parts:
72 part = part.strip()
73 if "/" in part:
74 (protocol, ports) = part.split("/",1)
75 elif " " in part:
76 (protocol, ports) = part.split(None,1)
77 else:
78 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
79
80 protocol = protocol.strip()
81 ports = ports.strip()
82
83 if not (protocol in ["udp", "tcp"]):
84 raise TypeError('unknown protocol %s' % protocol)
85
86 if "-" in ports:
87 (first, last) = ports.split("-")
88 first = int(first.strip())
89 last = int(last.strip())
90 portStr = "%d:%d" % (first, last)
91 elif ":" in ports:
92 (first, last) = ports.split(":")
93 first = int(first.strip())
94 last = int(last.strip())
95 portStr = "%d:%d" % (first, last)
96 else:
97 portStr = "%d" % int(ports)
98
99 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
100
101 return nats
102
Tony Mack5b061472014-02-04 07:57:10 -0500103 @staticmethod
104 def select_by_user(user):
105 if user.is_admin:
106 qs = Network.objects.all()
107 else:
Tony Mack5efa1332014-04-02 15:45:48 -0400108 slices = Slice.select_by_user(user)
109 #slice_ids = [s.id for s in Slice.select_by_user(user)]
110 qs = Network.objects.filter(owner__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500111 return qs
112
Tony Macke9b08692014-04-07 19:38:28 -0400113class NetworkDeployments(PlCoreBase):
Sapan Bhatia6df56512014-09-22 14:52:59 -0400114 objects = DeploymentLinkManager()
115 deleted_objects = DeploymentLinkDeletionManager()
116
Tony Macke9b08692014-04-07 19:38:28 -0400117 # Stores the openstack ids at various deployments
Scott Baker5bbaa232014-08-14 17:23:15 -0700118 network = models.ForeignKey(Network)
Tony Macke9b08692014-04-07 19:38:28 -0400119 deployment = models.ForeignKey(Deployment)
Tony Mack457c84c2014-04-08 16:37:56 -0400120 net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Tony Macke9b08692014-04-07 19:38:28 -0400121 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
Scott Baker95d81c72014-08-12 18:29:27 -0700122 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
123 subnet = models.CharField(max_length=32, blank=True)
Tony Macke9b08692014-04-07 19:38:28 -0400124
125 def can_update(self, user):
126 return user.is_admin
127
128 @staticmethod
129 def select_by_user(user):
130 if user.is_admin:
131 qs = NetworkDeployments.objects.all()
132 else:
133 slices = Slice.select_by_user(user)
134 networks = Network.objects.filter(owner__in=slices)
135 qs = NetworkDeployments.objects.filter(network__in=networks)
Scott Baker95d81c72014-08-12 18:29:27 -0700136 return qs
Tony Macke9b08692014-04-07 19:38:28 -0400137
Scott Baker87191e72013-08-06 08:55:07 -0700138class NetworkSlice(PlCoreBase):
139 # This object exists solely so we can implement the permission check when
140 # adding slices to networks. It adds no additional fields to the relation.
141
142 network = models.ForeignKey(Network)
143 slice = models.ForeignKey(Slice)
144
145 def save(self, *args, **kwds):
146 slice = self.slice
147 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
148 # to add a sliver to the network, then one of the following must be true:
149 # 1) sliver's slice is in network's permittedSlices list,
150 # 2) sliver's slice is network's owner, or
151 # 3) network's permitAllSlices is true
152 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
153
154 super(NetworkSlice, self).save(*args, **kwds)
155
156 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
157
Tony Mack5b061472014-02-04 07:57:10 -0500158 def can_update(self, user):
159 return self.slice.can_update(user)
160
Tony Mack5b061472014-02-04 07:57:10 -0500161 @staticmethod
162 def select_by_user(user):
163 if user.is_admin:
164 qs = NetworkSlice.objects.all()
165 else:
166 slice_ids = [s.id for s in Slice.select_by_user(user)]
167 qs = NetworkSlice.objects.filter(id__in=slice_ids)
168 return qs
169
Scott Baker58a9c7a2013-07-29 15:43:07 -0700170class NetworkSliver(PlCoreBase):
171 network = models.ForeignKey(Network)
172 sliver = models.ForeignKey(Sliver)
Scott Baker026bfe72013-07-29 16:03:50 -0700173 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -0700174 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -0700175
176 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -0700177 slice = self.sliver.slice
178 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
179 # 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
Scott Baker58a9c7a2013-07-29 15:43:07 -0700185 if (not self.ip) and (NO_OBSERVER):
186 from util.network_subnet_allocator import find_unused_address
187 self.ip = find_unused_address(self.network.subnet,
188 [x.ip for x in self.network.networksliver_set.all()])
189 super(NetworkSliver, self).save(*args, **kwds)
190
191 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
192
Tony Mack5b061472014-02-04 07:57:10 -0500193 def can_update(self, user):
194 return self.sliver.can_update(user)
195
Tony Mack5b061472014-02-04 07:57:10 -0500196 @staticmethod
197 def select_by_user(user):
198 if user.is_admin:
199 qs = NetworkSliver.objects.all()
200 else:
201 sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)]
202 qs = NetworkSliver.objects.filter(id__in=sliver_ids)
203 return qs
204
Scott Baker58a9c7a2013-07-29 15:43:07 -0700205class Router(PlCoreBase):
206 name = models.CharField(max_length=32)
207 owner = models.ForeignKey(Slice, related_name="routers")
208 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
209 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
210
211 def __unicode__(self): return u'%s' % (self.name)
212
213class NetworkParameterType(PlCoreBase):
214 name = models.SlugField(help_text="The name of this parameter", max_length=128)
215 description = models.CharField(max_length=1024)
216
217 def __unicode__(self): return u'%s' % (self.name)
218
219class NetworkParameter(PlCoreBase):
220 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
221 value = models.CharField(help_text="The value of this parameter", max_length=1024)
222
223 # The required fields to do a ObjectType lookup, and object_id assignment
224 content_type = models.ForeignKey(ContentType)
225 object_id = models.PositiveIntegerField()
226 content_object = generic.GenericForeignKey('content_type', 'object_id')
227
228 def __unicode__(self):
229 return self.parameter.name
230
231