CORD-1250 Update to new Service/Tenancy models

Change-Id: I8edcc9880b69c9435733540c5a4841db616d1914
diff --git a/xos/admin.py b/xos/admin.py
index 84e0ecd..2cbbecb 100644
--- a/xos/admin.py
+++ b/xos/admin.py
@@ -11,7 +11,7 @@
 from django.utils import timezone
 from django.contrib.contenttypes import generic
 from suit.widgets import LinkedSelect
-from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline
+from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline
 from core.middleware import get_request
 
 from functools import update_wrapper
@@ -57,11 +57,10 @@
                           'classes':['suit-tab suit-tab-general']})]
     readonly_fields = ('backend_status_text', 'service_specific_attribute',)
     form = CordSubscriberRootForm
-    inlines = (TenantRootPrivilegeInline) # VOLTTenantInline
 
     suit_form_tabs =(('general', 'Cord Subscriber Root Details'),
 #        ('volttenants','VOLT Tenancy'),
-        ('tenantrootprivileges','Privileges')
+#        ('tenantrootprivileges','Privileges')
     )
 
     def get_queryset(self, request):
diff --git a/xos/attic/cordsubscriberroot_model.py b/xos/attic/cordsubscriberroot_model.py
index e4ee81f..8130ee6 100644
--- a/xos/attic/cordsubscriberroot_model.py
+++ b/xos/attic/cordsubscriberroot_model.py
@@ -14,6 +14,14 @@
                    "status")
 
 def __init__(self, *args, **kwargs):
+    # TODO: Should probably make an RCORDService, rather than filtering by name
+    rcord_services = Service.objects.filter(name="rcord")
+    if not rcord_services:
+        # this isn't going to end well...
+        raise Exception("There is no rcord service to use as the default service for CordSubscriberRoot")
+
+    self._meta.get_field("owner").default = rcord_services[0].id
+
     super(CordSubscriberRoot, self).__init__(*args, **kwargs)
 
 def find_device(self, mac):
@@ -87,26 +95,32 @@
     pass
 
 def invalidate_related_objects(self):
-    # Dirty all vSGs related to this subscriber, so the vSG synchronizer

-    # will run.

-

-    # TODO: This should be reimplemented to use a watcher instead.

-

-    # NOTE: "vOLT" and "vCPE" are hardcoded below. They had better agree

-    # with the kinds defined in the vOLT and vCPE models.

-

-    for tenant in self.subscribed_tenants.all():

-        if tenant.kind == "vOLT":

-            for inner_tenant in tenant.subscribed_tenants.all():

-                if inner_tenant.kind == "vCPE":

-                    inner_tenant.save()

-

-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)

-

+    # Dirty all vSGs related to this subscriber, so the vSG synchronizer
+    # will run.
+
+    # TODO: This should be reimplemented to use a watcher instead.
+
+    # TODO: Hardcoded service dependency
+
+    from services.volt.models import VOLTTenant
+    from services.vsg.models import VSGTenant
+
+    for link in self.subscribed_links.all():
+        # cast from base class to derived class
+        volts = VOLTTenant.objects.filter(serviceinstance_ptr = link.provider_service_instance)
+        for volt in volts:
+            for inner_link in volt.subscribed_links.all():
+                # cast from base class to derived class
+                vsgs = VSGTenant.objects.filter(serviceinstance_ptr = inner_link.provider_service_instance)
+                for vsg in vsgs:
+                    vsg.save()
+
+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)
+
     self.invalidate_related_objects()
 
diff --git a/xos/attic/header.py b/xos/attic/header.py
index 4a13893..d630d68 100644
--- a/xos/attic/header.py
+++ b/xos/attic/header.py
@@ -1,6 +1,6 @@
 from django.db import models
 from django.db.models import *
-from core.models import Service, XOSBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, TenantRoot, NetworkParameter, NetworkParameterType, Port, AddressPool, User
+from core.models import Service, ServiceInstance, XOSBase, Slice, Instance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, User
 from core.models.xosbase import StrippedCharField
 import os
 from django.db import models, transaction
diff --git a/xos/rcord.xproto b/xos/rcord.xproto
index f2e6ac8..1257ace 100644
--- a/xos/rcord.xproto
+++ b/xos/rcord.xproto
@@ -1,7 +1,7 @@
 option name = "rcord";
 option verbose_name = "RCORD Profile";
 
-message CordSubscriberRoot (TenantRoot){
+message CordSubscriberRoot (ServiceInstance) {
      option kind = "CordSubscriberRoot";
 
      required bool firewall_enable = 1 [default = False, null = False, db_index = False, blank = True];