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 |
| 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 Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 11 | NO_OBSERVER=False |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 12 | |
| 13 | class NetworkTemplate(PlCoreBase): |
| 14 | VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 15 | TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT')) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 16 | |
| 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 Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 21 | translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none") |
Scott Baker | 5f814b5 | 2013-08-09 14:51:21 -0700 | [diff] [blame] | 22 | 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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 24 | |
| 25 | def __unicode__(self): return u'%s' % (self.name) |
| 26 | |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 27 | @staticmethod |
| 28 | def select_by_user(user): |
| 29 | return NetworkTemplate.objects.all() |
| 30 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 31 | class 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 Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 37 | 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] | 38 | |
| 39 | guaranteedBandwidth = models.IntegerField(default=0) |
| 40 | permitAllSlices = models.BooleanField(default=False) |
| 41 | permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 42 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 43 | slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver") |
| 44 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 45 | # 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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 50 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 58 | def can_update(self, user): |
Tony Mack | 31683c8 | 2014-04-02 15:39:32 -0400 | [diff] [blame] | 59 | return self.owner.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 60 | |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 61 | @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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 107 | @staticmethod |
| 108 | def select_by_user(user): |
| 109 | if user.is_admin: |
| 110 | qs = Network.objects.all() |
| 111 | else: |
Tony Mack | 5efa133 | 2014-04-02 15:45:48 -0400 | [diff] [blame] | 112 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 115 | return qs |
| 116 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 117 | class NetworkDeployments(PlCoreBase): |
Sapan Bhatia | 6df5651 | 2014-09-22 14:52:59 -0400 | [diff] [blame] | 118 | objects = DeploymentLinkManager() |
| 119 | deleted_objects = DeploymentLinkDeletionManager() |
| 120 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 121 | # Stores the openstack ids at various deployments |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 122 | network = models.ForeignKey(Network) |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 123 | deployment = models.ForeignKey(Deployment) |
Tony Mack | 457c84c | 2014-04-08 16:37:56 -0400 | [diff] [blame] | 124 | 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] | 125 | 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] | 126 | 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 Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 128 | |
| 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 Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 140 | return qs |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 141 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 142 | class 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 162 | def can_update(self, user): |
| 163 | return self.slice.can_update(user) |
| 164 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 165 | @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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 174 | class NetworkSliver(PlCoreBase): |
| 175 | network = models.ForeignKey(Network) |
| 176 | sliver = models.ForeignKey(Sliver) |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 177 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame] | 178 | 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] | 179 | |
| 180 | def save(self, *args, **kwds): |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 181 | 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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 189 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 197 | def can_update(self, user): |
| 198 | return self.sliver.can_update(user) |
| 199 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 200 | @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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 209 | class 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 | |
| 217 | class 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 | |
| 223 | class 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 | |