From 44506f8341fed32f064a762495caa2f7e9bc6774 Mon Sep 17 00:00:00 2001 From: Alexis Romero Date: Tue, 26 Apr 2022 19:10:21 +0200 Subject: [PATCH] Fixes bug 2013 : Passing Anti-affinity group as additionalParamsForVnf Change-Id: I965ad7339573ecfe8f2eb699a354396f096cf9c5 Signed-off-by: Alexis Romero --- osm_nbi/instance_topics.py | 117 +++++++++++++++++++++++++------------ osm_nbi/validation.py | 13 +++++ 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/osm_nbi/instance_topics.py b/osm_nbi/instance_topics.py index 02572d0..61369b5 100644 --- a/osm_nbi/instance_topics.py +++ b/osm_nbi/instance_topics.py @@ -594,13 +594,26 @@ class NsrTopic(BaseTopic): # Add Affinity or Anti-affinity group information to NSR vdu_profiles = vnfd.get("df", [[]])[0].get("vdu-profile", ()) - ag_prefix_name = "{}-{}".format(nsr_descriptor["name"][:16], vnf_profile.get("id")[:16]) + affinity_group_prefix_name = "{}-{}".format( + nsr_descriptor["name"][:16], vnf_profile.get("id")[:16] + ) for vdu_profile in vdu_profiles: - ag_data = {} - for ag in vdu_profile.get("affinity-or-anti-affinity-group", ()): - ag_data = self._get_affinity_or_anti_affinity_group_data_from_vnfd(vnfd, ag["id"]) - self._add_affinity_or_anti_affinity_group_to_nsr(nsr_descriptor, ag_data, ag_prefix_name) + affinity_group_data = {} + for affinity_group in vdu_profile.get( + "affinity-or-anti-affinity-group", () + ): + affinity_group_data = ( + self._get_affinity_or_anti_affinity_group_data_from_vnfd( + vnfd, affinity_group["id"] + ) + ) + affinity_group_data["member-vnf-index"] = vnf_profile.get("id") + self._add_affinity_or_anti_affinity_group_to_nsr( + nsr_descriptor, + affinity_group_data, + affinity_group_prefix_name, + ) for vld in nsr_vld: vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get( @@ -611,38 +624,50 @@ class NsrTopic(BaseTopic): return nsr_descriptor - def _get_affinity_or_anti_affinity_group_data_from_vnfd(self, vnfd, ag_id): + def _get_affinity_or_anti_affinity_group_data_from_vnfd( + self, vnfd, affinity_group_id + ): """ Gets affinity-or-anti-affinity-group info from df and returns the desired affinity group """ - affinity_or_anti_affinity_group = utils.find_in_list( - vnfd.get("df", [[]])[0].get("affinity-or-anti-affinity-group", ()), lambda ag: ag["id"] == ag_id + affinity_group = utils.find_in_list( + vnfd.get("df", [[]])[0].get("affinity-or-anti-affinity-group", ()), + lambda ag: ag["id"] == affinity_group_id, ) - ag_data = {} - if affinity_or_anti_affinity_group and affinity_or_anti_affinity_group.get("id"): - ag_data["ag-id"] = affinity_or_anti_affinity_group["id"] - if affinity_or_anti_affinity_group and affinity_or_anti_affinity_group.get("type"): - ag_data["type"] = affinity_or_anti_affinity_group["type"] - if affinity_or_anti_affinity_group and affinity_or_anti_affinity_group.get("scope"): - ag_data["scope"] = affinity_or_anti_affinity_group["scope"] - return ag_data - - def _add_affinity_or_anti_affinity_group_to_nsr(self, nsr_descriptor, ag_data, ag_prefix_name): + affinity_group_data = {} + if affinity_group: + if affinity_group.get("id"): + affinity_group_data["ag-id"] = affinity_group["id"] + if affinity_group.get("type"): + affinity_group_data["type"] = affinity_group["type"] + if affinity_group.get("scope"): + affinity_group_data["scope"] = affinity_group["scope"] + return affinity_group_data + + def _add_affinity_or_anti_affinity_group_to_nsr( + self, nsr_descriptor, affinity_group_data, affinity_group_prefix_name + ): """ Adds affinity-or-anti-affinity-group to nsr checking first it is not already added """ - ag = next( + affinity_group = next( ( f for f in nsr_descriptor["affinity-or-anti-affinity-group"] - if all(f.get(k) == ag_data[k] for k in ag_data) + if all(f.get(k) == affinity_group_data[k] for k in affinity_group_data) ), None, ) - if not ag: - ag_data["id"] = str(len(nsr_descriptor["affinity-or-anti-affinity-group"])) - ag_data["name"] = "{}-{}-{}".format(ag_prefix_name, ag_data["ag-id"][:32], ag_data.get("id") or 0) - nsr_descriptor["affinity-or-anti-affinity-group"].append(ag_data) + if not affinity_group: + affinity_group_data["id"] = str( + len(nsr_descriptor["affinity-or-anti-affinity-group"]) + ) + affinity_group_data["name"] = "{}-{}".format( + affinity_group_prefix_name, affinity_group_data["ag-id"][:32] + ) + nsr_descriptor["affinity-or-anti-affinity-group"].append( + affinity_group_data + ) def _get_image_data_from_vnfd(self, vnfd, sw_image_id): sw_image_desc = utils.find_in_list( @@ -986,28 +1011,46 @@ class NsrTopic(BaseTopic): # Adding Affinity groups information to vdur try: - ags_vdu_profile = utils.find_in_list( + vdu_profile_affinity_group = utils.find_in_list( vnfd.get("df")[0]["vdu-profile"], lambda a_vdu: a_vdu["id"] == vdu["id"], ) except Exception: - ags_vdu_profile = None - - if ags_vdu_profile: - ags_ids = [] - for ag in ags_vdu_profile.get("affinity-or-anti-affinity-group", ()): - vdu_ag = utils.find_in_list( - ags_vdu_profile.get("affinity-or-anti-affinity-group", ()), - lambda ag_fp: ag_fp["id"] == ag["id"], + vdu_profile_affinity_group = None + + if vdu_profile_affinity_group: + affinity_group_ids = [] + for affinity_group in vdu_profile_affinity_group.get( + "affinity-or-anti-affinity-group", () + ): + vdu_affinity_group = utils.find_in_list( + vdu_profile_affinity_group.get( + "affinity-or-anti-affinity-group", () + ), + lambda ag_fp: ag_fp["id"] == affinity_group["id"], ) - nsr_ags_data = utils.find_in_list( + nsr_affinity_group = utils.find_in_list( nsr_descriptor["affinity-or-anti-affinity-group"], lambda nsr_ag: ( - nsr_ag.get("ag-id") == vdu_ag.get("id") + nsr_ag.get("ag-id") == vdu_affinity_group.get("id") + and nsr_ag.get("member-vnf-index") + == vnfr_descriptor.get("member-vnf-index-ref") ), ) - ags_ids.append(nsr_ags_data["id"]) - vdur["affinity-or-anti-affinity-group-id"] = ags_ids + # Update Affinity Group VIM name if VDU instantiation parameter is present + if vnf_params and vnf_params.get("affinity-or-anti-affinity-group"): + vnf_params_affinity_group = utils.find_in_list( + vnf_params["affinity-or-anti-affinity-group"], + lambda vnfp_ag: ( + vnfp_ag.get("id") == vdu_affinity_group.get("id") + ), + ) + if vnf_params_affinity_group.get("vim-affinity-group-id"): + nsr_affinity_group[ + "vim-affinity-group-id" + ] = vnf_params_affinity_group["vim-affinity-group-id"] + affinity_group_ids.append(nsr_affinity_group["id"]) + vdur["affinity-or-anti-affinity-group-id"] = affinity_group_ids if vdu_instantiation_level: count = vdu_instantiation_level.get("number-of-instances") diff --git a/osm_nbi/validation.py b/osm_nbi/validation.py index d1cfc1d..1d637c5 100644 --- a/osm_nbi/validation.py +++ b/osm_nbi/validation.py @@ -316,6 +316,19 @@ additional_params_for_vnf = { "additionalProperties": False, }, }, + "affinity-or-anti-affinity-group": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": name_schema, + "vim-affinity-group-id": name_schema, + }, + "required": ["id"], + "minProperties": 2, + "additionalProperties": False, + }, + }, }, "required": ["member-vnf-index"], "minProperties": 2, -- 2.25.1