Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 1 | import os |
| 2 | import socket |
Scott Baker | bf7bc4a | 2015-01-06 15:11:29 -0800 | [diff] [blame] | 3 | import sys |
Scott Baker | 21c0c42 | 2016-02-12 18:05:34 -0800 | [diff] [blame] | 4 | from django.db import models, transaction |
Scott Baker | 0cefa53 | 2015-11-09 16:17:11 -0800 | [diff] [blame] | 5 | from core.models import PlCoreBase, Site, Slice, Instance, Controller |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 6 | from core.models import ControllerLinkManager,ControllerLinkDeletionManager |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 7 | from django.contrib.contenttypes.models import ContentType |
| 8 | from django.contrib.contenttypes import generic |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 9 | from django.core.exceptions import ValidationError |
Scott Baker | 63efe79 | 2015-10-07 22:50:21 -0700 | [diff] [blame] | 10 | from django.db.models import Q |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 11 | |
| 12 | # If true, then IP addresses will be allocated by the model. If false, then |
| 13 | # we will assume the observer handles it. |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 14 | NO_OBSERVER=False |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 15 | |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 16 | def ParseNatList(ports): |
| 17 | """ Support a list of ports in the format "protocol:port, protocol:port, ..." |
| 18 | examples: |
| 19 | tcp 123 |
| 20 | tcp 123:133 |
| 21 | tcp 123, tcp 124, tcp 125, udp 201, udp 202 |
| 22 | |
| 23 | User can put either a "/" or a " " between protocol and ports |
| 24 | Port ranges can be specified with "-" or ":" |
| 25 | """ |
| 26 | nats = [] |
| 27 | if ports: |
| 28 | parts = ports.split(",") |
| 29 | for part in parts: |
| 30 | part = part.strip() |
| 31 | if "/" in part: |
| 32 | (protocol, ports) = part.split("/",1) |
| 33 | elif " " in part: |
| 34 | (protocol, ports) = part.split(None,1) |
| 35 | else: |
| 36 | raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part) |
| 37 | |
| 38 | protocol = protocol.strip() |
| 39 | ports = ports.strip() |
| 40 | |
| 41 | if not (protocol in ["udp", "tcp"]): |
| 42 | raise ValueError('unknown protocol %s' % protocol) |
| 43 | |
| 44 | if "-" in ports: |
| 45 | (first, last) = ports.split("-") |
| 46 | first = int(first.strip()) |
| 47 | last = int(last.strip()) |
| 48 | portStr = "%d:%d" % (first, last) |
| 49 | elif ":" in ports: |
| 50 | (first, last) = ports.split(":") |
| 51 | first = int(first.strip()) |
| 52 | last = int(last.strip()) |
| 53 | portStr = "%d:%d" % (first, last) |
| 54 | else: |
| 55 | portStr = "%d" % int(ports) |
| 56 | |
| 57 | nats.append( {"l4_protocol": protocol, "l4_port": portStr} ) |
| 58 | |
| 59 | return nats |
| 60 | |
| 61 | def ValidateNatList(ports): |
| 62 | try: |
| 63 | ParseNatList(ports) |
| 64 | except Exception,e: |
| 65 | raise ValidationError(str(e)) |
| 66 | |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 67 | class ParameterMixin(object): |
| 68 | # helper classes for dealing with NetworkParameter |
| 69 | |
| 70 | def get_parameters(self): |
| 71 | parameter_dict = {} |
| 72 | |
| 73 | instance_type = ContentType.objects.get_for_model(self) |
| 74 | for param in NetworkParameter.objects.filter(content_type__pk=instance_type.id, object_id=self.id): |
| 75 | parameter_dict[param.parameter.name] = param.value |
| 76 | |
| 77 | return parameter_dict |
| 78 | |
| 79 | def set_parameter(self, name, value): |
| 80 | instance_type = ContentType.objects.get_for_model(self) |
| 81 | existing_params = NetworkParameter.objects.filter(parameter__name=name, content_type__pk=instance_type.id, object_id=self.id) |
| 82 | if existing_params: |
| 83 | p=existing_params[0] |
| 84 | p.value = value |
| 85 | p.save() |
| 86 | else: |
| 87 | pt = NetworkParameterType.objects.get(name=name) |
| 88 | p = NetworkParameter(parameter=pt, content_type=instance_type, object_id=self.id, value=value) |
| 89 | p.save() |
| 90 | |
| 91 | def unset_parameter(self, name): |
| 92 | instance_type = ContentType.objects.get_for_model(self) |
| 93 | existing_params = NetworkParameter.objects.filter(parameter__name=name, content_type__pk=instance_type.id, object_id=self.id) |
| 94 | for p in existing_params: |
| 95 | p.delete() |
| 96 | |
| 97 | |
| 98 | class NetworkTemplate(PlCoreBase, ParameterMixin): |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 99 | VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 100 | TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT')) |
Scott Baker | f2e0cfc | 2014-11-17 16:03:49 -0800 | [diff] [blame] | 101 | TOPOLOGY_CHOICES = (('bigswitch', 'BigSwitch'), ('physical', 'Physical'), ('custom', 'Custom')) |
| 102 | CONTROLLER_CHOICES = ((None, 'None'), ('onos', 'ONOS'), ('custom', 'Custom')) |
Scott Baker | 9d5bf3b | 2015-12-09 15:44:55 -0800 | [diff] [blame] | 103 | ACCESS_CHOICES = ((None, 'None'), ('indirect', 'Indirect'), ('direct', 'Direct')) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 104 | |
| 105 | name = models.CharField(max_length=32) |
| 106 | description = models.CharField(max_length=1024, blank=True, null=True) |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 107 | guaranteed_bandwidth = models.IntegerField(default=0) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 108 | visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private") |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 109 | translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none") |
Scott Baker | 9d5bf3b | 2015-12-09 15:44:55 -0800 | [diff] [blame] | 110 | access = models.CharField(max_length=30, null=True, blank=True, choices=ACCESS_CHOICES, help_text="Advertise this network as a means for other slices to contact this slice") |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 111 | shared_network_name = models.CharField(max_length=30, blank=True, null=True) |
| 112 | shared_network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
Scott Baker | 55aa265 | 2015-12-09 16:31:26 -0800 | [diff] [blame] | 113 | topology_kind = models.CharField(null=False, blank=False, max_length=30, choices=TOPOLOGY_CHOICES, default="bigswitch") |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 114 | controller_kind = models.CharField(null=True, blank=True, max_length=30, choices=CONTROLLER_CHOICES, default=None) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 115 | |
Scott Baker | a3134fe | 2014-12-23 10:56:06 -0800 | [diff] [blame] | 116 | def __init__(self, *args, **kwargs): |
| 117 | super(NetworkTemplate, self).__init__(*args, **kwargs) |
| 118 | |
| 119 | # somehow these got set wrong inside of the live database. Remove this |
| 120 | # code after all is well... |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 121 | if (self.topology_kind=="BigSwitch"): |
Scott Baker | bf7bc4a | 2015-01-06 15:11:29 -0800 | [diff] [blame] | 122 | print >> sys.stderr, "XXX warning: topology_kind invalid case" |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 123 | self.topology_kind="bigswitch" |
| 124 | elif (self.topology_kind=="Physical"): |
Scott Baker | bf7bc4a | 2015-01-06 15:11:29 -0800 | [diff] [blame] | 125 | print >> sys.stderr, "XXX warning: topology_kind invalid case" |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 126 | self.topology_kind="physical" |
| 127 | elif (self.topology_kind=="Custom"): |
Scott Baker | bf7bc4a | 2015-01-06 15:11:29 -0800 | [diff] [blame] | 128 | print >> sys.stderr, "XXX warning: topology_kind invalid case" |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 129 | self.toplogy_kind="custom" |
Scott Baker | a3134fe | 2014-12-23 10:56:06 -0800 | [diff] [blame] | 130 | |
Scott Baker | 55aa265 | 2015-12-09 16:31:26 -0800 | [diff] [blame] | 131 | def save(self, *args, **kwargs): |
| 132 | self.enforce_choices(self.access, self.ACCESS_CHOICES) |
| 133 | super(NetworkTemplate, self).save(*args, **kwargs) |
| 134 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 135 | def __unicode__(self): return u'%s' % (self.name) |
| 136 | |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 137 | class Network(PlCoreBase, ParameterMixin): |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 138 | name = models.CharField(max_length=32) |
| 139 | template = models.ForeignKey(NetworkTemplate) |
| 140 | subnet = models.CharField(max_length=32, blank=True) |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 141 | ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList]) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 142 | labels = models.CharField(max_length=1024, blank=True, null=True) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 143 | 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] | 144 | |
Scott Baker | 0451fb6 | 2015-01-03 12:29:29 -0800 | [diff] [blame] | 145 | guaranteed_bandwidth = models.IntegerField(default=0) |
| 146 | permit_all_slices = models.BooleanField(default=False) |
| 147 | permitted_slices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 148 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice") |
Scott Baker | 9368323 | 2015-09-14 11:41:05 -0700 | [diff] [blame] | 149 | instances = models.ManyToManyField(Instance, blank=True, related_name="networks", through="Port") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 150 | |
Scott Baker | 0451fb6 | 2015-01-03 12:29:29 -0800 | [diff] [blame] | 151 | topology_parameters = models.TextField(null=True, blank=True) |
| 152 | controller_url = models.CharField(null=True, blank=True, max_length=1024) |
| 153 | controller_parameters = models.TextField(null=True, blank=True) |
Scott Baker | f2e0cfc | 2014-11-17 16:03:49 -0800 | [diff] [blame] | 154 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 155 | # for observer/manager |
| 156 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 157 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 158 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 159 | |
Scott Baker | a4c11bd | 2015-08-21 16:40:53 -0700 | [diff] [blame] | 160 | autoconnect = models.BooleanField(default=True, help_text="This network can be autoconnected to the slice that owns it") |
| 161 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 162 | def __unicode__(self): return u'%s' % (self.name) |
| 163 | |
| 164 | def save(self, *args, **kwds): |
| 165 | if (not self.subnet) and (NO_OBSERVER): |
| 166 | from util.network_subnet_allocator import find_unused_subnet |
| 167 | self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()]) |
| 168 | super(Network, self).save(*args, **kwds) |
| 169 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 170 | def can_update(self, user): |
Tony Mack | 5ff90fc | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 171 | return user.can_update_slice(self.owner) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 172 | |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 173 | @property |
| 174 | def nat_list(self): |
Scott Baker | 198fda1 | 2014-10-17 16:22:20 -0700 | [diff] [blame] | 175 | return ParseNatList(self.ports) |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 176 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 177 | @staticmethod |
| 178 | def select_by_user(user): |
| 179 | if user.is_admin: |
| 180 | qs = Network.objects.all() |
| 181 | else: |
Tony Mack | 5efa133 | 2014-04-02 15:45:48 -0400 | [diff] [blame] | 182 | slices = Slice.select_by_user(user) |
| 183 | #slice_ids = [s.id for s in Slice.select_by_user(user)] |
| 184 | qs = Network.objects.filter(owner__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 185 | return qs |
| 186 | |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 187 | def get_parameters(self): |
| 188 | # returns parameters from the template, updated by self. |
| 189 | p={} |
| 190 | if self.template: |
| 191 | p = self.template.get_parameters() |
| 192 | p.update(ParameterMixin.get_parameters(self)) |
| 193 | return p |
| 194 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 195 | class ControllerNetwork(PlCoreBase): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 196 | objects = ControllerLinkManager() |
| 197 | deleted_objects = ControllerLinkDeletionManager() |
Sapan Bhatia | 6df5651 | 2014-09-22 14:52:59 -0400 | [diff] [blame] | 198 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 199 | # Stores the openstack ids at various controllers |
| 200 | network = models.ForeignKey(Network, related_name='controllernetworks') |
| 201 | controller = models.ForeignKey(Controller, related_name='controllernetworks') |
Tony Mack | 457c84c | 2014-04-08 16:37:56 -0400 | [diff] [blame] | 202 | 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] | 203 | 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] | 204 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 205 | subnet = models.CharField(max_length=32, blank=True) |
Scott Baker | da51365 | 2015-10-26 15:12:13 -0700 | [diff] [blame] | 206 | |
Tony Mack | 549f7b1 | 2015-04-11 12:17:59 -0400 | [diff] [blame] | 207 | class Meta: |
| 208 | unique_together = ('network', 'controller') |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 209 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 210 | @staticmethod |
| 211 | def select_by_user(user): |
| 212 | if user.is_admin: |
Scott Baker | 0da79c5 | 2015-02-23 17:22:33 -0800 | [diff] [blame] | 213 | qs = ControllerNetwork.objects.all() |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 214 | else: |
| 215 | slices = Slice.select_by_user(user) |
| 216 | networks = Network.objects.filter(owner__in=slices) |
Scott Baker | 0da79c5 | 2015-02-23 17:22:33 -0800 | [diff] [blame] | 217 | qs = ControllerNetwork.objects.filter(network__in=networks) |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 218 | return qs |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 219 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 220 | class NetworkSlice(PlCoreBase): |
| 221 | # This object exists solely so we can implement the permission check when |
| 222 | # adding slices to networks. It adds no additional fields to the relation. |
| 223 | |
Sapan Bhatia | 13d2db9 | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 224 | network = models.ForeignKey(Network,related_name='networkslices') |
| 225 | slice = models.ForeignKey(Slice,related_name='networkslices') |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 226 | |
Tony Mack | 549f7b1 | 2015-04-11 12:17:59 -0400 | [diff] [blame] | 227 | class Meta: |
| 228 | unique_together = ('network', 'slice') |
Tony Mack | a4736f5 | 2015-03-09 17:13:14 -0400 | [diff] [blame] | 229 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 230 | def save(self, *args, **kwds): |
| 231 | slice = self.slice |
Scott Baker | f1b474b | 2015-04-13 17:23:28 -0700 | [diff] [blame] | 232 | if (slice not in self.network.permitted_slices.all()) and (slice != self.network.owner) and (not self.network.permit_all_slices): |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 233 | # to add a instance to the network, then one of the following must be true: |
| 234 | # 1) instance's slice is in network's permittedSlices list, |
| 235 | # 2) instance's slice is network's owner, or |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 236 | # 3) network's permitAllSlices is true |
| 237 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 238 | |
| 239 | super(NetworkSlice, self).save(*args, **kwds) |
| 240 | |
| 241 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name) |
| 242 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 243 | def can_update(self, user): |
Tony Mack | 5ff90fc | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 244 | return user.can_update_slice(self.slice) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 245 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 246 | @staticmethod |
| 247 | def select_by_user(user): |
| 248 | if user.is_admin: |
| 249 | qs = NetworkSlice.objects.all() |
| 250 | else: |
| 251 | slice_ids = [s.id for s in Slice.select_by_user(user)] |
Scott Baker | 63efe79 | 2015-10-07 22:50:21 -0700 | [diff] [blame] | 252 | network_ids = [network.id for network in Network.select_by_user(user)] |
| 253 | qs = NetworkSlice.objects.filter(Q(slice__in=slice_ids) | Q(network__in=network_ids)) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 254 | return qs |
| 255 | |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 256 | class Port(PlCoreBase, ParameterMixin): |
Scott Baker | 9368323 | 2015-09-14 11:41:05 -0700 | [diff] [blame] | 257 | network = models.ForeignKey(Network,related_name='links') |
| 258 | instance = models.ForeignKey(Instance, null=True, blank=True, related_name='ports') |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 259 | ip = models.GenericIPAddressField(help_text="Instance ip address", blank=True, null=True) |
Scott Baker | 4c3ea37 | 2015-11-16 20:15:41 -0800 | [diff] [blame] | 260 | port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Neutron port id") |
Scott Baker | 0672e98 | 2015-09-08 18:22:15 -0700 | [diff] [blame] | 261 | mac = models.CharField(null=True, blank=True, max_length=256, help_text="MAC address associated with this port") |
Scott Baker | 2249f1b | 2016-02-16 11:59:07 -0800 | [diff] [blame] | 262 | xos_created = models.BooleanField(default=False) # True if XOS created this port in Neutron, False if port created by Neutron and observed by XOS |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 263 | |
Tony Mack | 549f7b1 | 2015-04-11 12:17:59 -0400 | [diff] [blame] | 264 | class Meta: |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 265 | unique_together = ('network', 'instance') |
Tony Mack | a4736f5 | 2015-03-09 17:13:14 -0400 | [diff] [blame] | 266 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 267 | def save(self, *args, **kwds): |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 268 | if self.instance: |
| 269 | slice = self.instance.slice |
Scott Baker | deb5f7b | 2015-08-18 17:04:01 -0700 | [diff] [blame] | 270 | if (slice not in self.network.permitted_slices.all()) and (slice != self.network.owner) and (not self.network.permit_all_slices): |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 271 | # to add a instance to the network, then one of the following must be true: |
| 272 | # 1) instance's slice is in network's permittedSlices list, |
| 273 | # 2) instance's slice is network's owner, or |
Scott Baker | deb5f7b | 2015-08-18 17:04:01 -0700 | [diff] [blame] | 274 | # 3) network's permitAllSlices is true |
| 275 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 276 | |
Scott Baker | 9368323 | 2015-09-14 11:41:05 -0700 | [diff] [blame] | 277 | super(Port, self).save(*args, **kwds) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 278 | |
Scott Baker | deb5f7b | 2015-08-18 17:04:01 -0700 | [diff] [blame] | 279 | def __unicode__(self): |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 280 | if self.instance: |
| 281 | return u'%s-%s' % (self.network.name, self.instance.instance_name) |
Scott Baker | deb5f7b | 2015-08-18 17:04:01 -0700 | [diff] [blame] | 282 | else: |
Scott Baker | ac4516e | 2015-08-25 23:24:36 -0700 | [diff] [blame] | 283 | return u'%s-unboundport-%s' % (self.network.name, self.id) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 284 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 285 | def can_update(self, user): |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 286 | if self.instance: |
| 287 | return user.can_update_slice(self.instance.slice) |
Scott Baker | deb5f7b | 2015-08-18 17:04:01 -0700 | [diff] [blame] | 288 | if self.network: |
| 289 | return user.can_update_slice(self.network.owner) |
| 290 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 291 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 292 | @staticmethod |
| 293 | def select_by_user(user): |
| 294 | if user.is_admin: |
Scott Baker | 9368323 | 2015-09-14 11:41:05 -0700 | [diff] [blame] | 295 | qs = Port.objects.all() |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 296 | else: |
Scott Baker | 63efe79 | 2015-10-07 22:50:21 -0700 | [diff] [blame] | 297 | instances = Instance.select_by_user(user) |
| 298 | instance_ids = [instance.id for instance in instances] |
| 299 | networks = Network.select_by_user(user) |
| 300 | network_ids = [network.id for network in networks] |
| 301 | qs = Port.objects.filter(Q(instance__in=instance_ids) | Q(network__in=network_ids)) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 302 | return qs |
| 303 | |
Scott Baker | 7db8f7c | 2015-11-16 15:07:43 -0800 | [diff] [blame] | 304 | def get_parameters(self): |
| 305 | # returns parameters from the network, updated by self. |
| 306 | p={} |
| 307 | if self.network: |
| 308 | p = self.network.get_parameters() |
| 309 | p.update(ParameterMixin.get_parameters(self)) |
| 310 | return p |
| 311 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 312 | class Router(PlCoreBase): |
| 313 | name = models.CharField(max_length=32) |
| 314 | owner = models.ForeignKey(Slice, related_name="routers") |
| 315 | permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters") |
| 316 | networks = models.ManyToManyField(Network, blank=True, related_name="routers") |
| 317 | |
| 318 | def __unicode__(self): return u'%s' % (self.name) |
| 319 | |
Tony Mack | 5ff90fc | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 320 | def can_update(self, user): |
| 321 | return user.can_update_slice(self.owner) |
| 322 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 323 | class NetworkParameterType(PlCoreBase): |
| 324 | name = models.SlugField(help_text="The name of this parameter", max_length=128) |
| 325 | description = models.CharField(max_length=1024) |
| 326 | |
| 327 | def __unicode__(self): return u'%s' % (self.name) |
| 328 | |
| 329 | class NetworkParameter(PlCoreBase): |
Sapan Bhatia | 13d2db9 | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 330 | parameter = models.ForeignKey(NetworkParameterType, related_name="networkparameters", help_text="The type of the parameter") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 331 | value = models.CharField(help_text="The value of this parameter", max_length=1024) |
| 332 | |
| 333 | # The required fields to do a ObjectType lookup, and object_id assignment |
| 334 | content_type = models.ForeignKey(ContentType) |
| 335 | object_id = models.PositiveIntegerField() |
| 336 | content_object = generic.GenericForeignKey('content_type', 'object_id') |
| 337 | |
| 338 | def __unicode__(self): |
| 339 | return self.parameter.name |
| 340 | |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 341 | class AddressPool(PlCoreBase): |
| 342 | name = models.CharField(max_length=32) |
| 343 | addresses = models.TextField(blank=True, null=True) |
Scott Baker | 082fcd4 | 2016-02-12 17:41:52 -0800 | [diff] [blame] | 344 | inuse = models.TextField(blank=True, null=True) |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 345 | |
| 346 | def __unicode__(self): return u'%s' % (self.name) |
| 347 | |
| 348 | def get_address(self): |
| 349 | with transaction.atomic(): |
| 350 | ap = AddressPool.objects.get(pk=self.pk) |
| 351 | if ap.addresses: |
Scott Baker | 2150018 | 2016-02-18 12:21:09 -0800 | [diff] [blame] | 352 | avail_ips = ap.addresses.split() |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 353 | else: |
Scott Baker | 2150018 | 2016-02-18 12:21:09 -0800 | [diff] [blame] | 354 | avail_ips = [] |
| 355 | |
| 356 | if ap.inuse: |
| 357 | inuse_ips = ap.inuse.split() |
| 358 | else: |
| 359 | inuse_ips = [] |
| 360 | |
| 361 | while avail_ips: |
| 362 | addr = avail_ips.pop(0) |
| 363 | |
| 364 | if addr in inuse_ips: |
| 365 | # This may have happened if someone re-ran the tosca |
| 366 | # recipe and 'refilled' the AddressPool while some addresses |
| 367 | # were still in use. |
| 368 | continue |
| 369 | |
| 370 | inuse_ips.insert(0,addr) |
| 371 | |
| 372 | ap.inuse = " ".join(inuse_ips) |
| 373 | ap.addresses = " ".join(avail_ips) |
| 374 | ap.save() |
| 375 | return addr |
| 376 | |
| 377 | addr = None |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 378 | return addr |
| 379 | |
| 380 | def put_address(self, addr): |
| 381 | with transaction.atomic(): |
| 382 | ap = AddressPool.objects.get(pk=self.pk) |
Scott Baker | 21c0c42 | 2016-02-12 18:05:34 -0800 | [diff] [blame] | 383 | addresses = ap.addresses or "" |
| 384 | parts = addresses.split() |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 385 | if addr not in parts: |
Scott Baker | 21c0c42 | 2016-02-12 18:05:34 -0800 | [diff] [blame] | 386 | parts.insert(0,addr) |
Scott Baker | 7b897e3 | 2016-02-12 17:31:04 -0800 | [diff] [blame] | 387 | ap.addresses = " ".join(parts) |
Scott Baker | 21c0c42 | 2016-02-12 18:05:34 -0800 | [diff] [blame] | 388 | |
| 389 | inuse = ap.inuse or "" |
| 390 | parts = inuse.split() |
| 391 | if addr in parts: |
| 392 | parts.remove(addr) |
| 393 | ap.inuse = " ".join(parts) |
| 394 | |
| 395 | ap.save() |
| 396 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 397 | |