CORD-1177: Brought vOLT service over to xproto

Change-Id: Ia80b9bef655302694d7a48fde2aa16d04ae657a5
diff --git a/xos/attic/accessagent_model.py b/xos/attic/accessagent_model.py
new file mode 100644
index 0000000..087b164
--- /dev/null
+++ b/xos/attic/accessagent_model.py
@@ -0,0 +1 @@
+def __unicode__(self): return u'%s' % (self.name)
diff --git a/xos/attic/accessdevice_model.py b/xos/attic/accessdevice_model.py
new file mode 100644
index 0000000..88c7d5f
--- /dev/null
+++ b/xos/attic/accessdevice_model.py
@@ -0,0 +1 @@
+def __unicode__(self): return u'%s-%d:%d' % (self.volt_device.name,self.uplink,self.vlan)
diff --git a/xos/attic/agentportmapping_model.py b/xos/attic/agentportmapping_model.py
new file mode 100644
index 0000000..d499bd2
--- /dev/null
+++ b/xos/attic/agentportmapping_model.py
@@ -0,0 +1 @@
+def __unicode__(self): return u'%s-%s-%s' % (self.access_agent.name, self.port, self.mac)
diff --git a/xos/attic/cordsubscriberroot_bottom.py b/xos/attic/cordsubscriberroot_bottom.py
new file mode 100644
index 0000000..5821215
--- /dev/null
+++ b/xos/attic/cordsubscriberroot_bottom.py
@@ -0,0 +1 @@
+CordSubscriberRoot.setup_simple_attributes()
diff --git a/xos/attic/cordsubscriberroot_model.py b/xos/attic/cordsubscriberroot_model.py
new file mode 100644
index 0000000..6183113
--- /dev/null
+++ b/xos/attic/cordsubscriberroot_model.py
@@ -0,0 +1,116 @@
+# 'simple_attributes' will be expanded into properties and setters that
+# store the attribute using self.set_attribute / self.get_attribute.
+
+simple_attributes = ( ("devices", []), )
+
+sync_attributes = ("firewall_enable",
+                   "firewall_rules",
+                   "url_filter_enable",
+                   "url_filter_rules",
+                   "cdn_enable",
+                   "uplink_speed",
+                   "downlink_speed",
+                   "enable_uverse",
+                   "status")
+
+def __init__(self, *args, **kwargs):
+    super(CordSubscriberRoot, self).__init__(*args, **kwargs)
+    self.cached_volt = None
+
+@property
+def volt(self):
+    volt = self.get_newest_subscribed_tenant(VOLTTenant)
+    if not volt:
+        return None
+
+    # always return the same object when possible
+    if (self.cached_volt) and (self.cached_volt.id == volt.id):
+        return self.cached_volt
+
+    #volt.caller = self.creator
+    self.cached_volt = volt
+    return volt
+
+def find_device(self, mac):
+    for device in self.devices:
+        if device["mac"] == mac:
+            return device
+    return None
+
+def update_device(self, mac, **kwargs):
+    # kwargs may be "level" or "mac"
+    #    Setting one of these to None will cause None to be stored in the db
+    devices = self.devices
+    for device in devices:
+        if device["mac"] == mac:
+            for arg in kwargs.keys():
+                device[arg] = kwargs[arg]
+            self.devices = devices
+            return device
+    raise ValueError("Device with mac %s not found" % mac)
+
+def create_device(self, **kwargs):
+    if "mac" not in kwargs:
+        raise XOSMissingField("The mac field is required")
+
+    if self.find_device(kwargs['mac']):
+            raise XOSDuplicateKey("Device with mac %s already exists" % kwargs["mac"])
+
+    device = kwargs.copy()
+
+    devices = self.devices
+    devices.append(device)
+    self.devices = devices
+
+    return device
+
+def delete_device(self, mac):
+    devices = self.devices
+    for device in devices:
+        if device["mac"]==mac:
+            devices.remove(device)
+            self.devices = devices
+            return
+
+    raise ValueError("Device with mac %s not found" % mac)
+
+#--------------------------------------------------------------------------
+# Deprecated -- devices used to be called users
+
+def find_user(self, uid):
+    return self.find_device(uid)
+
+def update_user(self, uid, **kwargs):
+    return self.update_device(uid, **kwargs)
+
+def create_user(self, **kwargs):
+    return self.create_device(**kwargs)
+
+def delete_user(self, uid):
+    return self.delete_user(uid)
+
+# ------------------------------------------------------------------------
+
+@property
+def services(self):
+    return {"cdn": self.cdn_enable,
+            "url_filter": self.url_filter_enable,
+            "firewall": self.firewall_enable}
+
+@services.setter
+def services(self, value):
+    pass
+
+def save(self, *args, **kwargs):
+    self.validate_unique_service_specific_id(none_okay=True)
+    if (not hasattr(self, 'caller') or not self.caller.is_admin):
+        if (self.has_field_changed("service_specific_id")):
+            raise XOSPermissionDenied("You do not have permission to change service_specific_id")
+    super(CordSubscriberRoot, self).save(*args, **kwargs)
+
+    # TODO - reimplement this as a watcher
+
+    if (self.volt) and (self.volt.vcpe):
+        # 1) trigger manage_bbs_account to run
+        # 2) trigger vcpe observer to wake up
+        self.volt.vcpe.save()
diff --git a/xos/attic/header.py b/xos/attic/header.py
new file mode 100644
index 0000000..8561d92
--- /dev/null
+++ b/xos/attic/header.py
@@ -0,0 +1,23 @@
+from django.db import models
+from django.db.models import *
+from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, TenantRoot, NetworkParameter, NetworkParameterType, Port, AddressPool, User
+from core.models.plcorebase import StrippedCharField
+import os
+from django.db import models, transaction
+from django.forms.models import model_to_dict
+from django.db.models import Q
+from operator import itemgetter, attrgetter, methodcaller
+from core.models import Tag
+from core.models.service import LeastLoadedNodeScheduler
+from services.vrouter.models import VRouterService, VRouterTenant
+import traceback
+from xos.exceptions import *
+from xos.config import Config
+
+class ConfigurationError(Exception):
+    pass
+
+VOLT_KIND = "vOLT"
+CORD_SUBSCRIBER_KIND = "CordSubscriberRoot"
+
+CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
diff --git a/xos/attic/voltdevice_model.py b/xos/attic/voltdevice_model.py
new file mode 100644
index 0000000..087b164
--- /dev/null
+++ b/xos/attic/voltdevice_model.py
@@ -0,0 +1 @@
+def __unicode__(self): return u'%s' % (self.name)
diff --git a/xos/attic/volttenant_bottom.py b/xos/attic/volttenant_bottom.py
new file mode 100644
index 0000000..d0935de
--- /dev/null
+++ b/xos/attic/volttenant_bottom.py
@@ -0,0 +1,11 @@
+def model_policy_volt(pk):
+    # TODO: this should be made in to a real model_policy
+    with transaction.atomic():
+        volt = VOLTTenant.objects.select_for_update().filter(pk=pk)
+        if not volt:
+            return
+        volt = volt[0]
+        volt.manage_vcpe()
+        volt.manage_subscriber()
+        volt.cleanup_orphans()
+
diff --git a/xos/attic/volttenant_model.py b/xos/attic/volttenant_model.py
new file mode 100644
index 0000000..e9a101b
--- /dev/null
+++ b/xos/attic/volttenant_model.py
@@ -0,0 +1,117 @@
+def __init__(self, *args, **kwargs):
+    volt_services = VOLTService.get_service_objects().all()
+    if volt_services:
+        self._meta.get_field("provider_service").default = volt_services[0].id
+    super(VOLTTenant, self).__init__(*args, **kwargs)
+    self.cached_vcpe = None
+
+@property
+def vcpe(self):
+    from services.vsg.models import VSGTenant
+    vcpe = self.get_newest_subscribed_tenant(VSGTenant)
+    if not vcpe:
+        return None
+
+    # always return the same object when possible
+    if (self.cached_vcpe) and (self.cached_vcpe.id == vcpe.id):
+        return self.cached_vcpe
+
+    vcpe.caller = self.creator
+    self.cached_vcpe = vcpe
+    return vcpe
+
+@vcpe.setter
+def vcpe(self, value):
+    raise XOSConfigurationError("vOLT.vCPE cannot be set this way -- create a new vCPE object and set its subscriber_tenant instead")
+
+@property
+def subscriber(self):
+    if not self.subscriber_root:
+        return None
+    subs = CordSubscriberRoot.objects.filter(id=self.subscriber_root.id)
+    if not subs:
+        return None
+    return subs[0]
+
+def manage_vcpe(self):
+    # Each VOLT object owns exactly one VCPE object
+
+    if self.deleted:
+        return
+
+    # Check to see if the wrong s-tag is set. This can only happen if the
+    # user changed the s-tag after the VoltTenant object was created.
+    if self.vcpe and self.vcpe.instance:
+        s_tags = Tag.select_by_content_object(self.vcpe.instance).filter(name="s_tag")
+        if s_tags and (s_tags[0].value != str(self.s_tag)):
+            self.vcpe.delete()
+
+    if self.vcpe is None:
+        from services.vsg.models import VSGService, VSGTenant
+        vsgServices = VSGService.get_service_objects().all()
+        if not vsgServices:
+            raise XOSConfigurationError("No VSG Services available")
+
+        vcpe = VSGTenant(provider_service = vsgServices[0],
+                          subscriber_tenant = self)
+        vcpe.caller = self.creator
+        vcpe.save()
+
+def manage_subscriber(self):
+    if (self.subscriber_root is None):
+        # The vOLT is not connected to a Subscriber, so either find an
+        # existing subscriber with the same SSID, or autogenerate a new
+        # subscriber.
+        #
+        # TODO: This probably goes away when we rethink the ONOS-to-XOS
+        # vOLT API.
+
+        subs = CordSubscriberRoot.get_tenant_objects().filter(service_specific_id = self.service_specific_id)
+        if subs:
+            sub = subs[0]
+        else:
+            sub = CordSubscriberRoot(service_specific_id = self.service_specific_id,
+                                     name = "autogenerated-for-vOLT-%s" % self.id)
+            sub.save()
+        self.subscriber_root = sub
+        self.save()
+
+def cleanup_vcpe(self):
+    if self.vcpe:
+        # print "XXX cleanup vcpe", self.vcpe
+        self.vcpe.delete()
+
+def cleanup_orphans(self):
+    from services.vsg.models import VSGTenant
+    # ensure vOLT only has one vCPE
+    cur_vcpe = self.vcpe
+    for vcpe in list(self.get_subscribed_tenants(VSGTenant)):
+        if (not cur_vcpe) or (vcpe.id != cur_vcpe.id):
+            # print "XXX clean up orphaned vcpe", vcpe
+            vcpe.delete()
+
+def save(self, *args, **kwargs):
+    # VOLTTenant probably doesn't need a SSID anymore; that will be handled
+    # by CORDSubscriberRoot...
+    # self.validate_unique_service_specific_id()
+
+    if (self.subscriber_root is not None):
+        subs = self.subscriber_root.get_subscribed_tenants(VOLTTenant)
+        if (subs) and (self not in subs):
+            raise XOSDuplicateKey("Subscriber should only be linked to one vOLT")
+
+    if not self.creator:
+        if not getattr(self, "caller", None):
+            # caller must be set when creating a vCPE since it creates a slice
+            raise XOSProgrammingError("VOLTTenant's self.caller was not set")
+        self.creator = self.caller
+        if not self.creator:
+            raise XOSProgrammingError("VOLTTenant's self.creator was not set")
+
+    super(VOLTTenant, self).save(*args, **kwargs)
+    model_policy_volt(self.pk)
+
+def delete(self, *args, **kwargs):
+    self.cleanup_vcpe()
+    super(VOLTTenant, self).delete(*args, **kwargs)
+