+ ns = self.get(name)
+ self._apiResource = "/ns_instances"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ endpoint = "{}/{}/{}".format(self._apiBase, ns["_id"], op_name)
+ # print('OP_NAME: {}'.format(op_name))
+ # print('OP_DATA: {}'.format(json.dumps(op_data)))
+ http_code, resp = self._http.post_cmd(
+ endpoint=endpoint, postfields_dict=op_data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ if resp:
+ resp = json.loads(resp)
+ if not resp or "id" not in resp:
+ raise ClientException(
+ "unexpected response from server - {}".format(resp)
+ )
+ if wait:
+ # Wait for status for NS instance action
+ # For the 'action' operation, 'id' is used
+ self._wait(resp.get("id"), wait)
+ return resp["id"]
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException(msg)
+ except ClientException as exc:
+ message = "failed to exec operation {}:\nerror:\n{}".format(name, str(exc))
+ raise ClientException(message)
+
+ def scale_vnf(
+ self,
+ ns_name,
+ vnf_name,
+ scaling_group,
+ scale_in,
+ scale_out,
+ wait=False,
+ timeout=None,
+ ):
+ """Scales a VNF by adding/removing VDUs"""
+ self._logger.debug("")
+ self._client.get_token()
+ try:
+ op_data = {}
+ op_data["scaleType"] = "SCALE_VNF"
+ op_data["scaleVnfData"] = {}
+ if scale_in and not scale_out:
+ op_data["scaleVnfData"]["scaleVnfType"] = "SCALE_IN"
+ elif not scale_in and scale_out:
+ op_data["scaleVnfData"]["scaleVnfType"] = "SCALE_OUT"
+ else:
+ raise ClientException("you must set either 'scale_in' or 'scale_out'")
+ op_data["scaleVnfData"]["scaleByStepData"] = {
+ "member-vnf-index": vnf_name,
+ "scaling-group-descriptor": scaling_group,
+ }
+ if timeout:
+ op_data["timeout_ns_scale"] = timeout
+ op_id = self.exec_op(ns_name, op_name="scale", op_data=op_data, wait=wait)
+ print(str(op_id))
+ except ClientException as exc:
+ message = "failed to scale vnf {} of ns {}:\nerror:\n{}".format(
+ vnf_name, ns_name, str(exc)
+ )
+ raise ClientException(message)
+
+ def update(self, ns_name, data, wait=False):
+ """Update NS instance.
+
+ This function calls the NBI in order to perform an update operation
+ on a Network Service instance.
+
+ Args:
+ ns_name: (str)
+ data: (dict)
+ wait: (boolean)
+
+ Returns:
+ None
+
+ """
+ self._logger.debug("")
+ self._client.get_token()
+ try:
+ op_data = {"updateType": data.pop("updateType")}
+
+ # Check update parameters availability according to update type
+ if op_data["updateType"] == "CHANGE_VNFPKG":
+ if not (
+ data["config"]["changeVnfPackageData"][0].get("vnfInstanceId")
+ and data["config"]["changeVnfPackageData"][0].get("vnfdId")
+ ):
+ raise ClientException("you must set both vnfInstanceId and vnfdId")
+
+ # Fill up op_data
+ op_data["changeVnfPackageData"] = {}
+ op_data["changeVnfPackageData"]["vnfInstanceId"] = data["config"][
+ "changeVnfPackageData"
+ ][0].get("vnfInstanceId")
+
+ op_data["changeVnfPackageData"]["vnfdId"] = data["config"][
+ "changeVnfPackageData"
+ ][0].get("vnfdId")
+
+ if data.get("timeout"):
+ op_data["timeout_ns_update"] = data["timeout"]
+
+ op_id = self.exec_op(ns_name, op_name="update", op_data=op_data, wait=wait)
+ print(str(op_id))
+
+ except ClientException as exc:
+ message = "failed to update ns {}:\nerror:\n{}".format(ns_name, str(exc))
+ raise ClientException(message)
+
+ def create_alarm(self, alarm):
+ self._logger.debug("")
+ self._client.get_token()
+ data = {}
+ data["create_alarm_request"] = {}
+ data["create_alarm_request"]["alarm_create_request"] = alarm
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint="/test/message/alarm_request", postfields_dict=data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ # resp = json.loads(resp)
+ print("Alarm created")
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException('error: code: {}, resp: {}'.format(
+ # http_code, msg))
+ except ClientException as exc:
+ message = "failed to create alarm: alarm {}\n{}".format(alarm, str(exc))
+ raise ClientException(message)
+
+ def delete_alarm(self, name):
+ self._logger.debug("")
+ self._client.get_token()
+ data = {}
+ data["delete_alarm_request"] = {}
+ data["delete_alarm_request"]["alarm_delete_request"] = {}
+ data["delete_alarm_request"]["alarm_delete_request"]["alarm_uuid"] = name
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint="/test/message/alarm_request", postfields_dict=data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ # resp = json.loads(resp)
+ print("Alarm deleted")
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException('error: code: {}, resp: {}'.format(
+ # http_code, msg))
+ except ClientException as exc:
+ message = "failed to delete alarm: alarm {}\n{}".format(name, str(exc))
+ raise ClientException(message)
+
+ def get_alarm(self, project_name=None, ns_id=None, uuid=None):
+ self._client.get_token()
+ try:
+ self._apiName = "/nsfm"
+ self._apiResource = "/alarms"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ if uuid:
+ # if request is for any uuid
+ http_code, resp = self._http.get2_cmd(
+ "{}/{}".format(self._apiBase, uuid)
+ )
+ if not uuid:
+ http_code, resp = self._http.get2_cmd(
+ "{}/{}/{}/{}".format(self._apiBase, uuid, project_name, ns_id)
+ )
+ if http_code == 200:
+ if resp:
+ resp = json.loads(resp)
+ return resp
+ else:
+ raise ClientException("unexpected response from server")