+ def can_update(self, user):
+ return self.owner.can_update(user)
+
+ @property
+ def nat_list(self):
+ """ Support a list of ports in the format "protocol:port, protocol:port, ..."
+ examples:
+ tcp 123
+ tcp 123:133
+ tcp 123, tcp 124, tcp 125, udp 201, udp 202
+
+ User can put either a "/" or a " " between protocol and ports
+ Port ranges can be specified with "-" or ":"
+ """
+ nats = []
+ if self.ports:
+ parts = self.ports.split(",")
+ for part in parts:
+ part = part.strip()
+ if "/" in part:
+ (protocol, ports) = part.split("/",1)
+ elif " " in part:
+ (protocol, ports) = part.split(None,1)
+ else:
+ raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
+
+ protocol = protocol.strip()
+ ports = ports.strip()
+
+ if not (protocol in ["udp", "tcp"]):
+ raise TypeError('unknown protocol %s' % protocol)
+
+ if "-" in ports:
+ (first, last) = ports.split("-")
+ first = int(first.strip())
+ last = int(last.strip())
+ portStr = "%d:%d" % (first, last)
+ elif ":" in ports:
+ (first, last) = ports.split(":")
+ first = int(first.strip())
+ last = int(last.strip())
+ portStr = "%d:%d" % (first, last)
+ else:
+ portStr = "%d" % int(ports)
+
+ nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
+
+ return nats
+
+ @staticmethod
+ def select_by_user(user):
+ if user.is_admin:
+ qs = Network.objects.all()
+ else:
+ slices = Slice.select_by_user(user)
+ #slice_ids = [s.id for s in Slice.select_by_user(user)]
+ qs = Network.objects.filter(owner__in=slices)
+ return qs
+
+class NetworkDeployments(PlCoreBase):
+ objects = DeploymentLinkManager()
+ deleted_objects = DeploymentLinkDeletionManager()
+
+ # Stores the openstack ids at various deployments
+ network = models.ForeignKey(Network)
+ deployment = models.ForeignKey(Deployment)
+ net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
+ router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
+ subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
+ subnet = models.CharField(max_length=32, blank=True)
+
+ def can_update(self, user):
+ return user.is_admin
+
+ @staticmethod
+ def select_by_user(user):
+ if user.is_admin:
+ qs = NetworkDeployments.objects.all()
+ else:
+ slices = Slice.select_by_user(user)
+ networks = Network.objects.filter(owner__in=slices)
+ qs = NetworkDeployments.objects.filter(network__in=networks)
+ return qs
+
+class NetworkSlice(PlCoreBase):
+ # This object exists solely so we can implement the permission check when
+ # adding slices to networks. It adds no additional fields to the relation.
+
+ network = models.ForeignKey(Network)
+ slice = models.ForeignKey(Slice)
+
+ def save(self, *args, **kwds):
+ slice = self.slice
+ if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
+ # to add a sliver to the network, then one of the following must be true:
+ # 1) sliver's slice is in network's permittedSlices list,
+ # 2) sliver's slice is network's owner, or
+ # 3) network's permitAllSlices is true
+ raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
+
+ super(NetworkSlice, self).save(*args, **kwds)
+
+ def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
+
+ def can_update(self, user):
+ return self.slice.can_update(user)
+
+ @staticmethod
+ def select_by_user(user):
+ if user.is_admin:
+ qs = NetworkSlice.objects.all()
+ else:
+ slice_ids = [s.id for s in Slice.select_by_user(user)]
+ qs = NetworkSlice.objects.filter(id__in=slice_ids)
+ return qs
+