+ def edit(self, session, _id, indata=None, kwargs=None, content=None):
+ raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
+
+
+class NsiLcmOpTopic(BaseTopic):
+ topic = "nsilcmops"
+ topic_msg = "nsi"
+ operation_schema = { # mapping between operation and jsonschema to validate
+ "instantiate": nsi_instantiate,
+ "terminate": None
+ }
+
+ def __init__(self, db, fs, msg, auth):
+ BaseTopic.__init__(self, db, fs, msg, auth)
+ self.nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg, self.auth)
+
+ def _check_nsi_operation(self, session, nsir, operation, indata):
+ """
+ Check that user has enter right parameters for the operation
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param operation: it can be: instantiate, terminate, action, TODO: update, heal
+ :param indata: descriptor with the parameters of the operation
+ :return: None
+ """
+ nsds = {}
+ nstd = nsir["network-slice-template"]
+
+ def check_valid_netslice_subnet_id(nstId):
+ # TODO change to vnfR (??)
+ for netslice_subnet in nstd["netslice-subnet"]:
+ if nstId == netslice_subnet["id"]:
+ nsd_id = netslice_subnet["nsd-ref"]
+ if nsd_id not in nsds:
+ nsds[nsd_id] = self.db.get_one("nsds", {"id": nsd_id})
+ return nsds[nsd_id]
+ else:
+ raise EngineException("Invalid parameter nstId='{}' is not one of the "
+ "nst:netslice-subnet".format(nstId))
+ if operation == "instantiate":
+ # check the existance of netslice-subnet items
+ for in_nst in get_iterable(indata.get("netslice-subnet")):
+ check_valid_netslice_subnet_id(in_nst["id"])
+
+ def _create_nsilcmop(self, session, netsliceInstanceId, operation, params):
+ now = time()
+ _id = str(uuid4())
+ nsilcmop = {
+ "id": _id,
+ "_id": _id,
+ "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
+ "statusEnteredTime": now,
+ "netsliceInstanceId": netsliceInstanceId,
+ "lcmOperationType": operation,
+ "startTime": now,
+ "isAutomaticInvocation": False,
+ "operationParams": params,
+ "isCancelPending": False,
+ "links": {
+ "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id,
+ "netsliceInstanceId": "/osm/nsilcm/v1/netslice_instances/" + netsliceInstanceId,
+ }
+ }
+ return nsilcmop
+
+ def add_shared_nsr_2vld(self, nsir, nsr_item):
+ for nst_sb_item in nsir["network-slice-template"].get("netslice-subnet"):
+ if nst_sb_item.get("is-shared-nss"):
+ for admin_subnet_item in nsir["_admin"].get("netslice-subnet"):
+ if admin_subnet_item["nss-id"] == nst_sb_item["id"]:
+ for admin_vld_item in nsir["_admin"].get("netslice-vld"):
+ for admin_vld_nss_cp_ref_item in admin_vld_item["nss-connection-point-ref"]:
+ if admin_subnet_item["nss-id"] == admin_vld_nss_cp_ref_item["nss-ref"]:
+ if not nsr_item["nsrId"] in admin_vld_item["shared-nsrs-list"]:
+ admin_vld_item["shared-nsrs-list"].append(nsr_item["nsrId"])
+ break
+ # self.db.set_one("nsis", {"_id": nsir["_id"]}, nsir)
+ self.db.set_one("nsis", {"_id": nsir["_id"]}, {"_admin.netslice-vld": nsir["_admin"].get("netslice-vld")})
+
+ def new(self, rollback, session, indata=None, kwargs=None, headers=None):
+ """
+ Performs a new operation over a ns
+ :param rollback: list to append created items at database in case a rollback must to be done
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param indata: descriptor with the parameters of the operation. It must contains among others
+ netsliceInstanceId: _id of the nsir to perform the operation
+ operation: it can be: instantiate, terminate, action, TODO: update, heal
+ :param kwargs: used to override the indata descriptor
+ :param headers: http request headers
+ :return: id of the nslcmops
+ """
+ try:
+ # Override descriptor with query string kwargs
+ self._update_input_with_kwargs(indata, kwargs)
+ operation = indata["lcmOperationType"]
+ netsliceInstanceId = indata["netsliceInstanceId"]
+ validate_input(indata, self.operation_schema[operation])
+
+ # get nsi from netsliceInstanceId
+ _filter = self._get_project_filter(session)
+ _filter["_id"] = netsliceInstanceId
+ nsir = self.db.get_one("nsis", _filter)
+ del _filter["_id"]
+
+ # initial checking
+ if not nsir["_admin"].get("nsiState") or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
+ if operation == "terminate" and indata.get("autoremove"):
+ # NSIR must be deleted
+ return None # a none in this case is used to indicate not instantiated. It can be removed
+ if operation != "instantiate":
+ raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
+ netsliceInstanceId, operation), HTTPStatus.CONFLICT)
+ else:
+ if operation == "instantiate" and not session["force"]:
+ raise EngineException("netslice_instance '{}' cannot be '{}' because it is already instantiated".
+ format(netsliceInstanceId, operation), HTTPStatus.CONFLICT)
+
+ # Creating all the NS_operation (nslcmop)
+ # Get service list from db
+ nsrs_list = nsir["_admin"]["nsrs-detailed-list"]
+ nslcmops = []
+ # nslcmops_item = None
+ for index, nsr_item in enumerate(nsrs_list):
+ nsi = None
+ if nsr_item.get("shared"):
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_item["nsrId"]
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.nslcmop_instantiate.ne"] = None
+ _filter["_id.ne"] = netsliceInstanceId
+ nsi = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
+ if operation == "terminate":
+ _update = {"_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(index): None}
+ self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
+
+ # looks the first nsi fulfilling the conditions but not being the current NSIR
+ if nsi:
+ nsi_admin_shared = nsi["_admin"]["nsrs-detailed-list"]
+ for nsi_nsr_item in nsi_admin_shared:
+ if nsi_nsr_item["nsd-id"] == nsr_item["nsd-id"] and nsi_nsr_item["shared"]:
+ self.add_shared_nsr_2vld(nsir, nsr_item)
+ nslcmops.append(nsi_nsr_item["nslcmop_instantiate"])
+ _update = {"_admin.nsrs-detailed-list.{}".format(index): nsi_nsr_item}
+ self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
+ break
+ # continue to not create nslcmop since nsrs is shared and nsrs was created
+ continue
+ else:
+ self.add_shared_nsr_2vld(nsir, nsr_item)
+
+ try:
+ service = self.db.get_one("nsrs", {"_id": nsr_item["nsrId"]})
+ indata_ns = {}
+ indata_ns = service["instantiate_params"]
+ indata_ns["lcmOperationType"] = operation
+ indata_ns["nsInstanceId"] = service["_id"]
+ # Including netslice_id in the ns instantiate Operation
+ indata_ns["netsliceInstanceId"] = netsliceInstanceId
+ # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation
+ # message via kafka bus
+ nslcmop, _ = self.nsi_NsLcmOpTopic.new(rollback, session, indata_ns, kwargs, headers,
+ slice_object=True)
+ nslcmops.append(nslcmop)
+ if operation == "terminate":
+ nslcmop = None
+ _update = {"_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(index): nslcmop}
+ self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
+ except (DbException, EngineException) as e:
+ if e.http_code == HTTPStatus.NOT_FOUND:
+ self.logger.info("HTTPStatus.NOT_FOUND")
+ pass
+ else:
+ raise
+
+ # Creates nsilcmop
+ indata["nslcmops_ids"] = nslcmops
+ self._check_nsi_operation(session, nsir, operation, indata)
+
+ nsilcmop_desc = self._create_nsilcmop(session, netsliceInstanceId, operation, indata)
+ self.format_on_new(nsilcmop_desc, session["project_id"], make_public=session["public"])
+ _id = self.db.create("nsilcmops", nsilcmop_desc)
+ rollback.append({"topic": "nsilcmops", "_id": _id})
+ self.msg.write("nsi", operation, nsilcmop_desc)
+ return _id, None
+ except ValidationError as e:
+ raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
+
+ def delete(self, session, _id, dry_run=False):
+ raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
+
+ def edit(self, session, _id, indata=None, kwargs=None, content=None):