+ # Set NSD usageState
+ nsr = db_content
+ used_nsd_id = nsr.get("nsd-id")
+ if used_nsd_id:
+ # check if used by another NSR
+ nsrs_list = self.db.get_one("nsrs", {"nsd-id": used_nsd_id},
+ fail_on_empty=False, fail_on_more=False)
+ if not nsrs_list:
+ self.db.set_one("nsds", {"_id": used_nsd_id}, {"_admin.usageState": "NOT_IN_USE"})
+
+ # Set VNFD usageState
+ used_vnfd_id_list = nsr.get("vnfd-id")
+ if used_vnfd_id_list:
+ for used_vnfd_id in used_vnfd_id_list:
+ # check if used by another NSR
+ nsrs_list = self.db.get_one("nsrs", {"vnfd-id": used_vnfd_id},
+ fail_on_empty=False, fail_on_more=False)
+ if not nsrs_list:
+ self.db.set_one("vnfds", {"_id": used_vnfd_id}, {"_admin.usageState": "NOT_IN_USE"})
+
+ @staticmethod
+ def _format_ns_request(ns_request):
+ formated_request = copy(ns_request)
+ formated_request.pop("additionalParamsForNs", None)
+ formated_request.pop("additionalParamsForVnf", None)
+ return formated_request
+
+ @staticmethod
+ def _format_additional_params(ns_request, member_vnf_index=None, vdu_id=None, kdu_name=None, descriptor=None):
+ """
+ Get and format user additional params for NS or VNF
+ :param ns_request: User instantiation additional parameters
+ :param member_vnf_index: None for extract NS params, or member_vnf_index to extract VNF params
+ :param descriptor: If not None it check that needed parameters of descriptor are supplied
+ :return: tuple with a formatted copy of additional params or None if not supplied, plus other parameters
+ """
+ additional_params = None
+ other_params = None
+ if not member_vnf_index:
+ additional_params = copy(ns_request.get("additionalParamsForNs"))
+ where_ = "additionalParamsForNs"
+ elif ns_request.get("additionalParamsForVnf"):
+ where_ = "additionalParamsForVnf[member-vnf-index={}]".format(member_vnf_index)
+ item = next((x for x in ns_request["additionalParamsForVnf"] if x["member-vnf-index"] == member_vnf_index),
+ None)
+ if item:
+ if not vdu_id and not kdu_name:
+ other_params = item
+ additional_params = copy(item.get("additionalParams")) or {}
+ if vdu_id and item.get("additionalParamsForVdu"):
+ item_vdu = next((x for x in item["additionalParamsForVdu"] if x["vdu_id"] == vdu_id), None)
+ other_params = item_vdu
+ if item_vdu and item_vdu.get("additionalParams"):
+ where_ += ".additionalParamsForVdu[vdu_id={}]".format(vdu_id)
+ additional_params = item_vdu["additionalParams"]
+ if kdu_name:
+ additional_params = {}
+ if item.get("additionalParamsForKdu"):
+ item_kdu = next((x for x in item["additionalParamsForKdu"] if x["kdu_name"] == kdu_name), None)
+ other_params = item_kdu
+ if item_kdu and item_kdu.get("additionalParams"):
+ where_ += ".additionalParamsForKdu[kdu_name={}]".format(kdu_name)
+ additional_params = item_kdu["additionalParams"]
+
+ if additional_params:
+ for k, v in additional_params.items():
+ # BEGIN Check that additional parameter names are valid Jinja2 identifiers if target is not Kdu
+ if not kdu_name and not match('^[a-zA-Z_][a-zA-Z0-9_]*$', k):
+ raise EngineException("Invalid param name at {}:{}. Must contain only alphanumeric characters "
+ "and underscores, and cannot start with a digit"
+ .format(where_, k))
+ # END Check that additional parameter names are valid Jinja2 identifiers
+ if not isinstance(k, str):
+ raise EngineException("Invalid param at {}:{}. Only string keys are allowed".format(where_, k))
+ if "." in k or "$" in k:
+ raise EngineException("Invalid param at {}:{}. Keys must not contain dots or $".format(where_, k))
+ if isinstance(v, (dict, tuple, list)):
+ additional_params[k] = "!!yaml " + safe_dump(v)
+
+ if descriptor:
+ # check that enough parameters are supplied for the initial-config-primitive
+ # TODO: check for cloud-init
+ if member_vnf_index:
+ if kdu_name:
+ initial_primitives = None
+ elif vdu_id:
+ vdud = next(x for x in descriptor["vdu"] if x["id"] == vdu_id)
+ initial_primitives = deep_get(vdud, ("vdu-configuration", "initial-config-primitive"))
+ else:
+ initial_primitives = deep_get(descriptor, ("vnf-configuration", "initial-config-primitive"))
+ else:
+ initial_primitives = deep_get(descriptor, ("ns-configuration", "initial-config-primitive"))
+
+ for initial_primitive in get_iterable(initial_primitives):
+ for param in get_iterable(initial_primitive.get("parameter")):
+ if param["value"].startswith("<") and param["value"].endswith(">"):
+ if param["value"] in ("<rw_mgmt_ip>", "<VDU_SCALE_INFO>", "<ns_config_info>"):
+ continue
+ if not additional_params or param["value"][1:-1] not in additional_params:
+ raise EngineException("Parameter '{}' needed for vnfd[id={}]:vnf-configuration:"
+ "initial-config-primitive[name={}] not supplied".
+ format(param["value"], descriptor["id"],
+ initial_primitive["name"]))
+
+ return additional_params or None, other_params or None
+
+ def new(self, rollback, session, indata=None, kwargs=None, headers=None):