From: K Sai Kiran Date: Mon, 25 Nov 2019 12:00:37 +0000 (+0530) Subject: Bug 868 - Fixed X-Git-Tag: v7.0.0rc1~6 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F33%2F8233%2F3;p=osm%2FNBI.git Bug 868 - Fixed 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 --- diff --git a/osm_nbi/descriptor_topics.py b/osm_nbi/descriptor_topics.py index 18ce3e7..f388ad1 100644 --- a/osm_nbi/descriptor_topics.py +++ b/osm_nbi/descriptor_topics.py @@ -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 = {}