[SEBA-547] TP NEM Integration

Change-Id: I6c0dab752f4728d5f6a777ea297321ebb75de769
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
index 4d5b8b8..59b6e66 100644
--- a/xos/synchronizer/models/models.py
+++ b/xos/synchronizer/models/models.py
@@ -25,6 +25,9 @@
 from models_decl import ONUDevice_decl
 from models_decl import PONONUPort_decl
 from models_decl import UNIPort_decl
+from models_decl import TechnologyProfile_decl
+
+import json
 
 class VOLTService(VOLTService_decl):
     class Meta:
@@ -116,3 +119,28 @@
     class Meta:
         proxy = True
 
+class TechnologyProfile(TechnologyProfile_decl):
+    class Meta:
+        proxy = True
+
+    def save(self, *args, **kwargs):
+
+        caller_kind = None
+        if "caller_kind" in kwargs:
+            caller_kind = kwargs.get("caller_kind")
+
+        # only synchronizer is allowed to update the model
+        if not self.is_new and caller_kind != "synchronizer":
+            if not self.deleted:
+                existing = TechnologyProfile.objects.filter(id=self.id)
+                raise XOSValidationError('Modification operation is not allowed on Technology Profile [/%s/%s]. Delete it and add again' % (existing[0].technology, existing[0].profile_id))
+
+        # validate if technology profile value is valid JSON format string
+        if self.profile_value != None:
+            try:
+                tp_json_val = json.loads(self.profile_value)
+            except ValueError as e:
+                raise XOSValidationError('Technology Profile value not in valid JSON format')
+
+        super(TechnologyProfile, self).save(*args, **kwargs)
+
diff --git a/xos/synchronizer/models/test_models.py b/xos/synchronizer/models/test_models.py
index 3ede98c..3048071 100644
--- a/xos/synchronizer/models/test_models.py
+++ b/xos/synchronizer/models/test_models.py
@@ -113,6 +113,9 @@
         self.onu_device.is_new = True
         self.onu_device.serial_number = 1234
 
+    def tearDown(self):
+        self.module_patcher.stop()
+
     def test_delete(self):
         self.onu_device.delete()
         self.models_decl.ONUDevice_decl.delete.assert_called()
@@ -129,5 +132,67 @@
                          'ONU "1234" can\'t be deleted as it has subscribers associated with it')
         self.models_decl.OLTDevice_decl.delete.assert_not_called()
 
+class TestTechnologyProfileModel(unittest.TestCase):
+
+    def setUp(self):
+        self.xos = XOS
+
+        self.models_decl = Mock()
+        self.models_decl.TechnologyProfile_decl = MagicMock
+        self.models_decl.TechnologyProfile_decl.save = Mock()
+        self.models_decl.TechnologyProfile_decl.objects = Mock()
+        self.models_decl.TechnologyProfile_decl.objects.filter.return_value = []
+
+        modules = {
+            'xos': MagicMock(),
+            '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 TechnologyProfile
+
+        self.technology_profile = TechnologyProfile()
+        self.technology_profile.deleted = False
+        self.technology_profile.id = None  # this is a new model
+        self.technology_profile.is_new = True
+        self.technology_profile.technology = 'xgspon'
+        self.technology_profile.profile_id = 64
+        self.technology_profile.profile_value = '{ "name": "4QueueHybridProfileMap1", "profile_type": "XPON", "version": 1, "num_gem_ports": 4, "instance_control": { "onu": "multi-instance", "uni": "single-instance", "max_gem_payload_size": "auto" }, "us_scheduler": { "additional_bw": "auto", "direction": "UPSTREAM", "priority": 0, "weight": 0, "q_sched_policy": "hybrid" }, "ds_scheduler": { "additional_bw": "auto", "direction": "DOWNSTREAM", "priority": 0, "weight": 0, "q_sched_policy": "hybrid" }, "upstream_gem_port_attribute_list": [ { "pbit_map": "0b00000101", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 4, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "max_threshold": 0, "min_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00011010", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 3, "weight": 75, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00100000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 2, "weight": 0, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b11000000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 1, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } } ], "downstream_gem_port_attribute_list": [ { "pbit_map": "0b00000101", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 4, "weight": 10, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00011010", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 3, "weight": 90, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00100000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 2, "weight": 0, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b11000000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 1, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } } ]}'
+
+    def tearDown(self):
+        self.module_patcher.stop()
+
+    def test_save(self):
+        self.technology_profile.save()
+        self.models_decl.TechnologyProfile_decl.save.assert_called()
+
+    def test_prevent_modify(self):
+        self.technology_profile.is_new = False
+        self.technology_profile.id = 1
+        self.technology_profile.profile_value = '{"name": "someValue", "profile_type": "someValue"}'
+
+        self.models_decl.TechnologyProfile_decl.objects.filter.return_value = [self.technology_profile]
+
+        with self.assertRaises(Exception) as e:
+            self.technology_profile.save()
+
+        self.assertEqual(e.exception.message,
+                         'Modification operation is not allowed on Technology Profile [/xgspon/64]. Delete it and add again')
+        self.models_decl.TechnologyProfile_decl.save.assert_not_called()
+
+    def test_invalid_tech_profile_value_format(self):
+        self.technology_profile.profile_value = 'someTechProfileValue'
+
+        with self.assertRaises(Exception) as e:
+            self.technology_profile.save()
+
+        self.assertEqual(e.exception.message,
+                         'Technology Profile value not in valid JSON format')
+        self.models_decl.TechnologyProfile_decl.save.assert_not_called()
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/xos/synchronizer/models/volt.xproto b/xos/synchronizer/models/volt.xproto
index 5ae85ea..8856c0f 100644
--- a/xos/synchronizer/models/volt.xproto
+++ b/xos/synchronizer/models/volt.xproto
@@ -230,3 +230,21 @@
         help_text = "ONUDevice that belongs to this Subscriber chain",
         db_index = True];
 }
+
+message TechnologyProfile (XOSBase) {
+    option verbose_name = "Technology Profile";
+    option description = "The Technology Profile that is utilized by VOLTHA";
+
+    required string technology = 1 [
+        help_text = "The technology being utilized by the adaptor",
+        db_index = True,
+        max_length = 16];
+    required int32 profile_id = 2 [
+        help_text = "The numeric id of the profile",
+        db_index = True,
+        min_value = 64,
+        max_value = 255];
+    required string profile_value = 3 [
+        help_text = "The technology profile value in JSON format",
+        max_length = 4096];
+}