Fix bug 2283: Flavors are not longer created when using vim-flavor-id 99/13799/2
authorGabriel Cuba <gcuba@whitestack.com>
Mon, 7 Aug 2023 23:43:26 +0000 (18:43 -0500)
committergatici <gulsum.atici@canonical.com>
Wed, 9 Aug 2023 08:41:54 +0000 (11:41 +0300)
Change-Id: Ic58611876180b93fb0a9b6310986a388df5a18d3
Signed-off-by: Gabriel Cuba <gcuba@whitestack.com>
NG-RO/osm_ng_ro/ns.py
NG-RO/osm_ng_ro/ns_thread.py
NG-RO/osm_ng_ro/tests/test_ns.py
releasenotes/notes/vim_flavor_id_fix-1bd322a8a284a560.yaml [new file with mode: 0644]

index 6cea780..84a800e 100644 (file)
@@ -818,17 +818,23 @@ class Ns(object):
         db = kwargs.get("db")
         target_vdur = {}
 
+        for vnf in indata.get("vnf", []):
+            for vdur in vnf.get("vdur", []):
+                if vdur.get("ns-flavor-id") == target_flavor.get("id"):
+                    target_vdur = vdur
+
+        vim_flavor_id = (
+            target_vdur.get("additionalParams", {}).get("OSM", {}).get("vim_flavor_id")
+        )
+        if vim_flavor_id:  # vim-flavor-id was passed so flavor won't be created
+            return {"find_params": {"vim_flavor_id": vim_flavor_id}}
+
         flavor_data = {
             "disk": int(target_flavor["storage-gb"]),
             "ram": int(target_flavor["memory-mb"]),
             "vcpus": int(target_flavor["vcpu-count"]),
         }
 
-        for vnf in indata.get("vnf", []):
-            for vdur in vnf.get("vdur", []):
-                if vdur.get("ns-flavor-id") == target_flavor.get("id"):
-                    target_vdur = vdur
-
         if db and isinstance(indata.get("vnf"), list):
             vnfd_id = indata.get("vnf")[0].get("vnfd-id")
             vnfd = db.get_one("vnfds", {"_id": vnfd_id})
@@ -1522,12 +1528,12 @@ class Ns(object):
         vnf_preffix = "vnfrs:{}".format(vnfr_id)
         ns_preffix = "nsrs:{}".format(nsr_id)
         image_text = ns_preffix + ":image." + target_vdu["ns-image-id"]
-        extra_dict = {"depends_on": [image_text]}
+        flavor_text = ns_preffix + ":flavor." + target_vdu["ns-flavor-id"]
+        extra_dict = {"depends_on": [image_text, flavor_text]}
         net_list = []
         persistent_root_disk = {}
         persistent_ordinary_disk = {}
         vdu_instantiation_volumes_list = []
-        vdu_instantiation_flavor_id = None
         disk_list = []
         vnfd_id = vnfr["vnfd-id"]
         vnfd = db.get_one("vnfds", {"_id": vnfd_id})
@@ -1566,17 +1572,6 @@ class Ns(object):
             vdu_instantiation_volumes_list = (
                 target_vdu.get("additionalParams").get("OSM", {}).get("vdu_volumes")
             )
-            vdu_instantiation_flavor_id = (
-                target_vdu.get("additionalParams").get("OSM", {}).get("vim_flavor_id")
-            )
-
-        # flavor id
-        if vdu_instantiation_flavor_id:
-            flavor_id = vdu_instantiation_flavor_id
-        else:
-            flavor_text = ns_preffix + ":flavor." + target_vdu["ns-flavor-id"]
-            flavor_id = "TASK-" + flavor_text
-            extra_dict["depends_on"].append(flavor_text)
 
         if vdu_instantiation_volumes_list:
             # Find the root volumes and add to the disk_list
@@ -1624,7 +1619,7 @@ class Ns(object):
             "description": target_vdu["vdu-name"],
             "start": True,
             "image_id": "TASK-" + image_text,
