[CORD-2810] Only synchronizers can write feedback state

Change-Id: Ia3f28763a0212a570186e4c6c5cb118c40325b5b
diff --git a/lib/xos-genx/xos-genx-tests/test_django_generator.py b/lib/xos-genx/xos-genx-tests/test_django_generator.py
index 0ba90b5..c3ffef5 100644
--- a/lib/xos-genx/xos-genx-tests/test_django_generator.py
+++ b/lib/xos-genx/xos-genx-tests/test_django_generator.py
@@ -73,6 +73,32 @@
         self.assertEqual(len(blank_true), 1)
         self.assertEqual(len(blank_false), 1)
 
+    def test_feedback_state(self):
+        """
+        [XOS-GenX] Generate DJANGO models, verify feedback_state fields
+        """
+        xproto = \
+            """
+            option app_label = "test";
+
+            message ParentFoo {
+                required string parent_name = 1 [null = False, blank = False, feedback_state = True];
+            }
+
+            message Foo (ParentFoo) {
+                required string name = 1 [null = False, blank = False, feedback_state = True];
+            }
+            """
+
+        args = FakeArgs()
+        args.inputs = xproto
+        args.target = 'django.xtarget'
+        output = XOSProcessor.process(args)
+
+        print output
+
+        self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
+
 if __name__ == '__main__':
     unittest.main()
 
diff --git a/lib/xos-genx/xos-genx-tests/test_jinja2_django.py b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
new file mode 100644
index 0000000..ea8738e
--- /dev/null
+++ b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
@@ -0,0 +1,50 @@
+
+# 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 xosgenx.jinja2_extensions.django import *
+
+class Jinja2BaseTests(unittest.TestCase):
+    def test_xproto_optioned_fields_to_list(self):
+
+        fields = [
+            {
+                'name': 'has_feedback_1',
+                'options': {
+                    'feedback_state': 'True',
+                }
+            },
+            {
+                'name': 'has_feedback_2',
+                'options': {
+                    'feedback_state': 'True',
+                }
+            },
+            {
+                'name': 'no_feedback',
+                'options': {
+                    'feedback_state': 'False',
+                }
+            }
+        ]
+
+        res = xproto_optioned_fields_to_list(fields, 'feedback_state', 'True')
+        self.assertEqual(res, ["has_feedback_1", "has_feedback_2"])
+
+if __name__ == '__main__':
+    unittest.main()
+
+
diff --git a/lib/xos-genx/xos-genx-tests/test_translator.py b/lib/xos-genx/xos-genx-tests/test_translator.py
index 2f8cc8a..5923bc7 100644
--- a/lib/xos-genx/xos-genx-tests/test_translator.py
+++ b/lib/xos-genx/xos-genx-tests/test_translator.py
@@ -315,6 +315,28 @@
         output = XOSProcessor.process(args)
         self.assertIn('verbose_name: "Verbose Foo Name"', output)
 
+    def test_feedback_field(self):
+        xproto = \
+"""
+option app_label = "test";
+
+message ParentFoo {
+    required string parent_name = 1 [null = False, blank = False, feedback_state = True];
+}
+
+message Foo (ParentFoo) {
+    required string name = 1 [null = False, blank = False, feedback_state = True];
+}
+"""
+
+        args = FakeArgs()
+        args.inputs = xproto
+        args.target = 'modeldefs.xtarget'
+        output = XOSProcessor.process(args)
+
+        read_only = filter(lambda s: 'read_only: True' in s, output.splitlines())
+        self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo
+
 if __name__ == '__main__':
     unittest.main()
 
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/django.py b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
index 593fefb..2632c00 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/django.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
@@ -168,3 +168,27 @@
         return [map(str.strip, validation.split(':')) for validation in unquote(options['validators']).split(',')]
     except KeyError:
         return []
