CORD-1520 Update to new Service/Tenancy models

Change-Id: I963add8252c895dd02f918d71c566768a3df1eaf
diff --git a/xos/admin.py b/xos/admin.py
index 4d86e92..312faaa 100644
--- a/xos/admin.py
+++ b/xos/admin.py
@@ -11,7 +11,7 @@
 from django.contrib.contenttypes import generic
 from suit.widgets import LinkedSelect
 from core.models import AddressPool
-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
diff --git a/xos/synchronizer/steps/sync_vroutertenant.py b/xos/synchronizer/steps/sync_vroutertenant.py
index fe3fae0..5f9c1db 100644
--- a/xos/synchronizer/steps/sync_vroutertenant.py
+++ b/xos/synchronizer/steps/sync_vroutertenant.py
@@ -13,14 +13,16 @@
     playbook='sync_host.yaml'
 
     def get_fabric_onos_service(self):
-        fos = None
         fs = FabricService.objects.first()
-        if fs.subscribed_tenants.exists():
-            app = fs.subscribed_tenants.first()
-            if app.provider_service:
-                ps = app.provider_service
-                fos = ONOSService.objects.filter(id=ps.id)[0]
-        return fos
+        for link in fs.subscribed_links.all():
+            if link.provider_service_instance:
+                # cast from ServiceInstance to ONOSApp
+                apps = ONOSApp.objects.filter(id=link.provider_service_instance.id)
+                if apps:
+                    # cast from Service to ONOSService
+                    onos = ONOSService.objects.get(id=apps[0].owner.id)
+                    return onos
+        return None
 
     def get_node_tag(self, node, tagname):
         tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
@@ -47,16 +49,11 @@
 
         # Check that each is a valid vCPE tenant or instance
         for vroutertenant in objs:
-            # Do we have a vCPE subscriber_tenant?
-            if vroutertenant.subscriber_tenant:
-                sub = vroutertenant.subscriber_tenant
-                if sub.kind != 'vCPE':
+            # TODO: hardcoded service dependency
+            vsg = self.get_vsg_subscriber(vroutertenant)
+            if vsg:
+                if not vsg.instance:
                     objs.remove(vroutertenant)
-                else:
-                    # coerce the subscriber tenant over to the VSGTenant
-                    vsg = VSGTenant.objects.filter(id=sub.id).first()
-                    if not vsg.instance:
-                        objs.remove(vroutertenant)
             else:
                 # Maybe the VRouterTenant is for an instance
                 # TODO: tenant_for_instance_id needs to be a real database field
@@ -70,10 +67,24 @@
 
         return objs
 
+    def get_vsg_subscriber(self, vroutertenant):
+        links = vroutertenant.provided_links.all()
+        for link in links:
+            if not link.subscriber_service_instance:
+                continue
+            # cast from ServiceInstance to VSGTEnant
+            vsgs = VSGTenant.objects.filter(id=link.subscriber_service_instance.id)
+            if vsgs:
+                return vsgs[0]
+        return None
+
     def map_sync_inputs(self, vroutertenant):
 
         fos = self.get_fabric_onos_service()
 
+        if not fos:
+            raise Exception("No fabric onos service")
+
         name = None
         instance = None
         # VRouterTenant setup is kind of hacky right now, we'll
@@ -81,12 +92,12 @@
         # * Look up the instance corresponding to the address
         # * Look up the node running the instance
         # * Get the "location" tag, push to the fabric
-        if vroutertenant.subscriber_tenant:
-            sub = vroutertenant.subscriber_tenant
-            assert(sub.kind == 'vCPE')
-            vsg = VSGTenant.objects.filter(id=sub.id).first()
+
+        # TODO: hardcoded service dependency
+        vsg = self.get_vsg_subscriber(vroutertenant)
+        if vsg:
             instance = vsg.instance
-            name = str(sub)
+            name = str(vsg)
         else:
             instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
             instance = Instance.objects.filter(id=instance_id)[0]