[CORD-3092][CORD-3042] Autogenerate c_tag for subscriber

Change-Id: I2f8eaf3fcd3ef1148e0070c307177c60ee79d6a1
diff --git a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
index c64ff1d..14e79b5 100644
--- a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
@@ -38,7 +38,6 @@
         links = si.owner.subscribed_dependencies.all()
 
         for link in links:
-            ps = link.provider_service.leaf_model
             si_class = link.provider_service.get_service_instance_class_name()
             self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class))
 
diff --git a/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py b/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
index c880e2f..85d89c9 100644
--- a/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
@@ -14,104 +14,89 @@
 
 
 import unittest
-from mock import patch, MagicMock
-import mock
-from xosconfig import Config
+from mock import patch, Mock
+
 
 import os, sys
 
-cwd=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-xos_dir=os.path.abspath(os.path.join(cwd, "../../../../../../orchestration/xos/xos"))
-services_dir=os.path.join(xos_dir, "../../xos_services")
-config_file = os.path.join(cwd, "test_config.yaml")
+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")
 
-# NOTE this have to start for xos_services
-RCORD_XPROTO = "../profiles/rcord/xos/synchronizer/models/rcord.xproto"
-
-Config.clear()
-Config.init(config_file, 'synchronizer-config-schema.yaml')
-
-# FIXME move the synchronizer framework into a library
-sys.path.append(xos_dir)
-from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-
-class MockSubscriber:
-
-    def __init__(self, *args, **kwargs):
-        self.name = kwargs['name']
-        self.is_new = True
-        self.id = 1
-
-    def subscribed_links(self):
-        pass
-
-class MockAll:
-    @staticmethod
-    def all():
-        pass
-
-class MockService:
-
-    def __init__(self):
-        self.subscribed_dependencies = MockAll
-
-
-class MockModel:
-    def __init__(self):
-        self.model_name = "VOLTService"
-
-class MockLeaf:
-    def __init__(self):
-        self.leaf_model = MockModel()
-
-class MockLink:
-    def __init__(self):
-        self.provider_service = MockLeaf()
+# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
+# both possible locations...
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        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 TestModelPolicyRCORDSubscriber(unittest.TestCase):
     def setUp(self):
-        global model_accessor
 
-        self.original_sys_path = sys.path
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
 
-        # Generate a fake model accessor (emulate the client library)
-        build_mock_modelaccessor(xos_dir, services_dir, [RCORD_XPROTO])
+        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("../profiles/rcord", "rcord.xproto"),
+            get_models_fn("olt-service", "volt.xproto") # in test create we spy on VOLTServiceInstance
+        ])
 
         import synchronizers.new_base.modelaccessor
-        from synchronizers.new_base.modelaccessor import model_accessor
-        from model_policy_rcordsubscriber import RCORDSubscriberPolicy
+        from model_policy_rcordsubscriber import RCORDSubscriberPolicy, 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
 
-        # model_accessor.reset_all_object_stores()
+        # 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()
 
-        # create base objects for testing
         self.policy = RCORDSubscriberPolicy()
+        self.si = Mock(name="myTestSubscriber")
 
     def tearDown(self):
-        sys.path = self.original_sys_path
+        sys.path = self.sys_path_save
 
-    @patch.object(MockSubscriber, 'subscribed_links', MockAll)
-    @patch.object(MockAll, 'all', MagicMock(return_value=['foo']))
     def test_update_and_do_nothing(self):
-        si = MockSubscriber(name="myTestSubscriber")
+        si = self.si
         si.is_new = False
+        si.subscribed_links.all.return_value = ["already", "have", "a", "chain"]
         self.policy.handle_create(si)
         # NOTE assert that no models are created
 
-    @patch.object(MockSubscriber, 'subscribed_links', MockAll)
-    @patch.object(MockAll, 'all', MagicMock(return_value=[MockLink]))
     def test_create(self):
-        si = MockSubscriber(name="myTestSubscriber")
-        owner = MockService()
+        volt = Mock()
+        volt.get_service_instance_class_name.return_value = "VOLTServiceInstance"
 
-        si.owner = owner
+        service_dependency = Mock()
+        service_dependency.provider_service = volt
 
-        with patch.object(owner.subscribed_dependencies, 'all', MagicMock(return_value=[MockLink()])), \
-             patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
+        si = self.si
+        si.is_new = True
+        si.subscribed_links.all.return_value = []
+        si.owner.subscribed_dependencies.all.return_value = [service_dependency]
+
+        with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
              patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
+
             self.policy.handle_create(si)
             self.assertEqual(save_link.call_count, 1)
             self.assertEqual(save_volt.call_count, 1)
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
index dd6c57f..34aff50 100644
--- a/xos/synchronizer/models/models.py
+++ b/xos/synchronizer/models/models.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 import re
 import socket