+
+def xproto_optioned_fields_to_list(fields, option, val):
+    """
+    List all the field that have a particural option
+    :param fields: (list) an array of message fields
+    :param option: (string) the option to look for
+    :param val: (any) the value of the option
+    :return: list of strings, field names where option is set
+    """
+
+    optioned_fields = []
+    for f in fields:
+        option_names = []
+        for k, v in f['options'].items():
+            option_names.append(k)
+
+        if option in  option_names and f['options'][option] == val:
+            optioned_fields.append(f['name'])
+
+    return optioned_fields
+
+# TODO
+# - in modeldefs add info about this fields
+# - update the gui to have this fields as readonly
diff --git a/lib/xos-genx/xosgenx/targets/django.xtarget b/lib/xos-genx/xosgenx/targets/django.xtarget
index 8ef8e5f..13d37f9 100644
--- a/lib/xos-genx/xosgenx/targets/django.xtarget
+++ b/lib/xos-genx/xosgenx/targets/django.xtarget
@@ -44,6 +44,11 @@
 class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases) }}:
   plural_name = "{{ xproto_pluralize(m) }}"
 
+  {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True')  %}
+  {%- if feedback_state_fields|length > 0 %}
+  feedback_state_fields = {{ feedback_state_fields }}
+  {%- endif %}
+
 {#  {% if m.options.no_sync or m.options.no_policy %}#}
 {#  {% if m.options.no_sync -%}#}
 {#  # Removing synchronizer feedback state from model#}
diff --git a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
index 3b0790a..15c43d7 100644
--- a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
+++ b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
@@ -29,6 +29,7 @@
     {% endfor %}
   {%- endif %}
     type: {{ xproto_type_to_ui_type(f) }}
+    read_only: {{ xproto_is_true(f.options.feedback_state) }}
     {% set validators = xproto_validators(f) -%}
     {% if validators -%}
     validators:
diff --git a/lib/xos-genx/xosgenx/targets/service.xtarget b/lib/xos-genx/xosgenx/targets/service.xtarget
index a9ca934..ed78ff1 100644
--- a/lib/xos-genx/xosgenx/targets/service.xtarget
+++ b/lib/xos-genx/xosgenx/targets/service.xtarget
@@ -41,6 +41,11 @@
 class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}:
   plural_name = "{{ xproto_pluralize(m) }}"
 
+  {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True')  %}
+  {%- if feedback_state_fields|length > 0 %}
+  feedback_state_fields = {{ feedback_state_fields }}
+  {%- endif %}
+
   KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
 
   {% if m.options.owner_class_name %}
diff --git a/xos/core/models/core.xproto b/xos/core/models/core.xproto
index f74b73d..19983db 100644
--- a/xos/core/models/core.xproto
+++ b/xos/core/models/core.xproto
@@ -13,18 +13,18 @@
      required string updated = 2 [default = "now()", content_type = "date"];
      optional string enacted = 3 [null = True, content_type = "date", blank = True, default = None];
      optional string policed = 4 [null = True, content_type = "date", blank = True, default = None];
-     optional string backend_register = 5 [default = "{}", max_length = 1024];
+     optional string backend_register = 5 [default = "{}", max_length = 1024, feedback_state = True];
      required bool backend_need_delete = 6 [default = False, blank = True];
      required bool backend_need_reap = 7 [default = False, blank = True];
-     required string backend_status = 8 [default = "Provisioning in progress", max_length = 1024, null = True];
-     required int32 backend_code = 9 [default = 0];
+     required string backend_status = 8 [default = "Provisioning in progress", max_length = 1024, null = True, feedback_state = True];
+     required int32 backend_code = 9 [default = 0, feedback_state = True];
      required bool deleted = 10 [default = False, blank = True];
      required bool write_protect = 12 [default = False, blank = True];
      required bool lazy_blocked = 13 [default = False, blank = True];
      required bool no_sync = 14 [default = False, blank = True];
      required bool no_policy = 15 [default = False, blank = True];
-     optional string policy_status = 16 [default = "Policy in process", max_length = 1024];
-     optional int32 policy_code = 16 [default = 0];
+     optional string policy_status = 16 [default = "Policy in process", max_length = 1024, feedback_state = True];
+     optional int32 policy_code = 16 [default = 0, feedback_state = True];
      required string leaf_model_name = 17 [null = False, max_length = 1024, help_text = "The most specialized model in this chain of inheritance, often defined by a service developer"];
      required bool backend_need_delete_policy = 18 [default = False, help_text = "True if delete model_policy must be run before object can be reaped", blank = True];
 }
@@ -288,10 +288,10 @@
 
 message Instance::instance_policy (XOSBase) {
      option validators = "instance_creator:Instance has no creator, instance_isolation: Container instance {obj.name} must use container image, instance_isolation_container_vm_parent:Container-vm instance {obj.name} must have a parent, instance_parent_isolation_container_vm:Parent field can only be set on Container-vm instances ({obj.name}), instance_isolation_vm: VM Instance {obj.name} must use VM image, instance_creator_privilege: instance creator has no privileges on slice";
-     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
-     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False, feedback_state = True];
+     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False, feedback_state = True];
      required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
-     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False, feedback_state = True];
      optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False, gui_hidden = True];
      required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
      optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
