+
+
+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):
+ BaseTopic.__init__(self, db, fs, msg)
+
+ def _check_nsi_operation(self, session, nsir, operation, indata):
+ """
+ Check that user has enter right parameters for the operation
+ :param session:
+ :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(nsId):
+ # TODO change to vnfR (??)
+ for ns in nstd["netslice-subnet"]:
+ if nsId == ns["id"]:
+ nsd_id = ns["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 nsId='{}' is not one of the "
+ "nst:netslice-subnet".format(nsId))
+ if operation == "instantiate":
+ # check the existance of netslice-subnet items
+ for in_nst in get_iterable(indata.get("netslice-subnet")):
+ nstd = check_valid_netslice_subnet_id(in_nst["nsdId"])
+
+ 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,
+ "nsInstance": "/osm/nsilcm/v1/netslice_instances/" + netsliceInstanceId,
+ }
+ }
+ return nsilcmop
+
+ def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False):
+ """
+ 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 the used login username and working project
+ :param indata: descriptor with the parameters of the operation. It must contains among others
+ nsiInstanceId: _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
+ :param force: If True avoid some dependence checks
+ :param make_public: Make the created item public to all projects
+ :return: id of the nslcmops
+ """
+ try:
+ # Override descriptor with query string kwargs
+ self._update_input_with_kwargs(indata, kwargs)
+ operation = indata["lcmOperationType"]
+ nsiInstanceId = indata["nsiInstanceId"]
+ validate_input(indata, self.operation_schema[operation])
+
+ # get nsi from nsiInstanceId
+ _filter = BaseTopic._get_project_filter(session, write=True, show_all=False)
+ _filter["_id"] = nsiInstanceId
+ nsir = self.db.get_one("nsis", _filter)
+
+ # 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 self.delete(session, nsiInstanceId)
+ if operation != "instantiate":
+ raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
+ nsiInstanceId, operation), HTTPStatus.CONFLICT)
+ else:
+ if operation == "instantiate" and not indata.get("force"):
+ raise EngineException("netslice_instance '{}' cannot be '{}' because it is already instantiated".
+ format(nsiInstanceId, operation), HTTPStatus.CONFLICT)
+
+ # Creating all the NS_operation (nslcmop)
+ # Get service list from db
+ nsrs_list = nsir["_admin"]["nsrs-detailed-list"]
+ nslcmops = []
+ for nsr_item in nsrs_list:
+ 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"] = nsiInstanceId
+ del indata_ns["key-pair-ref"]
+ nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg)
+ # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation
+ # message via kafka bus
+ nslcmop = nsi_NsLcmOpTopic.new(rollback, session, indata_ns, kwargs, headers, force, slice_object=True)
+ nslcmops.append(nslcmop)
+
+ # Creates nsilcmop
+ indata["nslcmops_ids"] = nslcmops
+ self._check_nsi_operation(session, nsir, operation, indata)
+ nsilcmop_desc = self._create_nsilcmop(session, nsiInstanceId, operation, indata)
+ self.format_on_new(nsilcmop_desc, session["project_id"], make_public=make_public)
+ _id = self.db.create("nsilcmops", nsilcmop_desc)
+ rollback.append({"topic": "nsilcmops", "_id": _id})
+ self.msg.write("nsi", operation, nsilcmop_desc)
+ return _id
+ except ValidationError as e:
+ raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
+ # except DbException as e:
+ # raise EngineException("Cannot get nsi_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
+
+ def delete(self, session, _id, force=False, dry_run=False):
+ raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
+
+ def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None):
+ raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)