+ self._validate_new_data(session, item_envelop, content, force=force)
+ if item in ("nsds", "vnfds"):
+ content = {"_admin": {"userDefinedData": content}}
+ self._format_new_data(session, item, content)
+ _id = self.db.create(item, content)
+ rollback.insert(0, {"item": item, "_id": _id})
+
+ if item == "vim_accounts":
+ msg_data = self.db.get_one(item, {"_id": _id})
+ msg_data.pop("_admin", None)
+ self.msg.write("vim_account", "create", msg_data)
+ elif item == "sdns":
+ msg_data = self.db.get_one(item, {"_id": _id})
+ msg_data.pop("_admin", None)
+ self.msg.write("sdn", "create", msg_data)
+ return _id
+ except ValidationError as e:
+ raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
+
+ def new_nslcmop(self, session, nsInstanceId, operation, params):
+ now = time()
+ _id = str(uuid4())
+ nslcmop = {
+ "id": _id,
+ "_id": _id,
+ "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
+ "statusEnteredTime": now,
+ "nsInstanceId": nsInstanceId,
+ "lcmOperationType": operation,
+ "startTime": now,
+ "isAutomaticInvocation": False,
+ "operationParams": params,
+ "isCancelPending": False,
+ "links": {
+ "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
+ "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsInstanceId,
+ }
+ }
+ return nslcmop
+
+ def ns_operation(self, rollback, session, nsInstanceId, operation, indata, kwargs=None):
+ """
+ Performs a new operation over a ns
+ :param rollback: list where this method appends created items at database in case a rollback may to be done
+ :param session: contains the used login username and working project
+ :param nsInstanceId: _id of the nsr to perform the operation
+ :param operation: it can be: instantiate, terminate, action, TODO: update, heal
+ :param indata: descriptor with the parameters of the operation
+ :param kwargs: used to override the indata descriptor
+ :return: id of the nslcmops
+ """
+ try:
+ # Override descriptor with query string kwargs
+ self._update_descriptor(indata, kwargs)
+ validate_input(indata, "ns_" + operation, new=True)
+ # get ns from nsr_id
+ nsr = self.get_item(session, "nsrs", nsInstanceId)
+ if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
+ if operation == "terminate" and indata.get("autoremove"):
+ # NSR must be deleted
+ return self.del_item(session, "nsrs", nsInstanceId)
+ if operation != "instantiate":
+ raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
+ nsInstanceId, operation), HTTPStatus.CONFLICT)
+ else:
+ if operation == "instantiate" and not indata.get("force"):
+ raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format(
+ nsInstanceId, operation), HTTPStatus.CONFLICT)
+ indata["nsInstanceId"] = nsInstanceId
+ self._check_ns_operation(session, nsr, operation, indata)
+ nslcmop = self.new_nslcmop(session, nsInstanceId, operation, indata)
+ self._format_new_data(session, "nslcmops", nslcmop)
+ _id = self.db.create("nslcmops", nslcmop)
+ rollback.insert(0, {"item": "nslcmops", "_id": _id})
+ indata["_id"] = _id
+ self.msg.write("ns", operation, nslcmop)
+ return _id
+ except ValidationError as e:
+ raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
+ # except DbException as e:
+ # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)