diff --git a/xos/core/models/xosbase.py b/xos/core/models/xosbase.py
index 03404cf..bc9220e 100644
--- a/xos/core/models/xosbase.py
+++ b/xos/core/models/xosbase.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from xos.exceptions import *
+from xos.exceptions import*
 from xosbase_decl import *
 
 class XOSBase(XOSBase_decl):
@@ -132,6 +132,20 @@
         if "always_update_timestamp" in kwargs:
             always_update_timestamp = always_update_timestamp or kwargs.pop("always_update_timestamp")
 
+        # validate that only synchronizers can write feedback state
+
+        # this for operator support via xossh in case if you want to force changes in feedback state
+        allow_modify_feedback = False
+        if "allow_modify_feedback" in kwargs:
+            allow_modify_feedback = kwargs.pop("allow_modify_feedback")
+
+        if hasattr(self, "feedback_state_fields") and not allow_modify_feedback and not self.is_new:
+            feedback_changed = [field for field in self.changed_fields if field in self.feedback_state_fields]
+
+            if len(feedback_changed) > 0 and caller_kind != "synchronizer":
+                log.error('A non Synchronizer is trying to update fields marked as feedback_state', model=self._dict, feedback_state_fields=self.feedback_state_fields, caller_kind=caller_kind)
+                raise XOSPermissionDenied('A non Synchronizer is trying to update fields marked as feedback_state')
+
         # SMBAKER: if an object is trying to delete itself, or if the observer
         # is updating an object's backend_* fields, then let it slip past the
         # composite key check.
diff --git a/xos/coreapi/protos/modeldefs.proto b/xos/coreapi/protos/modeldefs.proto
index 999f4f9..5eaa126 100644
--- a/xos/coreapi/protos/modeldefs.proto
+++ b/xos/coreapi/protos/modeldefs.proto
@@ -35,6 +35,7 @@
     repeated FieldValidator validators = 5;
     repeated FieldOption options = 6;
     string default = 7;
+    bool read_only = 8;
 };
 
 message ModelDef {
diff --git a/xos/xos_client/xosapi/convenience/voltserviceinstance.py b/xos/xos_client/xosapi/convenience/voltserviceinstance.py
index 26ed09a..47b11bd 100644
--- a/xos/xos_client/xosapi/convenience/voltserviceinstance.py
+++ b/xos/xos_client/xosapi/convenience/voltserviceinstance.py
@@ -50,6 +50,9 @@
 
     @property
     def s_tag(self):
+        if not self.subscriber:
+            raise Exception("vOLT %s has no subscriber" % self.name)
+
         olt_device = self.stub.VOLTDevice.objects.get(device_id = self.subscriber.olt_device)
         olt_port = self.stub.VOLTDevicePort.objects.get(port_id = self.subscriber.olt_port, volt_device_id=olt_device.id)
         if olt_port: