+ rollback = []
+ step = ""
+ try:
+ # look for nsr
+ step = "getting nsd id='{}' from database".format(ns_request.get("nsdId"))
+ nsd = self.get_item(session, "nsds", ns_request["nsdId"])
+ nsr_id = str(uuid4())
+ now = time()
+ step = "filling nsr from input data"
+ nsr_descriptor = {
+ "name": ns_request["nsName"],
+ "name-ref": ns_request["nsName"],
+ "short-name": ns_request["nsName"],
+ "admin-status": "ENABLED",
+ "nsd": nsd,
+ "datacenter": ns_request["vimAccountId"],
+ "resource-orchestrator": "osmopenmano",
+ "description": ns_request.get("nsDescription", ""),
+ "constituent-vnfr-ref": [],
+
+ "operational-status": "init", # typedef ns-operational-
+ "config-status": "init", # typedef config-states
+ "detailed-status": "scheduled",
+
+ "orchestration-progress": {},
+ # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}},
+
+ "crete-time": now,
+ "nsd-name-ref": nsd["name"],
+ "operational-events": [], # "id", "timestamp", "description", "event",
+ "nsd-ref": nsd["id"],
+ "instantiate_params": ns_request,
+ "ns-instance-config-ref": nsr_id,
+ "id": nsr_id,
+ "_id": nsr_id,
+ # "input-parameter": xpath, value,
+ "ssh-authorized-key": ns_request.get("key-pair-ref"),
+ }
+ ns_request["nsr_id"] = nsr_id
+
+ # Create VNFR
+ needed_vnfds = {}
+ for member_vnf in nsd["constituent-vnfd"]:
+ vnfd_id = member_vnf["vnfd-id-ref"]
+ step = "getting vnfd id='{}' constituent-vnfd='{}' from database".format(
+ member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
+ if vnfd_id not in needed_vnfds:
+ # Obtain vnfd
+ vnf_filter = {"id": vnfd_id}
+ self._add_read_filter(session, "vnfds", vnf_filter)
+ vnfd = self.db.get_one("vnfds", vnf_filter)
+ vnfd.pop("_admin")
+ needed_vnfds[vnfd_id] = vnfd
+ else:
+ vnfd = needed_vnfds[vnfd_id]
+ step = "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format(
+ member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
+ vnfr_id = str(uuid4())
+ vnfr_descriptor = {
+ "id": vnfr_id,
+ "_id": vnfr_id,
+ "nsr-id-ref": nsr_id,
+ "member-vnf-index-ref": member_vnf["member-vnf-index"],
+ "created-time": now,
+ # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise
+ "vnfd-ref": vnfd_id,
+ "vnfd-id": vnfr_id, # not at OSM model, but useful
+ "vim-account-id": None,
+ "vdur": [],
+ "connection-point": [],
+ "ip-address": None, # mgmt-interface filled by LCM
+ }
+ for cp in vnfd.get("connection-point", ()):
+ vnf_cp = {
+ "name": cp["name"],
+ "connection-point-id": cp.get("id"),
+ "id": cp.get("id"),
+ # "ip-address", "mac-address" # filled by LCM
+ # vim-id # TODO it would be nice having a vim port id
+ }
+ vnfr_descriptor["connection-point"].append(vnf_cp)
+ for vdu in vnfd["vdu"]:
+ vdur_id = str(uuid4())
+ vdur = {
+ "id": vdur_id,
+ "vdu-id-ref": vdu["id"],
+ "ip-address": None, # mgmt-interface filled by LCM
+ # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM
+ "internal-connection-point": [],
+ }
+ # TODO volumes: name, volume-id
+ for icp in vdu.get("internal-connection-point", ()):
+ vdu_icp = {
+ "id": icp["id"],
+ "connection-point-id": icp["id"],
+ "name": icp.get("name"),
+ # "ip-address", "mac-address" # filled by LCM
+ # vim-id # TODO it would be nice having a vim port id
+ }
+ vdur["internal-connection-point"].append(vdu_icp)
+ vnfr_descriptor["vdur"].append(vdur)
+
+ step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format(
+ member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
+ self._format_new_data(session, "vnfrs", vnfr_descriptor)
+ self.db.create("vnfrs", vnfr_descriptor)
+ rollback.append({"session": session, "item": "vnfrs", "_id": vnfr_id, "force": True})
+ nsr_descriptor["constituent-vnfr-ref"].append(vnfr_id)
+
+ step = "creating nsr at database"
+ self._format_new_data(session, "nsrs", nsr_descriptor)
+ self.db.create("nsrs", nsr_descriptor)
+ return nsr_id
+ except Exception as e:
+ raise EngineException("Error {}: {}".format(step, e))
+ for rollback_item in rollback:
+ try:
+ self.engine.del_item(**rollback)
+ except Exception as e2:
+ self.logger.error("Rollback Exception {}: {}".format(rollback, e2))