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 |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 5 | from django.contrib.contenttypes.models import ContentType |
| 6 | from django.contrib.contenttypes import generic |
| 7 | |
| 8 | # If true, then IP addresses will be allocated by the model. If false, then |
| 9 | # we will assume the observer handles it. |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 10 | NO_OBSERVER=False |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 11 | |
| 12 | class NetworkTemplate(PlCoreBase): |
| 13 | VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private')) |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 14 | TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT')) |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 15 | |
| 16 | name = models.CharField(max_length=32) |
| 17 | description = models.CharField(max_length=1024, blank=True, null=True) |
| 18 | guaranteedBandwidth = models.IntegerField(default=0) |
| 19 | visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private") |
Scott Baker | 87e5e09 | 2013-08-07 18:58:10 -0700 | [diff] [blame] | 20 | translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none") |
Scott Baker | 5f814b5 | 2013-08-09 14:51:21 -0700 | [diff] [blame] | 21 | sharedNetworkName = models.CharField(max_length=30, blank=True, null=True) |
| 22 | 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] | 23 | |
| 24 | def __unicode__(self): return u'%s' % (self.name) |
| 25 | |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 26 | @staticmethod |
| 27 | def select_by_user(user): |
| 28 | return NetworkTemplate.objects.all() |
| 29 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 30 | class Network(PlCoreBase): |
| 31 | name = models.CharField(max_length=32) |
| 32 | template = models.ForeignKey(NetworkTemplate) |
| 33 | subnet = models.CharField(max_length=32, blank=True) |
| 34 | ports = models.CharField(max_length=1024, blank=True, null=True) |
| 35 | labels = models.CharField(max_length=1024, blank=True, null=True) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 36 | 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] | 37 | |
| 38 | guaranteedBandwidth = models.IntegerField(default=0) |
| 39 | permitAllSlices = models.BooleanField(default=False) |
| 40 | permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks") |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 41 | slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice") |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 42 | slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver") |
| 43 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 44 | # for observer/manager |
| 45 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 46 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 47 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 48 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 49 | def __unicode__(self): return u'%s' % (self.name) |
| 50 | |
| 51 | def save(self, *args, **kwds): |
| 52 | if (not self.subnet) and (NO_OBSERVER): |
| 53 | from util.network_subnet_allocator import find_unused_subnet |
| 54 | self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()]) |
| 55 | super(Network, self).save(*args, **kwds) |
| 56 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 57 | def can_update(self, user): |
Tony Mack | 31683c8 | 2014-04-02 15:39:32 -0400 | [diff] [blame] | 58 | return self.owner.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 59 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 60 | @staticmethod |
| 61 | def select_by_user(user): |
| 62 | if user.is_admin: |
| 63 | qs = Network.objects.all() |
| 64 | else: |
Tony Mack | 5efa133 | 2014-04-02 15:45:48 -0400 | [diff] [blame] | 65 | slices = Slice.select_by_user(user) |
| 66 | #slice_ids = [s.id for s in Slice.select_by_user(user)] |
| 67 | qs = Network.objects.filter(owner__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 68 | return qs |
| 69 | |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 70 | class NetworkDeployments(PlCoreBase): |
| 71 | # Stores the openstack ids at various deployments |
| 72 | network = models.ForeignKey(Network) |
| 73 | deployment = models.ForeignKey(Deployment) |
Tony Mack | 457c84c | 2014-04-08 16:37:56 -0400 | [diff] [blame] | 74 | 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] | 75 | 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] | 76 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 77 | subnet = models.CharField(max_length=32, blank=True) |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 78 | |
| 79 | def can_update(self, user): |
| 80 | return user.is_admin |
| 81 | |
| 82 | @staticmethod |
| 83 | def select_by_user(user): |
| 84 | if user.is_admin: |
| 85 | qs = NetworkDeployments.objects.all() |
| 86 | else: |
| 87 | slices = Slice.select_by_user(user) |
| 88 | networks = Network.objects.filter(owner__in=slices) |
| 89 | qs = NetworkDeployments.objects.filter(network__in=networks) |
Scott Baker | 95d81c7 | 2014-08-12 18:29:27 -0700 | [diff] [blame] | 90 | return qs |
Tony Mack | e9b0869 | 2014-04-07 19:38:28 -0400 | [diff] [blame] | 91 | |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 92 | class NetworkSlice(PlCoreBase): |
| 93 | # This object exists solely so we can implement the permission check when |
| 94 | # adding slices to networks. It adds no additional fields to the relation. |
| 95 | |
| 96 | network = models.ForeignKey(Network) |
| 97 | slice = models.ForeignKey(Slice) |
| 98 | |
| 99 | def save(self, *args, **kwds): |
| 100 | slice = self.slice |
| 101 | if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices): |
| 102 | # to add a sliver to the network, then one of the following must be true: |
| 103 | # 1) sliver's slice is in network's permittedSlices list, |
| 104 | # 2) sliver's slice is network's owner, or |
| 105 | # 3) network's permitAllSlices is true |
| 106 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 107 | |
| 108 | super(NetworkSlice, self).save(*args, **kwds) |
| 109 | |
| 110 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name) |
| 111 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 112 | def can_update(self, user): |
| 113 | return self.slice.can_update(user) |
| 114 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 115 | @staticmethod |
| 116 | def select_by_user(user): |
| 117 | if user.is_admin: |
| 118 | qs = NetworkSlice.objects.all() |
| 119 | else: |
| 120 | slice_ids = [s.id for s in Slice.select_by_user(user)] |
| 121 | qs = NetworkSlice.objects.filter(id__in=slice_ids) |
| 122 | return qs |
| 123 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 124 | class NetworkSliver(PlCoreBase): |
| 125 | network = models.ForeignKey(Network) |
| 126 | sliver = models.ForeignKey(Sliver) |
Scott Baker | 026bfe7 | 2013-07-29 16:03:50 -0700 | [diff] [blame] | 127 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame] | 128 | 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] | 129 | |
| 130 | def save(self, *args, **kwds): |
Scott Baker | 87191e7 | 2013-08-06 08:55:07 -0700 | [diff] [blame] | 131 | slice = self.sliver.slice |
| 132 | if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices): |
| 133 | # to add a sliver to the network, then one of the following must be true: |
| 134 | # 1) sliver's slice is in network's permittedSlices list, |
| 135 | # 2) sliver's slice is network's owner, or |
| 136 | # 3) network's permitAllSlices is true |
| 137 | raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network))) |
| 138 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 139 | if (not self.ip) and (NO_OBSERVER): |
| 140 | from util.network_subnet_allocator import find_unused_address |
| 141 | self.ip = find_unused_address(self.network.subnet, |
| 142 | [x.ip for x in self.network.networksliver_set.all()]) |
| 143 | super(NetworkSliver, self).save(*args, **kwds) |
| 144 | |
| 145 | def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name) |
| 146 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 147 | def can_update(self, user): |
| 148 | return self.sliver.can_update(user) |
| 149 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 150 | @staticmethod |
| 151 | def select_by_user(user): |
| 152 | if user.is_admin: |
| 153 | qs = NetworkSliver.objects.all() |
| 154 | else: |
| 155 | sliver_ids = [s.id for s in NetworkSliver.select_by_user(user)] |
| 156 | qs = NetworkSliver.objects.filter(id__in=sliver_ids) |
| 157 | return qs |
| 158 | |
Scott Baker | 58a9c7a | 2013-07-29 15:43:07 -0700 | [diff] [blame] | 159 | class Router(PlCoreBase): |
| 160 | name = models.CharField(max_length=32) |
| 161 | owner = models.ForeignKey(Slice, related_name="routers") |
| 162 | permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters") |
| 163 | networks = models.ManyToManyField(Network, blank=True, related_name="routers") |
| 164 | |
| 165 | def __unicode__(self): return u'%s' % (self.name) |
| 166 | |
| 167 | class NetworkParameterType(PlCoreBase): |
| 168 | name = models.SlugField(help_text="The name of this parameter", max_length=128) |
| 169 | description = models.CharField(max_length=1024) |
| 170 | |
| 171 | def __unicode__(self): return u'%s' % (self.name) |
| 172 | |
| 173 | class NetworkParameter(PlCoreBase): |
| 174 | parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter") |
| 175 | value = models.CharField(help_text="The value of this parameter", max_length=1024) |
| 176 | |
| 177 | # The required fields to do a ObjectType lookup, and object_id assignment |
| 178 | content_type = models.ForeignKey(ContentType) |
| 179 | object_id = models.PositiveIntegerField() |
| 180 | content_object = generic.GenericForeignKey('content_type', 'object_id') |
| 181 | |
| 182 | def __unicode__(self): |
| 183 | return self.parameter.name |
| 184 | |
| 185 | |