[CORD-3008] Before removing OLT check that no subscribers are using it

Change-Id: I7f3d0f9d552a74a05c3f447cbb35b75f29595c87
diff --git a/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py b/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
index f4359ed..b33bf1f 100644
--- a/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_voltserviceinstance.py
@@ -14,39 +14,29 @@
 # limitations under the License.
 
 
-from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, model_accessor
+from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, ONUDevice, ServiceInstance, model_accessor
 from synchronizers.new_base.policy import Policy
 
 class VOLTServiceInstancePolicy(Policy):
     model_name = "VOLTServiceInstance"
 
-    def handle_create(self, tenant):
-        return self.handle_update(tenant)
+    def handle_create(self, si):
+        return self.handle_update(si)
 
-    def handle_update(self, tenant):
+    def handle_update(self, si):
 
-        if (tenant.link_deleted_count > 0) and (not tenant.provided_links.exists()):
+        if (si.link_deleted_count > 0) and (not si.provided_links.exists()):
             # If this instance has no links pointing to it, delete
-            self.handle_delete(tenant)
-            if VOLTServiceInstance.objects.filter(id=tenant.id).exists():
-                tenant.delete()
+            self.handle_delete(si)
+            if VOLTServiceInstance.objects.filter(id=si.id).exists():
+                si.delete()
             return
 
-        self.manage_vsg(tenant)
-        self.cleanup_orphans(tenant)
+        self.create_eastbound_instance(si)
+        self.associate_onu_device(si)
 
-    def handle_delete(self, tenant):
+    def handle_delete(self, si):
         pass
-        # assume this is handled by ServiceInstanceLink being deleted
-        #if tenant.vcpe:
-        #    tenant.vcpe.delete()
-
-    def get_current_vsg(self, tenant):
-        for link in ServiceInstanceLink.objects.filter(subscriber_service_instance_id = tenant.id):
-            # NOTE: Assumes the first (and only?) link is to a vsg
-            # cast from ServiceInstance to VSGTenant
-            return link.provider_service_instance.leaf_model
-        return None
 
     def create_eastbound_instance(self, si):
 
@@ -75,33 +65,21 @@
             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
+    def associate_onu_device(self, si):
 
-        if tenant.deleted:
-            self.logger.info("MODEL_POLICY: VOLTServiceInstance %s deleted, deleting vsg" % tenant)
-            return
+        self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id)
 
-        cur_vsg = self.get_current_vsg(tenant)
+        base_si = ServiceInstance.objects.get(id=si.id)
+        try:
+            onu_device_serial_number = base_si.get_westbound_service_instance_properties("onu_device")
+        except Exception as e:
+            raise Exception(
+                "VOLTServiceInstance %s has no westbound ServiceInstance specifying the onu_device, you need to manually specify it" % self.id)
 
-        # Check to see if the wrong s-tag is set. This can only happen if the
-        # user changed the s-tag after the VOLTServiceInstance object was created.
-        if cur_vsg and hasattr(cur_vsg, 'instance') and cur_vsg.instance:
-            s_tags = Tag.objects.filter(content_type=cur_vsg.instance.self_content_type_id,
-                                        object_id=cur_vsg.instance.id, name="s_tag")
-            if s_tags and (s_tags[0].value != str(tenant.s_tag)):
-                self.logger.info("MODEL_POLICY: VOLTServiceInstance %s s_tag changed, deleting vsg" % tenant)
-                cur_vsg.delete()
-                cur_vsg = None
+        try:
+            onu = ONUDevice.objects.get(serial_number=onu_device_serial_number)
+        except IndexError:
+            raise Exception("ONUDevice with serial number %s can't be found" % onu_device_serial_number)
 
