Feature 10906: Support for Anti-Affinity groups 84/11884/1
authorAlexis Romero <garomero@indra.es>
Fri, 11 Mar 2022 14:53:40 +0000 (15:53 +0100)
committerAlexis Romero <garomero@indra.es>
Wed, 13 Apr 2022 16:45:56 +0000 (18:45 +0200)
Change-Id: I4ba62d2da9f03c2b26ec60d885673f906268887a
Signed-off-by: Alexis Romero <garomero@indra.es>
osm_nbi/instance_topics.py

index 22580d1..39d9834 100644 (file)
@@ -477,6 +477,7 @@ class NsrTopic(BaseTopic):
             "ssh-authorized-key": ns_request.get("ssh_keys"),  # TODO remove
             "flavor": [],
             "image": [],
+            "affinity-or-anti-affinity-group": [],
         }
         ns_request["nsr_id"] = nsr_id
         if ns_request and ns_request.get("config-units"):
@@ -591,6 +592,16 @@ class NsrTopic(BaseTopic):
                         image_data = self._get_image_data_from_vnfd(vnfd, alt_image)
                         self._add_image_to_nsr(nsr_descriptor, image_data)
 
+                # 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])
+
+                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)
+
             for vld in nsr_vld:
                 vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get(
                     vld.get("id"), []
@@ -600,6 +611,39 @@ class NsrTopic(BaseTopic):
 
         return nsr_descriptor
 
+    def _get_affinity_or_anti_affinity_group_data_from_vnfd(self, vnfd, ag_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
+        )
+        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):
+        """
+        Adds affinity-or-anti-affinity-group to nsr checking first it is not already added
+        """
+        ag = 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)
+            ),
+            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)
+
     def _get_image_data_from_vnfd(self, vnfd, sw_image_id):
         sw_image_desc = utils.find_in_list(
             vnfd.get("sw-image-desc", ()), lambda sw: sw["id"] == sw_image_id
@@ -941,6 +985,31 @@ class NsrTopic(BaseTopic):
             if nsr_flavor_desc:
                 vdur["ns-flavor-id"] = nsr_flavor_desc["id"]
 
+            # Adding Affinity groups information to vdur
+            try:
+                ags_vdu_profile = 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"],
+                    )
+                    nsr_ags_data = utils.find_in_list(
+                        nsr_descriptor["affinity-or-anti-affinity-group"],
+                        lambda nsr_ag: (
+                            nsr_ag.get("ag-id") == vdu_ag.get("id")
+                        ),
+                    )
+                    ags_ids.append(nsr_ags_data["id"])
+                vdur["affinity-or-anti-affinity-group-id"] = ags_ids
+
             if vdu_instantiation_level:
                 count = vdu_instantiation_level.get("number-of-instances")
             else: