Watching whitelist changes to update SI
Change-Id: I6ee5d6f1dc380708cbe4795c2685fbef58ae6a65
diff --git a/xos/synchronizer/model_policies/model_policy_hippieossservice.py b/xos/synchronizer/model_policies/model_policy_hippieossservice.py
new file mode 100644
index 0000000..3559d88
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_hippieossservice.py
@@ -0,0 +1,43 @@
+
+# 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.
+
+
+from synchronizers.new_base.modelaccessor import HippieOSSServiceInstance, model_accessor
+from synchronizers.new_base.policy import Policy
+
+class OSSServicePolicy(Policy):
+ model_name = "HippieOSSService"
+
+ def handle_update(self, service):
+ self.logger.debug("MODEL_POLICY: handle_update for HippieOSSService", oss=service)
+
+ sis = HippieOSSServiceInstance.objects.all()
+
+ whitelist = [x.strip() for x in service.whitelist.split(',')]
+
+ for si in sis:
+ if si.serial_number in whitelist and not si.valid == "valid":
+ self.logger.debug("MODEL_POLICY: activating HippieOSSServiceInstance because of change in the whitelist", si=si)
+ si.valid = "valid"
+ si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True)
+ if si.serial_number not in whitelist and not si.valid == "invalid":
+ self.logger.debug(
+ "MODEL_POLICY: disabling HippieOSSServiceInstance because of change in the whitelist", si=si)
+ si.valid = "invalid"
+ si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True)
+
+
+ def handle_delete(self, si):
+ pass
diff --git a/xos/synchronizer/model_policies/test_model_policy_hippieossservice.py b/xos/synchronizer/model_policies/test_model_policy_hippieossservice.py
new file mode 100644
index 0000000..7c5a598
--- /dev/null
+++ b/xos/synchronizer/model_policies/test_model_policy_hippieossservice.py
@@ -0,0 +1,125 @@
+
+# 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, call, Mock, PropertyMock
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+service_dir=os.path.join(test_path, "../../../..")
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+ xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+ services_dir=os.path.join(xos_dir, "../../xos_services")
+
+def get_models_fn(service_name, xproto_name):
+ name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+ if os.path.exists(os.path.join(services_dir, name)):
+ return name
+ raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+
+class TestModelPolicyHippieOssService(unittest.TestCase):
+ def setUp(self):
+
+ self.sys_path_save = sys.path
+ sys.path.append(xos_dir)
+ sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+ config = os.path.join(test_path, "../test_config.yaml")
+ from xosconfig import Config
+ Config.clear()
+ Config.init(config, 'synchronizer-config-schema.yaml')
+
+ from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+ build_mock_modelaccessor(xos_dir, services_dir, [
+ get_models_fn("hippie-oss", "hippie-oss.xproto"),
+ get_models_fn("olt-service", "volt.xproto"),
+ get_models_fn("../profiles/rcord", "rcord.xproto")
+ ])
+
+ import synchronizers.new_base.modelaccessor
+ from model_policy_hippieossservice import OSSServicePolicy, model_accessor
+
+ from mock_modelaccessor import MockObjectList
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
+ # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
+ model_accessor.reset_all_object_stores()
+
+ self.policy = OSSServicePolicy()
+
+ self.service = HippieOSSService(
+ whitelist="BRCM111, BRCM222"
+ )
+
+ # needs to be enabled
+ self.si1 = HippieOSSServiceInstance(
+ valid="awaiting",
+ serial_number="BRCM111"
+ )
+
+ # needs to be enabled
+ self.si2 = HippieOSSServiceInstance(
+ valid="invalid",
+ serial_number="BRCM222"
+ )
+
+ # remains disabled
+ self.si3 = HippieOSSServiceInstance(
+ valid="invalid",
+ serial_number="BRCM333"
+ )
+
+ # needs to be disabled
+ self.si4 = HippieOSSServiceInstance(
+ valid="valid",
+ serial_number="BRCM444"
+ )
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+ self.service = None
+
+ def test_whitelist_update(self):
+ """
+ When the whitelist is updated, check for added ONU to be enabled and for removed ONU to be disabled
+ """
+ with patch.object(HippieOSSServiceInstance.objects, "get_items") as oss_si, \
+ patch.object(self.si1, "save") as si1_save, \
+ patch.object(self.si2, "save") as si2_save, \
+ patch.object(self.si3, "save") as si3_save, \
+ patch.object(self.si4, "save") as si4_save:
+ oss_si.return_value = [self.si1, self.si2, self.si3, self.si4]
+
+ self.policy.handle_update(self.service)
+
+ self.si1.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
+ self.assertEqual(self.si1.valid, "valid")
+ self.si2.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
+ self.assertEqual(self.si2.valid, "valid")
+ self.si3.save.assert_not_called()
+ self.assertEqual(self.si3.valid, "invalid")
+ self.si4.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
+ self.assertEqual(self.si4.valid, "invalid")
+
+if __name__ == '__main__':
+ unittest.main()
+