[CORD-3008] Before removing OLT check that no subscribers are using it
Change-Id: I7f3d0f9d552a74a05c3f447cbb35b75f29595c87
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