Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 1 | import os |
| 2 | import socket |
| 3 | from django.db import models |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 4 | from core.models import PlCoreBase, Site, Slice, Sliver, Deployment |
Sapan Bhatia | 6df5651 | 2014-09-22 14:52:59 -0400 | [diff] [blame] | 5 | from core.models import DeploymentLinkManager,DeploymentLinkDeletionManager |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 6 | from django.contrib.contenttypes.models import ContentType |
| 7 | from django.contrib.contenttypes import generic |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 8 | from django.core.exceptions import ValidationError |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 9 | |
| 10 | # If true, then IP addresses will be allocated by the model. If false, then |
| 11 | # we will assume the observer handles it. |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 12 | NO_OBSERVER=False |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 13 | |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 14 | def 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 | |
| 59 | def ValidateNatList(ports): |
| 60 | try: |
| 61 | ParseNatList(ports) |
| 62 | except Exception,e: |
| 63 | raise ValidationError(str(e)) |
| 64 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 65 | class NetworkTemplate(PlCoreBase): |
| 66 | VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 67 | TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT')) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 68 | |
| 69 | name = models.CharField(max_length=32) |
| 70 | description = models.CharField(max_length=1024, blank=True, null=True) |
| 71 | guaranteedBandwidth = models.IntegerField(default=0) |
| 72 | visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private") |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 73 | translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none") |
Scott Baker | 5f814b5 | 2013-08-09 14:51:21 -0700 | [diff] [blame] | 74 | sharedNetworkName = models.CharField(max_length=30, blank=True, null=True) |
| 75 | sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 76 | |
| 77 | def __unicode__(self): return u'%s' % (self.name) |
| 78 | |
| 79 | class Network(PlCoreBase): |
| 80 | name = models.CharField(max_length=32) |
| 81 | template = models.ForeignKey(NetworkTemplate) |
| 82 | subnet = models.CharField(max_length=32, blank=True) |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 83 | ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList]) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 84 | labels = models.CharField(max_length=1024, blank=True, null=True) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 85 | owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 86 | |
| 87 | guaranteedBandwidth = models.IntegerField(default=0) |
| 88 | permitAllSlices = models.BooleanField(default=False) |
| 89 | permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 90 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 91 | slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver") |
| 92 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 93 | # for observer/manager |
| 94 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 95 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 96 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 97 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 98 | def __unicode__(self): return u'%s' % (self.name) |
| 99 | |
| 100 | def save(self, *args, **kwds): |
| 101 | if (not self.subnet) and (NO_OBSERVER): |
| 102 | from util.network_subnet_allocator import find_unused_subnet |
| 103 | self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()]) |
| 104 | super(Network, self).save(*args, **kwds) |
| 105 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 106 | def can_update(self, user): |
Tony Mack | 31683c8 | 2014-04-02 15:39:32 -0400 | [diff] [blame] | 107 | return self.owner.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 108 | |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 109 | @property |
| 110 | def nat_list(self): |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 111 | return ParseNatList(self.ports) |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 112 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 113 | @staticmethod |
| 114 | def select_by_user(user): |
| 115 | if user.is_admin: |
| 116 | qs = Network.objects.all() |
| 117 | else: |
Tony Mack | 5efa133 | 2014-04-02 15:45:48 -0400 | [diff] [blame] | 118 | slices = Slice.select_by_user(user) |
| 119 | #slice_ids = [s.id for s in Slice.select_by_user(user)] |
| 120 | qs = Network.objects.filter(owner__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 121 | return qs |
| 122 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 123 | class NetworkDeployments(PlCoreBase): |
Sapan Bhatia | 6df5651 | 2014-09-22 14:52:59 -0400 | [diff] [blame] | 124 | objects = DeploymentLinkManager() |
| 125 | deleted_objects = DeploymentLinkDeletionManager() |
| 126 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 127 | # Stores the openstack ids at various deployments |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 128 | network = models.ForeignKey(Network) |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 129 | deployment = models.ForeignKey(Deployment) |
Tony Mack | 457c84c | 2014-04-08 16:37:56 -0400 | [diff] [blame] | 130 | net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 131 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 132 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 133 | subnet = models.CharField(max_length=32, blank=True) |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 134 | |
| 135 | def can_update(self, user): |
| 136 | return user.is_admin |
| 137 | |
| 138 | @staticmethod |
| 139 | def select_by_user(user): |
| 140 | if user.is_admin: |
| 141 | qs = NetworkDeployments.objects.all() |
| 142 | else: |
| 143 | slices = Slice.select_by_user(user) |
| 144 | networks = Network.objects.filter(owner__in=slices) |
| 145 | qs = NetworkDeployments.objects.filter(network__in=networks) |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 146 | return qs |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 147 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 148 | class NetworkSlice(PlCoreBase): |
| 149 | # This object exists solely so we can implement the permission check when |
| 150 | # adding slices to networks. It adds no additional fields to the relation. |
| 151 | |
| 152 | network = models.ForeignKey(Network) |
| 153 | slice = models.ForeignKey(Slice) |
| 154 | |
| 155 | def save(self, *args, **kwds): |
| 156 | slice = self.slice |
| 157 | if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices): |
| 158 | # to add a sliver to the network, then one of the following must be true: |
| 159 | # 1) sliver's slice is in network's permittedSlices list, |
| 160 | # 2) sliver's slice is network's owner, or |
| 161 | # 3) network's permitAllSlices is true |
| 162 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 163 | |
| 164 | super(NetworkSlice, self).save(*args, **kwds) |
| 165 | |
| 166 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name) |
| 167 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 168 | def can_update(self, user): |
| 169 | return self.slice.can_update(user) |
| 170 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 171 | @staticmethod |
| 172 | def select_by_user(user): |
| 173 | if user.is_admin: |
| 174 | qs = NetworkSlice.objects.all() |
| 175 | else: |
| 176 | slice_ids = [s.id for s in Slice.select_by_user(user)] |
| 177 | qs = NetworkSlice.objects.filter(id__in=slice_ids) |
| 178 | return qs |
| 179 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 180 | class NetworkSliver(PlCoreBase): |
| 181 | network = models.ForeignKey(Network) |
| 182 | sliver = models.ForeignKey(Sliver) |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 183 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame] | 184 | port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 185 | |
| 186 | def save(self, *args, **kwds): |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 187 | slice = self.sliver.slice |
| 188 | if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices): |
| 189 | # to add a sliver to the network, then one of the following must be true: |
| 190 | # 1) sliver's slice is in network's permittedSlices list, |
| 191 | # 2) sliver's slice is network's owner, or |
| 192 | # 3) network's permitAllSlices is true |
| 193 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 194 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 195 | if (not self.ip) and (NO_OBSERVER): |
| 196 | from util.network_subnet_allocator import find_unused_address |
| 197 | self.ip = find_unused_address(self.network.subnet, |
| 198 | [x.ip for x in self.network.networksliver_set.all()]) |
| 199 | super(NetworkSliver, self).save(*args, **kwds) |
| 200 | |
| 201 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name) |
| 202 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 203 | def can_update(self, user): |
| 204 | return self.sliver.can_update(user) |
| 205 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 206 | @staticmethod |
| 207 | def select_by_user(user): |
| 208 | if user.is_admin: |
| 209 | qs = NetworkSliver.objects.all() |
| 210 | else: |
| 211 | sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)] |
| 212 | qs = NetworkSliver.objects.filter(id__in=sliver_ids) |
| 213 | return qs |
| 214 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 215 | class Router(PlCoreBase): |
| 216 | name = models.CharField(max_length=32) |
| 217 | owner = models.ForeignKey(Slice, related_name="routers") |
| 218 | permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters") |
| 219 | networks = models.ManyToManyField(Network, blank=True, related_name="routers") |
| 220 | |
| 221 | def __unicode__(self): return u'%s' % (self.name) |
| 222 | |
| 223 | class NetworkParameterType(PlCoreBase): |
| 224 | name = models.SlugField(help_text="The name of this parameter", max_length=128) |
| 225 | description = models.CharField(max_length=1024) |
| 226 | |
| 227 | def __unicode__(self): return u'%s' % (self.name) |
| 228 | |
| 229 | class NetworkParameter(PlCoreBase): |
| 230 | parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter") |
| 231 | value = models.CharField(help_text="The value of this parameter", max_length=1024) |
| 232 | |
| 233 | # The required fields to do a ObjectType lookup, and object_id assignment |
| 234 | content_type = models.ForeignKey(ContentType) |
| 235 | object_id = models.PositiveIntegerField() |
| 236 | content_object = generic.GenericForeignKey('content_type', 'object_id') |
| 237 | |
| 238 | def __unicode__(self): |
| 239 | return self.parameter.name |
| 240 | |
| 241 | |