[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()
 
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
index 9431488..ed1e062 100644
--- a/xos/synchronizer/models/models.py
+++ b/xos/synchronizer/models/models.py
@@ -14,7 +14,6 @@
 
 import random
 
-from core.models.xosbase import *
 from xos.exceptions import XOSValidationError
 
 from models_decl import VOLTService_decl
@@ -36,16 +35,44 @@
         except IndexError, e:
             return False
 
-
 class VOLTServiceInstance(VOLTServiceInstance_decl):
     class Meta:
-        proxy = True 
-
+        proxy = True
 
 class OLTDevice(OLTDevice_decl):
     class Meta:
-        proxy = True 
+        proxy = True
 
+    def get_volt_si(self):
+        return VOLTServiceInstance.objects.all()
+
+    def delete(self, *args, **kwargs):
+
+        onus = []
+        pon_ports = self.pon_ports.all()
+        for port in pon_ports:
+            onus = onus + list(port.onu_devices.all())
+
+
+        if len(onus) > 0:
+            onus = [o.id for o in onus]
+
+            # find the ONUs used by VOLTServiceInstances
+            used_onus = [o.onu_device_id for o in self.get_volt_si()]
+
+            # find the intersection between the onus associated with this OLT and the used one
+            used_onus_to_delete = [o for o in onus if o in used_onus]
+
+            if len(used_onus_to_delete) > 0:
+                if hasattr(self, "device_id") and self.device_id:
+                    item = self.device_id
+                elif hasattr(self, "name") and self.name:
+                    item = self.name
+                else:
+                    item = self.id
+                raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
+
+        super(OLTDevice, self).delete(*args, **kwargs)
 
 class PONPort(PONPort_decl):
     class Meta:
diff --git a/xos/synchronizer/models/test_oltdevice_model.py b/xos/synchronizer/models/test_oltdevice_model.py
new file mode 100644
index 0000000..34bf200
--- /dev/null
+++ b/xos/synchronizer/models/test_oltdevice_model.py
@@ -0,0 +1,77 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+from mock import patch, Mock, MagicMock
+
+# mocking XOS exception, as they're based in Django
+class Exceptions:
+    XOSValidationError = Exception
+
+class XOS:
+    exceptions = Exceptions
+
+class TestOLTDeviceModel(unittest.TestCase):
+    def setUp(self):
+        self.xos = XOS
+
+        self.models_decl = Mock()
+        self.models_decl.OLTDevice_decl = MagicMock
+        self.models_decl.OLTDevice_decl.delete = Mock()
+
+        modules = {
+            'xos.exceptions': self.xos.exceptions,
+            'models_decl': self.models_decl,
+        }
+
+        self.module_patcher = patch.dict('sys.modules', modules)
+        self.module_patcher.start()
+
+        from models import OLTDevice
+
+        print OLTDevice
+
+        self.olt_device = OLTDevice()
+        self.olt_device.id = None # this is a new model
+        self.olt_device.is_new = True
+        self.olt_device.device_id = 1234
+
+
+    def test_delete(self):
+        self.olt_device.delete()
+        self.models_decl.OLTDevice_decl.delete.assert_called()
+
+    def test_prevent_delete(self):
+
+        onu1 = Mock()
+        onu1.id = 1
+
+        pon1 = Mock()
+        pon1.onu_devices.all.return_value = [onu1]
+
+        self.olt_device.pon_ports.all.return_value = [pon1]
+
+        volt_si_1 = Mock()
+        volt_si_1.onu_device_id = onu1.id
+
+        with patch.object(self.olt_device, "get_volt_si")as volt_si_get:
+            volt_si_get.return_value = [volt_si_1]
+            with self.assertRaises(Exception) as e:
+                self.olt_device.delete()
+
+            self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs')
+            self.models_decl.OLTDevice_decl.delete.assert_not_called()
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/models/volt.xproto b/xos/synchronizer/models/volt.xproto
index 0eda4e0..39383f4 100644
--- a/xos/synchronizer/models/volt.xproto
+++ b/xos/synchronizer/models/volt.xproto
@@ -16,14 +16,6 @@
     required string onos_voltha_pass = 8 [help_text = "The ONOS Voltha password. By default rocks", max_length = 254, default="rocks", null = True, db_index = False, blank = False];
 }
 
-message VOLTServiceInstance (ServiceInstance){
-    option kind = "vOLT";
-    option owner_class_name = "VOLTService";
-    option verbose_name = "vOLT Service Instance";
-
-    optional string description = 1 [max_length = 254, null = True, db_index = False, blank = True];
-}
-
 message OLTDevice (XOSBase){
     option verbose_name = "OLT Device";
     option description="Represents a physical OLT device";
@@ -83,4 +75,13 @@
     optional string admin_state = 6 [help_text = "admin_state", null = True, db_index = False, blank = False, feedback_state = True];
     optional string oper_status = 7 [help_text = "oper_status", null = True, db_index = False, blank = False, feedback_state = True];
     optional string connect_status = 8 [help_text = "connect_status", null = True, db_index = False, blank = False, feedback_state = True];
+}
+
+message VOLTServiceInstance (ServiceInstance){
+    option kind = "vOLT";
+    option owner_class_name = "VOLTService";
+    option verbose_name = "vOLT Service Instance";
+
+    optional string description = 1 [max_length = 254, null = True, db_index = False, blank = True];
+    optional manytoone onu_device->ONUDevice:volt_service_instances = 1 [db_index = True, null = True, blank = False];
 }
\ No newline at end of file
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
index 123cb69..426392e 100644
--- a/xos/unittest.cfg
+++ b/xos/unittest.cfg
@@ -5,3 +5,4 @@
                  steps
                  pull_steps
                  event_steps
+                 models