-            "flavor_id": flavor_id,
+            "flavor_id": "TASK-" + flavor_text,
             "affinity_group_list": affinity_group_list,
             "net_list": net_list,
             "cloud_config": cloud_config or None,
index 861ed3d..42a8a59 100644 (file)
@@ -847,7 +847,9 @@ class VimInteractionFlavor(VimInteractionBase):
             # FIND
             vim_flavor_id = None
 
-            if task.get("find_params"):
+            if task.get("find_params", {}).get("vim_flavor_id"):
+                vim_flavor_id = task["find_params"]["vim_flavor_id"]
+            elif task.get("find_params", {}).get("flavor_data"):
                 try:
                     flavor_data = task["find_params"]["flavor_data"]
                     vim_flavor_id = target_vim.get_flavor_id_from_data(flavor_data)
index 27c21a6..c692c51 100644 (file)
@@ -207,24 +207,6 @@ expected_extra_dict2 = {
     },
 }
 
-expected_extra_dict3 = {
-    "depends_on": [
-        f"{ns_preffix}:image.0",
-    ],
-    "params": {
-        "affinity_group_list": [],
-        "availability_zone_index": None,
-        "availability_zone_list": None,
-        "cloud_config": None,
-        "description": "without_volumes-VM",
-        "disk_list": [],
-        "flavor_id": "flavor_test",
-        "image_id": f"TASK-{ns_preffix}:image.0",
-        "name": "sample_name-vnf-several-volu-without_volumes-VM-0",
-        "net_list": [],
-        "start": True,
-    },
-}
 tasks_by_target_record_id = {
     "nsrs:th47f48-9870-4169-b758-9732e1ff40f3": {
         "extra_dict": {
@@ -2386,6 +2368,53 @@ class TestNs(unittest.TestCase):
         self.assertTrue(epa_params.called)
         self.assertDictEqual(result, expected_result)
 
+    @patch("osm_ng_ro.ns.Ns._process_epa_params")
+    def test__process_flavor_params_with_vim_flavor_id(
+        self,
+        epa_params,
+    ):
+        expected_result = {
+            "find_params": {
+                "vim_flavor_id": "test.flavor",
+            },
+        }
+        target_flavor = {
+            "id": "test_id",
+            "name": "test",
+            "storage-gb": "10",
+            "memory-mb": "1024",
+            "vcpu-count": "2",
+        }
+        indata = {
+            "vnf": [
+                {
+                    "vdur": [
+                        {
+                            "ns-flavor-id": "test_id",
+                            "additionalParams": {
+                                "OSM": {"vim_flavor_id": "test.flavor"}
+                            },
+                        },
+                    ],
+                    "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+                },
+            ],
+        }
+        vim_info = {}
+        target_record_id = ""
+
+        epa_params.return_value = {}
+
+        result = Ns._process_flavor_params(
+            target_flavor=target_flavor,
+            indata=indata,
+            vim_info=vim_info,
+            target_record_id=target_record_id,
+        )
+
+        self.assertFalse(epa_params.called)
+        self.assertDictEqual(result, expected_result)
+
     @patch("osm_ng_ro.ns.Ns._process_epa_params")
     def test__process_flavor_params(
         self,
@@ -4976,76 +5005,6 @@ class TestProcessVduParams(unittest.TestCase):
             persistent_root_disk, target_vdu, vdu_instantiation_vol_list, []
         )
 
-    @patch("osm_ng_ro.ns.Ns._sort_vdu_interfaces")
-    @patch("osm_ng_ro.ns.Ns._partially_locate_vdu_interfaces")
-    @patch("osm_ng_ro.ns.Ns._prepare_vdu_interfaces")
-    @patch("osm_ng_ro.ns.Ns._prepare_vdu_cloud_init")
-    @patch("osm_ng_ro.ns.Ns._prepare_vdu_ssh_keys")
-    @patch("osm_ng_ro.ns.Ns.find_persistent_root_volumes")
-    @patch("osm_ng_ro.ns.Ns.find_persistent_volumes")
-    @patch("osm_ng_ro.ns.Ns._add_persistent_root_disk_to_disk_list")
-    @patch("osm_ng_ro.ns.Ns._add_persistent_ordinary_disks_to_disk_list")
-    @patch("osm_ng_ro.ns.Ns._prepare_vdu_affinity_group_list")
-    def test_process_vdu_params_with_inst_flavor_id(
-        self,
-        mock_prepare_vdu_affinity_group_list,
-        mock_add_persistent_ordinary_disks_to_disk_list,
-        mock_add_persistent_root_disk_to_disk_list,
-        mock_find_persistent_volumes,
-        mock_find_persistent_root_volumes,
-        mock_prepare_vdu_ssh_keys,
-        mock_prepare_vdu_cloud_init,
-        mock_prepare_vdu_interfaces,
-        mock_locate_vdu_interfaces,
-        mock_sort_vdu_interfaces,
-    ):
-        """Instantiation volume list is empty."""
-        target_vdu = deepcopy(target_vdu_wthout_persistent_storage)
-
-        target_vdu["interfaces"] = interfaces_wth_all_positions
-
-        vdu_instantiation_flavor_id = "flavor_test"
-
-        target_vdu["additionalParams"] = {
-            "OSM": {"vim_flavor_id": vdu_instantiation_flavor_id}
-        }
-        mock_prepare_vdu_cloud_init.return_value = {}
-        mock_prepare_vdu_affinity_group_list.return_value = []
-
-        new_kwargs = deepcopy(kwargs)
-        new_kwargs.update(
-            {
-                "vnfr_id": vnfr_id,
-                "nsr_id": nsr_id,
-                "tasks_by_target_record_id": {},
-                "logger": "logger",
-            }
-        )
-        expected_extra_dict_copy = deepcopy(expected_extra_dict3)
-        vnfd = deepcopy(vnfd_wth_persistent_storage)
-        db.get_one.return_value = vnfd
-        result = Ns._process_vdu_params(
-            target_vdu, indata, vim_info=None, target_record_id=None, **new_kwargs
-        )
-        mock_sort_vdu_interfaces.assert_called_once_with(target_vdu)
-        mock_locate_vdu_interfaces.assert_not_called()
-        mock_prepare_vdu_cloud_init.assert_called_once()
-        mock_add_persistent_root_disk_to_disk_list.assert_called_once()
-        mock_add_persistent_ordinary_disks_to_disk_list.assert_called_once()
-        mock_prepare_vdu_interfaces.assert_called_once_with(
-            target_vdu,
-            expected_extra_dict_copy,
-            ns_preffix,
-            vnf_preffix,
-            "logger",
-            {},
-            [],
-        )
-        self.assertDictEqual(result, expected_extra_dict_copy)
-        mock_prepare_vdu_ssh_keys.assert_called_once_with(target_vdu, None, {})
-        mock_prepare_vdu_affinity_group_list.assert_called_once()
-        mock_find_persistent_volumes.assert_not_called()
-
     @patch("osm_ng_ro.ns.Ns._sort_vdu_interfaces")
     @patch("osm_ng_ro.ns.Ns._partially_locate_vdu_interfaces")
     @patch("osm_ng_ro.ns.Ns._prepare_vdu_interfaces")
diff --git a/releasenotes/notes/vim_flavor_id_fix-1bd322a8a284a560.yaml b/releasenotes/notes/vim_flavor_id_fix-1bd322a8a284a560.yaml
new file mode 100644 (file)
index 0000000..b4ffa65
--- /dev/null
@@ -0,0 +1,21 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# 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.
+#######################################################################################
+---
+fixes:
+  - |
+    Fixes bug 2283: flavors are no longer unnecessarily created when `vim-flavor-id` is passed as instantiation 
+    parameter.