+ extra_dict = process_params(
+ target_item,
+ indata,
+ target_viminfo,
+ target_record_id,
+ **kwargs,
+ )
+ self._assign_vim(target_vim)
+
+ deployment_info = {
+ "action_id": action_id,
+ "nsr_id": nsr_id,
+ "task_index": task_index,
+ }
+
+ new_item = {
+ "deployment_info": deployment_info,
+ "target_id": target_vim,
+ "item": item_,
+ "action": "CREATE",
+ "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}",
+ "target_record_id": target_record_id,
+ "extra_dict": extra_dict,
+ "common_id": target_item.get("common_id", None),
+ }
+ diff_items.append(new_item)
+ tasks_by_target_record_id[target_record_id] = new_item
+ task_index += 1
+
+ db_nsr_update[db_path + ".{}".format(item_index)] = target_item
+
+ return diff_items, task_index
+
+ def calculate_all_differences_to_deploy(
+ self,
+ indata,
+ nsr_id,
+ db_nsr,
+ db_vnfrs,
+ db_ro_nsr,
+ db_nsr_update,
+ db_vnfrs_update,
+ action_id,
+ tasks_by_target_record_id,
+ ):
+ """This method calculates the ordered list of items (`changes_list`)
+ to be created and deleted.
+
+ Args:
+ indata (Dict[str, Any]): deployment info
+ nsr_id (str): NSR id
+ db_nsr: NSR record from DB
+ db_vnfrs: VNFRS record from DB
+ db_ro_nsr (Dict[str, Any]): record from "ro_nsrs"
+ db_nsr_update (Dict[str, Any]): NSR info to update in DB
+ db_vnfrs_update (Dict[str, Any]): VNFRS info to update in DB
+ action_id (str): action id
+ tasks_by_target_record_id (Dict[str, Any]):
+ [<target_record_id>, <task>]
+
+ Returns:
+ List: ordered list of items to be created and deleted.
+ """
+
+ task_index = 0
+ # set list with diffs:
+ changes_list = []
+
+ # NS vld, image and flavor
+ for item in ["net", "image", "flavor", "affinity-or-anti-affinity-group"]:
+ self.logger.debug("process NS={} {}".format(nsr_id, item))
+ diff_items, task_index = self.calculate_diff_items(
+ indata=indata,
+ db_nsr=db_nsr,
+ db_ro_nsr=db_ro_nsr,
+ db_nsr_update=db_nsr_update,
+ item=item,
+ tasks_by_target_record_id=tasks_by_target_record_id,
+ action_id=action_id,
+ nsr_id=nsr_id,
+ task_index=task_index,
+ vnfr_id=None,
+ )
+ changes_list += diff_items
+
+ # VNF vlds and vdus
+ for vnfr_id, vnfr in db_vnfrs.items():
+ # vnfr_id need to be set as global variable for among others nested method _process_vdu_params
+ for item in ["net", "vdu"]:
+ self.logger.debug("process VNF={} {}".format(vnfr_id, item))
+ diff_items, task_index = self.calculate_diff_items(
+ indata=indata,
+ db_nsr=db_nsr,
+ db_ro_nsr=db_ro_nsr,
+ db_nsr_update=db_vnfrs_update[vnfr["_id"]],
+ item=item,
+ tasks_by_target_record_id=tasks_by_target_record_id,
+ action_id=action_id,
+ nsr_id=nsr_id,
+ task_index=task_index,
+ vnfr_id=vnfr_id,
+ vnfr=vnfr,
+ )
+ changes_list += diff_items
+
+ return changes_list
+
+ def define_all_tasks(
+ self,
+ changes_list,
+ db_new_tasks,
+ tasks_by_target_record_id,
+ ):
+ """Function to create all the task structures obtanied from
+ the method calculate_all_differences_to_deploy
+
+ Args:
+ changes_list (List): ordered list of items to be created or deleted
+ db_new_tasks (List): tasks list to be created
+ action_id (str): action id
+ tasks_by_target_record_id (Dict[str, Any]):
+ [<target_record_id>, <task>]
+
+ """
+
+ for change in changes_list:
+ task = Ns._create_task(
+ deployment_info=change["deployment_info"],
+ target_id=change["target_id"],
+ item=change["item"],
+ action=change["action"],
+ target_record=change["target_record"],
+ target_record_id=change["target_record_id"],
+ extra_dict=change.get("extra_dict", None),
+ )
+
+ tasks_by_target_record_id[change["target_record_id"]] = task
+ db_new_tasks.append(task)
+
+ if change.get("common_id"):
+ task["common_id"] = change["common_id"]
+
+ def upload_all_tasks(
+ self,
+ db_new_tasks,
+ now,
+ ):
+ """Function to save all tasks in the common DB
+
+ Args:
+ db_new_tasks (List): tasks list to be created
+ now (time): current time
+
+ """
+
+ nb_ro_tasks = 0 # for logging
+
+ for db_task in db_new_tasks:
+ target_id = db_task.pop("target_id")
+ common_id = db_task.get("common_id")
+
+ if common_id:
+ if self.db.set_one(
+ "ro_tasks",
+ q_filter={
+ "target_id": target_id,
+ "tasks.common_id": common_id,
+ },
+ update_dict={"to_check_at": now, "modified_at": now},
+ push={"tasks": db_task},
+ fail_on_empty=False,
+ ):
+ continue
+
+ if not self.db.set_one(
+ "ro_tasks",
+ q_filter={
+ "target_id": target_id,
+ "tasks.target_record": db_task["target_record"],
+ },
+ update_dict={"to_check_at": now, "modified_at": now},
+ push={"tasks": db_task},
+ fail_on_empty=False,
+ ):
+ # Create a ro_task
+ self.logger.debug("Updating database, Creating ro_tasks")
+ db_ro_task = Ns._create_ro_task(target_id, db_task)
+ nb_ro_tasks += 1
+ self.db.create("ro_tasks", db_ro_task)
+
+ self.logger.debug(
+ "Created {} ro_tasks; {} tasks - db_new_tasks={}".format(
+ nb_ro_tasks, len(db_new_tasks), db_new_tasks
+ )
+ )
+
+ def deploy(self, session, indata, version, nsr_id, *args, **kwargs):
+ self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata))
+ validate_input(indata, deploy_schema)
+ action_id = indata.get("action_id", str(uuid4()))
+ task_index = 0
+ # get current deployment
+ db_nsr_update = {} # update operation on nsrs
+ db_vnfrs_update = {}
+ db_vnfrs = {} # vnf's info indexed by _id
+ step = ""
+ logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
+ self.logger.debug(logging_text + "Enter")
+
+ try:
+ step = "Getting ns and vnfr record from db"
+ db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
+ db_new_tasks = []
+ tasks_by_target_record_id = {}
+ # read from db: vnf's of this ns
+ step = "Getting vnfrs from db"
+ db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})