Bug 868 - Fixed 33/8233/3
authorK Sai Kiran <saikiran.k@tataelxsi.co.in>
Mon, 25 Nov 2019 12:00:37 +0000 (17:30 +0530)
committerK Sai Kiran <saikiran.k@tataelxsi.co.in>
Tue, 3 Dec 2019 05:49:07 +0000 (11:19 +0530)
NSD and VNFD items with schema type list containing same identifiers are truncated and used without error.
Fix essentially makes a depth traversal of the JSON object and
uses Id or Name of object to uniquely identify object. Key Id takes precedence over Name.

Change-Id: I534c96fb573b651cca71f0309e1a912d6b5889da
Signed-off-by: K Sai Kiran <saikiran.k@tataelxsi.co.in>
osm_nbi/descriptor_topics.py

index 18ce3e7..f388ad1 100644 (file)
@@ -39,6 +39,27 @@ class DescriptorTopic(BaseTopic):
 
     def check_conflict_on_edit(self, session, final_content, edit_content, _id):
         super().check_conflict_on_edit(session, final_content, edit_content, _id)
+
+        def _check_unique_id_name(descriptor, position=""):
+            for desc_key, desc_item in descriptor.items():
+                if isinstance(desc_item, list) and desc_item:
+                    used_ids = []
+                    desc_item_id = None
+                    for index, list_item in enumerate(desc_item):
+                        if isinstance(list_item, dict):
+                            _check_unique_id_name(list_item, "{}.{}[{}]"
+                                                  .format(position, desc_key, index))
+                            # Base case
+                            if index == 0 and (list_item.get("id") or list_item.get("name")):
+                                desc_item_id = "id" if list_item.get("id") else "name"
+                            if desc_item_id and list_item.get(desc_item_id):
+                                if list_item[desc_item_id] in used_ids:
+                                    position = "{}.{}[{}]".format(position, desc_key, index)
+                                    raise EngineException("Error: identifier {} '{}' is not unique and repeats at '{}'"
+                                                          .format(desc_item_id, list_item[desc_item_id],
+                                                                  position), HTTPStatus.UNPROCESSABLE_ENTITY)
+                                used_ids.append(list_item[desc_item_id])
+        _check_unique_id_name(final_content)
         # 1. validate again with pyangbind
         # 1.1. remove internal keys
         internal_keys = {}