- if item == "nsrs":
- # in this case the imput descriptor is not the data to be stored
- ns_request = content
- content, _id = self.new_nsr(session, ns_request)
- transaction = {"_id": _id}
-
- self._validate_new_data(session, item_envelop, content)
- if item in ("nsds", "vnfds"):
- content = {"_admin": {"userDefinedData": content}}
- self._format_new_data(session, item, content, transaction)
- _id = self.db.create(item, content)
- if item == "nsrs":
- self.msg.write("ns", "create", _id)
- elif item == "vims":
- 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
+ def ns_action(self, session, nsInstanceId, action, indata, kwargs=None):
+ """
+ Performs a new action over a ns
+ :param session: contains the used login username and working project
+ :param nsInstanceId: _id of the nsr to perform the action
+ :param action: it can be: instantiate, terminate, action, TODO: update, heal
+ :param indata: descriptor with the parameters of the action
+ :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_" + action, new=True)
+ # get ns from nsr_id
+ nsr = self.get_item(session, "nsrs", nsInstanceId)
+ if nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
+ if action == "terminate" and indata.get("autoremove"):
+ # NSR must be deleted
+ return self.del_item(session, "nsrs", nsInstanceId)
+ if action != "instantiate":
+ raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
+ nsInstanceId, action), HTTPStatus.CONFLICT)
+ else:
+ if action == "instantiate" and not indata.get("force"):
+ raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format(
+ nsInstanceId, action), HTTPStatus.CONFLICT)
+ indata["nsInstanceId"] = nsInstanceId
+ # TODO
+ nslcmop = self.new_nslcmop(session, nsInstanceId, action, indata)
+ self._format_new_data(session, "nslcmops", nslcmop)
+ _id = self.db.create("nslcmops", nslcmop)
+ indata["_id"] = _id
+ self.msg.write("ns", action, 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)