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 | |
| 27 | class 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 Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 33 | 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] | 34 | |
| 35 | guaranteedBandwidth = models.IntegerField(default=0) |
| 36 | permitAllSlices = models.BooleanField(default=False) |
| 37 | permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 38 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 39 | slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver") |
| 40 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 41 | # 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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 46 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 54 | def can_update(self, user): |
Tony Mack | 31683c8 | 2014-04-02 15:39:32 -0400 | [diff] [blame] | 55 | return self.owner.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 56 | |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 57 | @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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 103 | @staticmethod |
| 104 | def select_by_user(user): |
| 105 | if user.is_admin: |
| 106 | qs = Network.objects.all() |
| 107 | else: |
Tony Mack | 5efa133 | 2014-04-02 15:45:48 -0400 | [diff] [blame] | 108 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 111 | return qs |
| 112 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 113 | class NetworkDeployments(PlCoreBase): |
Sapan Bhatia | 6df5651 | 2014-09-22 14:52:59 -0400 | [diff] [blame] | 114 | objects = DeploymentLinkManager() |
| 115 | deleted_objects = DeploymentLinkDeletionManager() |
| 116 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 117 | # Stores the openstack ids at various deployments |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 118 | network = models.ForeignKey(Network) |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 119 | deployment = models.ForeignKey(Deployment) |
Tony Mack | 457c84c | 2014-04-08 16:37:56 -0400 | [diff] [blame] | 120 | 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] | 121 | 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] | 122 | 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 Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 124 | |
| 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 Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 136 | return qs |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 137 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 138 | class 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 158 | def can_update(self, user): |
| 159 | return self.slice.can_update(user) |
| 160 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 161 | @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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 170 | class NetworkSliver(PlCoreBase): |
| 171 | network = models.ForeignKey(Network) |
| 172 | sliver = models.ForeignKey(Sliver) |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 173 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame] | 174 | 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] | 175 | |
| 176 | def save(self, *args, **kwds): |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 177 | 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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 185 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 193 | def can_update(self, user): |
| 194 | return self.sliver.can_update(user) |
| 195 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 196 | @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 Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 205 | class 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 | |
| 213 | class 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 | |
| 219 | class 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 | |