Fix bug 1865: Manually scaling VDU from 0 to 1 instance fails 72/11772/2
authorvegall <lvega@whitestack.com>
Tue, 22 Mar 2022 16:23:30 +0000 (16:23 +0000)
committervegall <lvega@whitestack.com>
Tue, 22 Mar 2022 19:12:51 +0000 (19:12 +0000)
Now it's possible scale to 0 and then scale back to 1 again

Change-Id: I295f1ae34d15da0186ba470897a9d6834fe9168b
Signed-off-by: vegall <lvega@whitestack.com>
osm_lcm/ns.py

index 9800f9e..613163b 100644 (file)
@@ -440,7 +440,8 @@ class NsLcm(LcmBase):
 
     def _get_vdu_additional_params(self, db_vnfr, vdu_id):
         vdur = next(
-            vdur for vdur in db_vnfr.get("vdur") if vdu_id == vdur["vdu-id-ref"]
+            (vdur for vdur in db_vnfr.get("vdur") if vdu_id == vdur["vdu-id-ref"]),
+            {}
         )
         additional_params = vdur.get("additionalParams")
         return parse_yaml_strings(additional_params)
@@ -517,6 +518,7 @@ class NsLcm(LcmBase):
     def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None, mark_delete=False):
 
         db_vdu_push_list = []
+        template_vdur = []
         db_update = {"_admin.modified": time()}
         if vdu_create:
             for vdu_id, vdu_count in vdu_create.items():
@@ -529,12 +531,22 @@ class NsLcm(LcmBase):
                     None,
                 )
                 if not vdur:
-                    raise LcmException(
-                        "Error scaling OUT VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
+                    # Read the template saved in the db:
+                    self.logger.debug(f"No vdur in the database. Using the vdur-template to scale")
+                    vdur_template = db_vnfr.get("vdur-template")
+                    if not vdur_template:
+                        raise LcmException(
+                           "Error scaling OUT VNFR for {}. No vnfr or template exists".format(
                             vdu_id
+                            )
                         )
-                    )
-
+                    vdur = vdur_template[0]
+                    #Delete a template from the database after using it
+                    self.db.set_one("vnfrs",
+                                {"_id": db_vnfr["_id"]},
+                                None,
+                                pull={"vdur-template": {"_id": vdur['_id']}}
+                            )
                 for count in range(vdu_count):
                     vdur_copy = deepcopy(vdur)
                     vdur_copy["status"] = "BUILD"
@@ -559,12 +571,17 @@ class NsLcm(LcmBase):
                             )
                         else:
                             iface.pop("mac-address", None)
-                        iface.pop(
-                            "mgmt_vnf", None
-                        )  # only first vdu can be managment of vnf
+                        if db_vnfr["vdur"]:
+                            iface.pop(
+                                "mgmt_vnf", None
+                            )  # only first vdu can be managment of vnf
                     db_vdu_push_list.append(vdur_copy)
                     # self.logger.debug("scale out, adding vdu={}".format(vdur_copy))
         if vdu_delete:
+            if len(db_vnfr["vdur"]) == 1:
+                # The scale will move to 0 instances
+                self.logger.debug(f"Scaling to 0 !, creating the template with the last vdur")
+                template_vdur = [db_vnfr["vdur"][0]]
             for vdu_id, vdu_count in vdu_delete.items():
                 if mark_delete:
                     indexes_to_delete = [
@@ -592,7 +609,14 @@ class NsLcm(LcmBase):
                             None,
                             pull={"vdur": {"_id": vdu["_id"]}},
                         )
-        db_push = {"vdur": db_vdu_push_list} if db_vdu_push_list else None
+        db_push = {}
+        if db_vdu_push_list:
+            db_push["vdur"] = db_vdu_push_list
+        if template_vdur:
+            db_push["vdur-template"] = template_vdur
+        if not db_push:
+            db_push = None
+        db_vnfr["vdur-template"] = template_vdur
         self.db.set_one("vnfrs", {"_id": db_vnfr["_id"]}, db_update, push_list=db_push)
         # modify passed dictionary db_vnfr
         db_vnfr_ = self.db.get_one("vnfrs", {"_id": db_vnfr["_id"]})