+import random
 
 from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied
 from models_decl import RCORDService_decl, RCORDSubscriber_decl
@@ -40,7 +41,19 @@
                 inner_service_instance = link.provider_service_instance
                 inner_service_instance.save(update_fields=["updated"])
 
+    def generate_tag(self):
+        # NOTE this method will loop if available c_tags are ended
+        tag = random.randint(16, 4096)
+        if tag in self.get_used_c_tags():
+            return self.generate_tag()
+        return tag
+
+    def get_used_c_tags(self):
+        same_onu_subscribers = RCORDSubscriber.objects.filter(onu_device=self.onu_device)
+        return [s.c_tag for s in same_onu_subscribers]
+
     def save(self, *args, **kwargs):
+
         self.validate_unique_service_specific_id(none_okay=True)
 
         # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
@@ -67,9 +80,18 @@
             if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()):
                 raise XOSValidationError("The mac_address you specified (%s) is not valid" % self.mac_address)
 
+        # validate c_tag
+        if hasattr(self, 'c_tag') and self.c_tag is not None:
+            if self.c_tag in self.get_used_c_tags():
+                raise XOSValidationError("The c_tag you specified (%s) has already been used on device %s" % (self.c_tag, self.onu_device))
+
+        if not hasattr(self, "c_tag") or self.c_tag is None:
+            self.c_tag = self.generate_tag()
+
         self.set_owner()
 
         if hasattr(self.owner.leaf_model, "access") and self.owner.leaf_model.access == "voltha":
+
             # if the access network is managed by voltha, validate that onu_device actually exist
             volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService
 
diff --git a/xos/synchronizer/models/rcord.xproto b/xos/synchronizer/models/rcord.xproto
index 122b80c..9b4deb2 100644
--- a/xos/synchronizer/models/rcord.xproto
+++ b/xos/synchronizer/models/rcord.xproto
@@ -12,21 +12,23 @@
     option verbose_name = "RCORD Subscriber";
     option owner_class_name = "RCORDService";
 
