+ def delete(self, session, _id, dry_run=False, not_send_msg=None):
+ raise EngineException(
+ "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR
+ )
+
+ def edit(self, session, _id, indata=None, kwargs=None, content=None):
+ raise EngineException(
+ "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
+ )
+
+
+class NsiTopic(BaseTopic):
+ topic = "nsis"
+ topic_msg = "nsi"
+ quota_name = "slice_instances"
+
+ def __init__(self, db, fs, msg, auth):
+ BaseTopic.__init__(self, db, fs, msg, auth)
+ self.nsrTopic = NsrTopic(db, fs, msg, auth)
+
+ @staticmethod
+ def _format_ns_request(ns_request):
+ formated_request = copy(ns_request)
+ # TODO: Add request params
+ return formated_request
+
+ @staticmethod
+ def _format_addional_params(slice_request):
+ """
+ Get and format user additional params for NS or VNF
+ :param slice_request: User instantiation additional parameters
+ :return: a formatted copy of additional params or None if not supplied
+ """
+ additional_params = copy(slice_request.get("additionalParamsForNsi"))
+ if additional_params:
+ for k, v in additional_params.items():
+ if not isinstance(k, str):
+ raise EngineException(
+ "Invalid param at additionalParamsForNsi:{}. Only string keys are allowed".format(
+ k
+ )
+ )
+ if "." in k or "$" in k:
+ raise EngineException(
+ "Invalid param at additionalParamsForNsi:{}. Keys must not contain dots or $".format(
+ k
+ )
+ )
+ if isinstance(v, (dict, tuple, list)):
+ additional_params[k] = "!!yaml " + safe_dump(v)
+ return additional_params
+
+ def _check_descriptor_dependencies(self, session, descriptor):
+ """
+ Check that the dependent descriptors exist on a new descriptor or edition
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param descriptor: descriptor to be inserted or edit
+ :return: None or raises exception
+ """
+ if not descriptor.get("nst-ref"):
+ return
+ nstd_id = descriptor["nst-ref"]
+ if not self.get_item_list(session, "nsts", {"id": nstd_id}):
+ raise EngineException(
+ "Descriptor error at nst-ref='{}' references a non exist nstd".format(
+ nstd_id
+ ),
+ http_code=HTTPStatus.CONFLICT,
+ )
+
+ def check_conflict_on_del(self, session, _id, db_content):
+ """
+ Check that NSI is not instantiated
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param _id: nsi internal id
+ :param db_content: The database content of the _id
+ :return: None or raises EngineException with the conflict
+ """
+ if session["force"]:
+ return
+ nsi = db_content
+ if nsi["_admin"].get("nsiState") == "INSTANTIATED":
+ raise EngineException(
+ "nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
+ "Launch 'terminate' operation first; or force deletion".format(_id),
+ http_code=HTTPStatus.CONFLICT,
+ )
+
+ def delete_extra(self, session, _id, db_content, not_send_msg=None):
+ """
+ Deletes associated nsilcmops from database. Deletes associated filesystem.
+ Set usageState of nst
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param _id: server internal id
+ :param db_content: The database content of the descriptor
+ :param not_send_msg: To not send message (False) or store content (list) instead
+ :return: None if ok or raises EngineException with the problem
+ """
+
+ # Deleting the nsrs belonging to nsir
+ nsir = db_content
+ for nsrs_detailed_item in nsir["_admin"]["nsrs-detailed-list"]:
+ nsr_id = nsrs_detailed_item["nsrId"]
+ if nsrs_detailed_item.get("shared"):
+ _filter = {
+ "_admin.nsrs-detailed-list.ANYINDEX.shared": True,
+ "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nsr_id,
+ "_id.ne": nsir["_id"],
+ }
+ nsi = self.db.get_one(
+ "nsis", _filter, fail_on_empty=False, fail_on_more=False
+ )
+ if nsi: # last one using nsr
+ continue
+ try:
+ self.nsrTopic.delete(
+ session, nsr_id, dry_run=False, not_send_msg=not_send_msg
+ )
+ except (DbException, EngineException) as e:
+ if e.http_code == HTTPStatus.NOT_FOUND:
+ pass
+ else:
+ raise
+
+ # delete related nsilcmops database entries
+ self.db.del_list("nsilcmops", {"netsliceInstanceId": _id})
+
+ # Check and set used NST usage state
+ nsir_admin = nsir.get("_admin")
+ if nsir_admin and nsir_admin.get("nst-id"):
+ # check if used by another NSI
+ nsis_list = self.db.get_one(
+ "nsis",
+ {"nst-id": nsir_admin["nst-id"]},
+ fail_on_empty=False,
+ fail_on_more=False,
+ )
+ if not nsis_list:
+ self.db.set_one(
+ "nsts",
+ {"_id": nsir_admin["nst-id"]},
+ {"_admin.usageState": "NOT_IN_USE"},
+ )
+
+ def new(self, rollback, session, indata=None, kwargs=None, headers=None):
+ """
+ Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs
+ :param rollback: list to append the created items at database in case a rollback must be done
+ :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
+ :param indata: params to be used for the nsir
+ :param kwargs: used to override the indata descriptor
+ :param headers: http request headers
+ :return: the _id of nsi descriptor created at database
+ """
+
+ try:
+ step = "checking quotas"
+ self.check_quota(session)
+
+ step = ""
+ slice_request = self._remove_envelop(indata)
+ # Override descriptor with query string kwargs
+ self._update_input_with_kwargs(slice_request, kwargs)
+ slice_request = self._validate_input_new(slice_request, session["force"])
+
+ # look for nstd
+ step = "getting nstd id='{}' from database".format(
+ slice_request.get("nstId")
+ )
+ _filter = self._get_project_filter(session)
+ _filter["_id"] = slice_request["nstId"]
+ nstd = self.db.get_one("nsts", _filter)
+ # check NST is not disabled
+ step = "checking NST operationalState"
+ if nstd["_admin"]["operationalState"] == "DISABLED":
+ raise EngineException(
+ "nst with id '{}' is DISABLED, and thus cannot be used to create a netslice "
+ "instance".format(slice_request["nstId"]),
+ http_code=HTTPStatus.CONFLICT,
+ )
+ del _filter["_id"]
+
+ # check NSD is not disabled
+ step = "checking operationalState"
+ if nstd["_admin"]["operationalState"] == "DISABLED":
+ raise EngineException(
+ "nst with id '{}' is DISABLED, and thus cannot be used to create "
+ "a network slice".format(slice_request["nstId"]),
+ http_code=HTTPStatus.CONFLICT,
+ )
+
+ nstd.pop("_admin", None)
+ nstd_id = nstd.pop("_id", None)
+ nsi_id = str(uuid4())
+ step = "filling nsi_descriptor with input data"
+
+ # Creating the NSIR
+ nsi_descriptor = {
+ "id": nsi_id,
+ "name": slice_request["nsiName"],
+ "description": slice_request.get("nsiDescription", ""),
+ "datacenter": slice_request["vimAccountId"],
+ "nst-ref": nstd["id"],
+ "instantiation_parameters": slice_request,
+ "network-slice-template": nstd,
+ "nsr-ref-list": [],
+ "vlr-list": [],
+ "_id": nsi_id,
+ "additionalParamsForNsi": self._format_addional_params(slice_request),
+ }
+
+ step = "creating nsi at database"
+ self.format_on_new(
+ nsi_descriptor, session["project_id"], make_public=session["public"]
+ )
+ nsi_descriptor["_admin"]["nsiState"] = "NOT_INSTANTIATED"
+ nsi_descriptor["_admin"]["netslice-subnet"] = None
+ nsi_descriptor["_admin"]["deployed"] = {}
+ nsi_descriptor["_admin"]["deployed"]["RO"] = []
+ nsi_descriptor["_admin"]["nst-id"] = nstd_id
+
+ # Creating netslice-vld for the RO.
+ step = "creating netslice-vld at database"
+
+ # Building the vlds list to be deployed
+ # From netslice descriptors, creating the initial list
+ nsi_vlds = []
+
+ for netslice_vlds in get_iterable(nstd.get("netslice-vld")):
+ # Getting template Instantiation parameters from NST
+ nsi_vld = deepcopy(netslice_vlds)
+ nsi_vld["shared-nsrs-list"] = []
+ nsi_vld["vimAccountId"] = slice_request["vimAccountId"]
+ nsi_vlds.append(nsi_vld)
+
+ nsi_descriptor["_admin"]["netslice-vld"] = nsi_vlds
+ # Creating netslice-subnet_record.
+ needed_nsds = {}
+ services = []
+
+ # Updating the nstd with the nsd["_id"] associated to the nss -> services list
+ for member_ns in nstd["netslice-subnet"]:
+ nsd_id = member_ns["nsd-ref"]
+ step = "getting nstd id='{}' constituent-nsd='{}' from database".format(
+ member_ns["nsd-ref"], member_ns["id"]
+ )
+ if nsd_id not in needed_nsds:
+ # Obtain nsd
+ _filter["id"] = nsd_id
+ nsd = self.db.get_one(
+ "nsds", _filter, fail_on_empty=True, fail_on_more=True
+ )
+ del _filter["id"]
+ nsd.pop("_admin")
+ needed_nsds[nsd_id] = nsd
+ else:
+ nsd = needed_nsds[nsd_id]
+ member_ns["_id"] = needed_nsds[nsd_id].get("_id")
+ services.append(member_ns)
+
+ step = "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format(
+ member_ns["nsd-ref"], member_ns["id"]
+ )
+
+ # creates Network Services records (NSRs)
+ step = "creating nsrs at database using NsrTopic.new()"
+ ns_params = slice_request.get("netslice-subnet")
+ nsrs_list = []
+ nsi_netslice_subnet = []
+ for service in services:
+ # Check if the netslice-subnet is shared and if it is share if the nss exists
+ _id_nsr = None
+ indata_ns = {}
+ # Is the nss shared and instantiated?
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.nsd-id"] = service[
+ "nsd-ref"
+ ]
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.nss-id"] = service["id"]
+ nsi = self.db.get_one(
+ "nsis", _filter, fail_on_empty=False, fail_on_more=False
+ )
+ if nsi and service.get("is-shared-nss"):
+ nsrs_detailed_list = nsi["_admin"]["nsrs-detailed-list"]
+ for nsrs_detailed_item in nsrs_detailed_list:
+ if nsrs_detailed_item["nsd-id"] == service["nsd-ref"]:
+ if nsrs_detailed_item["nss-id"] == service["id"]:
+ _id_nsr = nsrs_detailed_item["nsrId"]
+ break
+ for netslice_subnet in nsi["_admin"]["netslice-subnet"]:
+ if netslice_subnet["nss-id"] == service["id"]:
+ indata_ns = netslice_subnet
+ break
+ else:
+ indata_ns = {}
+ if service.get("instantiation-parameters"):
+ indata_ns = deepcopy(service["instantiation-parameters"])
+ # del service["instantiation-parameters"]
+
+ indata_ns["nsdId"] = service["_id"]
+ indata_ns["nsName"] = (
+ slice_request.get("nsiName") + "." + service["id"]
+ )
+ indata_ns["vimAccountId"] = slice_request.get("vimAccountId")
+ indata_ns["nsDescription"] = service["description"]
+ if slice_request.get("ssh_keys"):
+ indata_ns["ssh_keys"] = slice_request.get("ssh_keys")
+
+ if ns_params:
+ for ns_param in ns_params:
+ if ns_param.get("id") == service["id"]:
+ copy_ns_param = deepcopy(ns_param)
+ del copy_ns_param["id"]
+ indata_ns.update(copy_ns_param)
+ break
+
+ # Creates Nsr objects
+ _id_nsr, _ = self.nsrTopic.new(
+ rollback, session, indata_ns, kwargs, headers
+ )
+ nsrs_item = {
+ "nsrId": _id_nsr,
+ "shared": service.get("is-shared-nss"),
+ "nsd-id": service["nsd-ref"],
+ "nss-id": service["id"],
+ "nslcmop_instantiate": None,
+ }
+ indata_ns["nss-id"] = service["id"]
+ nsrs_list.append(nsrs_item)
+ nsi_netslice_subnet.append(indata_ns)
+ nsr_ref = {"nsr-ref": _id_nsr}
+ nsi_descriptor["nsr-ref-list"].append(nsr_ref)
+
+ # Adding the nsrs list to the nsi
+ nsi_descriptor["_admin"]["nsrs-detailed-list"] = nsrs_list
+ nsi_descriptor["_admin"]["netslice-subnet"] = nsi_netslice_subnet
+ self.db.set_one(
+ "nsts", {"_id": slice_request["nstId"]}, {"_admin.usageState": "IN_USE"}
+ )
+
+ # Creating the entry in the database
+ self.db.create("nsis", nsi_descriptor)
+ rollback.append({"topic": "nsis", "_id": nsi_id})
+ return nsi_id, None
+ except Exception as e: # TODO remove try Except, it is captured at nbi.py
+ self.logger.exception(
+ "Exception {} at NsiTopic.new()".format(e), exc_info=True
+ )
+ raise EngineException("Error {}: {}".format(step, e))
+ except ValidationError as e:
+ raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
+
+ 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:
+ _filter = self._get_project_filter(session)
+ _filter["id"] = nsd_id
+ nsds[nsd_id] = self.db.get_one("nsds", _filter)
+ 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)
+ logging_prefix = "nsi={} {} ".format(netsliceInstanceId, operation)
+ 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,
+ 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):
+ nsr_id = nsr_item["nsrId"]
+ if nsr_item.get("shared"):
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
+ _filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id
+ _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)
+ if (
+ nsi
+ ): # other nsi is using this nsr and it needs this nsr instantiated
+ continue # do not create nsilcmop
+ else: # instantiate
+ # looks the first nsi fulfilling the conditions but not being the current NSIR
+ if nsi:
+ nsi_nsr_item = next(
+ n
+ for n in nsi["_admin"]["nsrs-detailed-list"]
+ if n["nsrId"] == nsr_id
+ and n["shared"]
+ and n["nslcmop_instantiate"]
+ )
+ 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)
+ # continue to not create nslcmop since nsrs is shared and nsrs was created
+ continue
+ else:
+ self.add_shared_nsr_2vld(nsir, nsr_item)
+
+ # create operation
+ try:
+ indata_ns = {
+ "lcmOperationType": operation,
+ "nsInstanceId": nsr_id,
+ # Including netslice_id in the ns instantiate Operation
+ "netsliceInstanceId": netsliceInstanceId,
+ }
+ if operation == "instantiate":
+ service = self.db.get_one("nsrs", {"_id": nsr_id})
+ indata_ns.update(service["instantiate_params"])
+
+ # 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, None, headers, slice_object=True
+ )
+ nslcmops.append(nslcmop)
+ if operation == "instantiate":
+ _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(
+ logging_prefix
+ + "skipping NS={} because not found".format(nsr_id)
+ )
+ 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, not_send_msg=None):
+ raise EngineException(
+ "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR
+ )