-        if cur_vsg is None:
-            self.create_eastbound_instance(tenant)
-
-    def cleanup_orphans(self, tenant):
-        # ensure vOLT only has one vCPE
-        cur_vsg = self.get_current_vsg(tenant)
-
-        links = tenant.subscribed_links.all()
-        for link in links:
-            if (link.provider_service_instance_id != cur_vsg.id):
-                link.delete()
+        si.onu_device_id = onu.id
+        si.save()
diff --git a/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
index 5488b15..20835a0 100644
--- a/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
+++ b/xos/synchronizer/model_policies/test_model_policy_voltserviceinstance.py
@@ -71,53 +71,22 @@
         model_accessor.reset_all_object_stores()
 
         self.policy = VOLTServiceInstancePolicy()
-        self.tenant = VOLTServiceInstance(s_tag=111, c_tag=222, service_specific_id=1234)
-
-        self.vsg_service = VSGService(name="the vsg service")
+        self.si = Mock()
 
     def tearDown(self):
         sys.path = self.sys_path_save
 
     def test_handle_create(self):
-        with patch.object(VOLTServiceInstancePolicy, "manage_vsg") as manage_vsg, \
-                patch.object(VOLTServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans:
-            self.policy.handle_create(self.tenant)
-            manage_vsg.assert_called_with(self.tenant)
-            cleanup_orphans.assert_called_with(self.tenant)
+        with patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_eastbound_instance, \
+            patch.object(VOLTServiceInstancePolicy, "associate_onu_device") as associate_onu_device:
 
-    def test_manage_vsg(self):
-        with patch.object(VOLTServiceInstancePolicy, "get_current_vsg") as get_current_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]
-            get_current_vsg.return_value = None
-            self.policy.manage_vsg(self.tenant)
-
-            create_vsg.assert_called()
-
-    def test_get_current_vsg(self):
-        with patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
-            vsg = VSGServiceInstance()
-            link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id)
-
-            link_items.return_value = [link]
-
-            vsg = self.policy.get_current_vsg(self.tenant)
-
-            self.assertNotEqual(vsg, None)
-
-    def test_get_current_vsg_noexist(self):
-        vsg = self.policy.get_current_vsg(self.tenant)
-
-        self.assertEqual(vsg, None)
+            self.policy.handle_create(self.si)
+            create_eastbound_instance.assert_called_with(self.si)
+            associate_onu_device.assert_called_with(self.si)
 
     def test_create_vsg(self):
-        # 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:
-
-            # mock_model_accessor.return_value = VSGServiceInstance
+            patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
 
             link = Mock()
             link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
@@ -140,34 +109,27 @@
             self.assertEqual(link.provider_service_instance, vsg)
             self.assertEqual(link.subscriber_service_instance, si)
 
+    def test_associate_onu(self):
+        with patch.object(ServiceInstance.objects, "get") as get_si, \
+            patch.object(ONUDevice.objects, "get") as get_onu:
+
+            mock_si = Mock()
+            mock_si.get_westbound_service_instance_properties.return_value = "BRCM1234"
+            get_si.return_value = mock_si
+
+            mock_onu = Mock()
+            mock_onu.id = 12
+            get_onu.return_value = mock_onu
+
+            self.policy.associate_onu_device(self.si)
+
+            self.assertEqual(self.si.onu_device_id, mock_onu.id)
+            self.si.save.assert_called()
+
     def test_handle_delete(self):
-        self.policy.handle_delete(self.tenant)
+        self.policy.handle_delete(self.si)
         # handle delete does nothing, and should trivially succeed
 
-    def test_cleanup_orphans(self):
-        with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \
-                patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \
-                patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
-
-            vsg1 = VSGServiceInstance(id=123)
-            vsg2 = VSGServiceInstance(id=456)
-            link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id,
-                                        subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
-            link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id,
-                                        subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
-
-            self.tenant.subscribed_links=MockObjectList(initial=[link1,link2])
-
-            vsg_si_items.return_value = [vsg1, vsg2]
-            link_items.return_value = [link1, link2]
-
-            self.policy.cleanup_orphans(self.tenant)
-
-            # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be
-            # deleted.
-
-            self.assertEqual(delete_link.call_count, 1)
-
 if __name__ == '__main__':
     unittest.main()