-    required bool firewall_enable = 1 [default = False, null = False, db_index = False, blank = True];
-    optional string firewall_rules = 2 [default = "accept all anywhere anywhere", null = True, db_index = False, blank = True];
-    required bool url_filter_enable = 3 [default = False, null = False, db_index = False, blank = True];
-    optional string url_filter_rules = 4 [default = "allow all", null = True, db_index = False, blank = True];
-    required string url_filter_level = 5 [default = "PG", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
-    required bool cdn_enable = 6 [default = False, null = False, db_index = False, blank = True];
-    required bool is_demo_user = 7 [default = False, null = False, db_index = False, blank = True];
-    required int32 uplink_speed = 8 [default = 1000000000, null = False, db_index = False, blank = False];
-    required int32 downlink_speed = 9 [default = 1000000000, null = False, db_index = False, blank = False];
-    required bool enable_uverse = 10 [default = True, null = False, db_index = False, blank = True];
-    required string status = 11 [default = "enabled", choices = "(('enabled', 'Enabled'), ('suspended', 'Suspended'), ('delinquent', 'Delinquent'), ('copyrightviolation', 'Copyright Violation'))", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
-    optional int32 c_tag = 12 [null = True, db_index = False, blank = False];
-    required string onu_device = 13 [help_text = "ONUDevice serial number", null = False, db_index = False, blank = False, unique = True];
+    // vsg related configurations
     optional manytoone creator->User:created_rcord_subscribers = 15 [db_index = True, null = True, blank = True];
+    optional bool firewall_enable = 1 [default = False, null = False, db_index = False, blank = True];
+    optional string firewall_rules = 2 [default = "accept all anywhere anywhere", null = True, db_index = False, blank = True];
+    optional bool url_filter_enable = 3 [default = False, null = False, db_index = False, blank = True];
+    optional string url_filter_rules = 4 [default = "allow all", null = True, db_index = False, blank = True];
+    optional string url_filter_level = 5 [default = "PG", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
+    optional bool cdn_enable = 6 [default = False, null = False, db_index = False, blank = True];
+    optional bool is_demo_user = 7 [default = False, null = False, db_index = False, blank = True];
+    optional int32 uplink_speed = 8 [default = 1000000000, null = False, db_index = False, blank = False];
+    optional int32 downlink_speed = 9 [default = 1000000000, null = False, db_index = False, blank = False];
+    optional bool enable_uverse = 10 [default = True, null = False, db_index = False, blank = True];
+    optional string status = 11 [default = "enabled", choices = "(('enabled', 'Enabled'), ('suspended', 'Suspended'), ('delinquent', 'Delinquent'), ('copyrightviolation', 'Copyright Violation'))", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
 
+    // parameters for r-cord lite
+    optional int32 c_tag = 12 [null = True, db_index = False, blank = False];
+    required string onu_device = 13 [help_text = "ONUDevice serial number", null = False, db_index = False, blank = False];
     optional int32 uni_port_id = 16 [help_text = "UNI PORT ID in VOLTHA", null = True, db_index = False, blank = False];
     optional string ip_address = 17 [help_text = "Subscriber IP Address", null = True, db_index = False, blank = False];
     optional string mac_address = 18 [null = True, db_index = False, blank = False];
diff --git a/xos/synchronizer/models/test_models.py b/xos/synchronizer/models/test_models.py
new file mode 100644
index 0000000..bb2a469
--- /dev/null
+++ b/xos/synchronizer/models/test_models.py
@@ -0,0 +1,128 @@
+# 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
+    XOSProgrammingError = Exception
+    XOSPermissionDenied = Exception
+
+class XOS:
+    exceptions = Exceptions
+
+class TestRCORDModels(unittest.TestCase):
+    def setUp(self):
+        self.xos = XOS
+
+        self.models_decl = Mock()
+        self.models_decl.RCORDSubscriber_decl = MagicMock
+        self.models_decl.RCORDSubscriber_decl.save = Mock()
+        self.models_decl.RCORDSubscriber_decl.objects = Mock()
+        self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = []
+
+
+        modules = {
+            'xos.exceptions': self.xos.exceptions,
+            'models_decl': self.models_decl
+        }
+
+        self.module_patcher = patch.dict('sys.modules', modules)
+        self.module_patcher.start()
+
+        self.volt = Mock()
+
+        from models import RCORDSubscriber
+
+        self.rcord_subscriber_class = RCORDSubscriber
+
+        self.rcord_subscriber = RCORDSubscriber()
+        self.rcord_subscriber.onu_device = "BRCM1234"
+        self.rcord_subscriber.c_tag = "111"
+        self.rcord_subscriber.ip_address = "1.1.1.1"
+        self.rcord_subscriber.mac_address = "00:AA:00:00:00:01"
+        self.rcord_subscriber.owner.leaf_model.access = "voltha"
+        self.rcord_subscriber.owner.provider_services = [self.volt]
+
+
+    def test_save(self):
+        self.rcord_subscriber.save()
+        self.models_decl.RCORDSubscriber_decl.save.assert_called()
+
+    def test_validate_ip_address(self):
+        self.rcord_subscriber.ip_address = "invalid"
+        with self.assertRaises(Exception) as e:
+            self.rcord_subscriber.save()
+
+        self.assertEqual(e.exception.message, "The ip_address you specified (invalid) is not valid")
+        self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
+
+    def test_validate_mac_address(self):
+        self.rcord_subscriber.mac_address = "invalid"
+        with self.assertRaises(Exception) as e:
+            self.rcord_subscriber.save()
+
+        self.assertEqual(e.exception.message, "The mac_address you specified (invalid) is not valid")
+        self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
+
+    def test_valid_onu_device(self):
+        self.rcord_subscriber.save()
+        self.models_decl.RCORDSubscriber_decl.save.assert_called()
+
+    def test_invalid_onu_device(self):
+        self.volt.leaf_model.has_access_device.return_value = False
+        with self.assertRaises(Exception) as e:
+            self.rcord_subscriber.save()
+
+        self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists")
+        self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
+
+    def test_validate_c_tag(self):
+        """
+        check that other subscriber attached to the same ONU don't have the same c_tag
+        """
+
+        s = Mock()
+        s.c_tag = "111"
+        s.onu_device = "BRCM1234"
+
+        self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
+
+        with self.assertRaises(Exception) as e:
+            self.rcord_subscriber.save()
+
+        self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234")
+        self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
+
+
+    def test_generate_c_tag(self):
+        s = Mock()
+        s.c_tag = "111"
+        s.onu_device = "BRCM1234"
+
+        self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
+        self.rcord_subscriber.c_tag = None
+
+        self.rcord_subscriber.save()
+
+        self.models_decl.RCORDSubscriber_decl.save.assert_called()
+        self.assertNotEquals(self.rcord_subscriber.c_tag, "111")
+        self.assertGreater(self.rcord_subscriber.c_tag, 16)
+        self.assertLess(self.rcord_subscriber.c_tag, 4097)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/model_policies/test_config.yaml b/xos/synchronizer/test_config.yaml
similarity index 100%
rename from xos/synchronizer/model_policies/test_config.yaml
rename to xos/synchronizer/test_config.yaml
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
index 71be7ca..b434628 100644
--- a/xos/unittest.cfg
+++ b/xos/unittest.cfg
@@ -3,3 +3,4 @@
 code-directories=synchronizer
                  model_policies
                  steps
+                 models