update code to use renamed network fields
[plstackapi.git] / planetstack / core / models / network.py
index 298c54d..554bbf5 100644 (file)
@@ -1,25 +1,97 @@
 import os
 import socket
 from django.db import models
-from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
+from core.models import PlCoreBase, Site, Slice, Sliver, Controller
+from core.models import ControllerLinkManager,ControllerLinkDeletionManager
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
+from django.core.exceptions import ValidationError
 
 # If true, then IP addresses will be allocated by the model. If false, then
 # we will assume the observer handles it.
 NO_OBSERVER=False
 
+def ParseNatList(ports):
+    """ 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 ports:
+        parts = 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 ValueError('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
+
+def ValidateNatList(ports):
+    try:
+        ParseNatList(ports)
+    except Exception,e:
+        raise ValidationError(str(e))
+
 class NetworkTemplate(PlCoreBase):
     VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
     TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
+    TOPOLOGY_CHOICES = (('bigswitch', 'BigSwitch'), ('physical', 'Physical'), ('custom', 'Custom'))
+    CONTROLLER_CHOICES = ((None, 'None'), ('onos', 'ONOS'), ('custom', 'Custom'))
 
     name = models.CharField(max_length=32)
     description = models.CharField(max_length=1024, blank=True, null=True)
-    guaranteedBandwidth = models.IntegerField(default=0)
+    guaranteed_bandwidth = models.IntegerField(default=0)
     visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
     translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
-    sharedNetworkName = models.CharField(max_length=30, blank=True, null=True)
-    sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
+    shared_network_name = models.CharField(max_length=30, blank=True, null=True)
+    shared_network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
+    topology_kind = models.CharField(null=False, blank=False, max_length=30, choices=TOPOLOGY_CHOICES, default="BigSwitch")
+    controller_kind = models.CharField(null=True, blank=True, max_length=30, choices=CONTROLLER_CHOICES, default=None)
+
+    def __init__(self, *args, **kwargs):
+        super(NetworkTemplate, self).__init__(*args, **kwargs)
+
+        # somehow these got set wrong inside of the live database. Remove this
+        # code after all is well...
+        if (self.topology_kind=="BigSwitch"):
+            print "XXX warning: topology_kind invalid case"
+            self.topology_kind="bigswitch"
+        elif (self.topology_kind=="Physical"):
+            print "XXX warning: topology_kind invalid case"
+            self.topology_kind="physical"
+        elif (self.topology_kind=="Custom"):
+            print "XXX warning: topology_kind invalid case"
+            self.toplogy_kind="custom"
 
     def __unicode__(self):  return u'%s' % (self.name)
 
@@ -27,16 +99,20 @@ class Network(PlCoreBase):
     name = models.CharField(max_length=32)
     template = models.ForeignKey(NetworkTemplate)
     subnet = models.CharField(max_length=32, blank=True)
-    ports = models.CharField(max_length=1024, blank=True, null=True)
+    ports = models.CharField(max_length=1024, blank=True, null=True, validators=[ValidateNatList])
     labels = models.CharField(max_length=1024, blank=True, null=True)
     owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
 
-    guaranteedBandwidth = models.IntegerField(default=0)
-    permitAllSlices = models.BooleanField(default=False)
-    permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
+    guaranteed_bandwidth = models.IntegerField(default=0)
+    permit_all_slices = models.BooleanField(default=False)
+    permitted_slices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
     slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
     slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
 
+    topology_parameters = models.TextField(null=True, blank=True)
+    controller_url = models.CharField(null=True, blank=True, max_length=1024)
+    controller_parameters = models.TextField(null=True, blank=True)
+
     # for observer/manager
     network_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")
@@ -53,6 +129,10 @@ class Network(PlCoreBase):
     def can_update(self, user):
         return self.owner.can_update(user)
 
+    @property
+    def nat_list(self):
+        return ParseNatList(self.ports)
+
     @staticmethod
     def select_by_user(user):
         if user.is_admin:
@@ -63,14 +143,17 @@ class Network(PlCoreBase):
             qs = Network.objects.filter(owner__in=slices)
         return qs
 
-class NetworkDeployments(PlCoreBase):
-    # Stores the openstack ids at various deployments
-    network = models.ForeignKey(Network)    
-    deployment = models.ForeignKey(Deployment)
+class ControllerNetworks(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
+
+    # Stores the openstack ids at various controllers
+    network = models.ForeignKey(Network, related_name='controllernetworks')
+    controller = models.ForeignKey(Controller, related_name='controllernetworks')
     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)    
+    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
@@ -78,23 +161,23 @@ class NetworkDeployments(PlCoreBase):
     @staticmethod
     def select_by_user(user):
         if user.is_admin:
-            qs = NetworkDeployments.objects.all()
+            qs = NetworkControllers.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      
+            qs = NetworkControllers.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)
+    network = models.ForeignKey(Network,related_name='networkslices')
+    slice = models.ForeignKey(Slice,related_name='networkslices')
 
     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):
+        if (slice not in self.network.permitted_slices.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
@@ -118,14 +201,14 @@ class NetworkSlice(PlCoreBase):
         return qs
 
 class NetworkSliver(PlCoreBase):
-    network = models.ForeignKey(Network)
-    sliver = models.ForeignKey(Sliver)
+    network = models.ForeignKey(Network,related_name='networkslivers')
+    sliver = models.ForeignKey(Sliver,related_name='networkslivers')
     ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
     port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
 
     def save(self, *args, **kwds):
         slice = self.sliver.slice
-        if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
+        if (slice not in self.network.permitted_slices.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
@@ -167,7 +250,7 @@ class NetworkParameterType(PlCoreBase):
     def __unicode__(self):  return u'%s' % (self.name)
 
 class NetworkParameter(PlCoreBase):
-    parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
+    parameter = models.ForeignKey(NetworkParameterType, related_name="networkparameters", help_text="The type of the parameter")
     value = models.CharField(help_text="The value of this parameter", max_length=1024)
 
     # The required fields to do a ObjectType lookup, and object_id assignment