[CORD-2826] Removing hardcoded dependencies

Change-Id: Ib04c11a805c48740184a8a60ce29e2425903742d
diff --git a/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py b/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
index e807441..0255d74 100644
--- a/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
@@ -48,18 +48,32 @@
             return link.provider_service_instance.leaf_model
         return None
 
-    def create_vsg(self, tenant):
-        vsgServices = VSGService.objects.all()
-        if not vsgServices:
-            raise XOSConfigurationError("No VSG Services available")
+    def create_eastbound_instance(self, si):
 
-        self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating vsg" % tenant)
+        chain = si.subscribed_links.all()
 
-        cur_vsg = VSGServiceInstance(owner=vsgServices[0])
-        cur_vsg.creator = tenant.creator
-        cur_vsg.save()
-        link = ServiceInstanceLink(provider_service_instance=cur_vsg, subscriber_service_instance=tenant)
-        link.save()
+        # Already has a chain
+        if len(chain) > 0 and not si.is_new:
+            self.logger.debug("MODEL_POLICY: Subscriber %s is already part of a chain" % si.id)
+            return
+
+        # if it does not have a chain,
+        # Find links to the next element in the service chain
+        # and create one
+
+        links = si.owner.subscribed_dependencies.all()
+
+        for link in links:
+            si_class = link.provider_service.get_service_instance_class_name()
+            self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating %s" % (si, si_class))
+
+            eastbound_si_class = model_accessor.get_model_class(si_class)
+            eastbound_si = eastbound_si_class()
+            eastbound_si.creator = si.creator
+            eastbound_si.owner_id = link.provider_service_id
+            eastbound_si.save()
+            link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
+            link.save()
 
     def manage_vsg(self, tenant):
         # Each VOLT object owns exactly one VCPE object
@@ -81,7 +95,7 @@
                 cur_vsg = None
 
         if cur_vsg is None:
-            self.create_vsg(tenant)
+            self.create_eastbound_instance(tenant)
 
     def cleanup_orphans(self, tenant):
         # ensure vOLT only has one vCPE
diff --git a/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
index a8dfaea..b0c867b 100644
--- a/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
+++ b/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
@@ -88,7 +88,7 @@
 
     def test_manage_vsg(self):
         with patch.object(VOLTServiceInstancePolicy, "get_current_vsg") as get_current_vsg, \
-                patch.object(VOLTServiceInstancePolicy, "create_vsg") as create_vsg, \
+                patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_vsg, \
                 patch.object(VSGService.objects, "get_items") as vsg_items:
 
             vsg_items.return_value = [self.vsg_service]
@@ -114,25 +114,34 @@
         self.assertEqual(vsg, None)
 
     def test_create_vsg(self):
-        with patch.object(VSGService.objects, "get_items") as vsg_items, \
-                patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
+        # with patch.object(model_accessor, "get_model_class") as mock_model_accessor, \
+        with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
                 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
 
-            vsg_items.return_value = [self.vsg_service]
-            self.policy.create_vsg(self.tenant)
+            # mock_model_accessor.return_value = VSGServiceInstance
+
+            link = Mock()
+            link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
+
+            si = Mock()
+            si.creator = 1
+            si.subscribed_links.all.return_value = []
+            si.owner.subscribed_dependencies.all.return_value = [link]
+
+            self.policy.create_eastbound_instance(si)
 
             # Should have created a vsg
 
             self.assertEqual(save_vsg.call_count, 1)
             vsg = save_vsg.call_args[0][0]
-            self.assertEqual(vsg.creator, self.tenant.creator)
+            self.assertEqual(vsg.creator, si.creator)
 
             # Should have created a link from OLT to vsg
 
             self.assertEqual(save_link.call_count, 1)
             link = save_link.call_args[0][0]
             self.assertEqual(link.provider_service_instance, vsg)
-            self.assertEqual(link.subscriber_service_instance, self.tenant)
+            self.assertEqual(link.subscriber_service_instance, si)
 
     def test_handle_delete(self):
         self.policy.handle_delete(self.tenant)
diff --git a/xos/synchronizer/volt_config.yaml b/xos/synchronizer/volt_config.yaml
index 0dc7e6f..12003cb 100644
--- a/xos/synchronizer/volt_config.yaml
+++ b/xos/synchronizer/volt_config.yaml
@@ -21,8 +21,6 @@
 required_models:
   - VOLTService
   - VOLTServiceInstance
-  - VSGService
-  - VSGServiceInstance
   - ServiceInstanceLink
 dependency_graph: "/opt/xos/synchronizers/volt/model-deps"
 model_policies_dir: "/opt/xos/synchronizers/volt/model_policies"