X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fns.py;h=8328dba40afdc7adf5192ae571795aefd45958e5;hb=f5b188e14053b2246be507a23d9eef5ff7e42123;hp=f4366b23f5e50be735d652c7c9f0d4e7af6e587b;hpb=b66761bd5f0c65dcec418a27ebfd83a56eec7e91;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/ns.py b/osmclient/sol005/ns.py index f4366b2..8328dba 100644 --- a/osmclient/sol005/ns.py +++ b/osmclient/sol005/ns.py @@ -40,7 +40,7 @@ class Ns(object): ) # NS '--wait' option - def _wait(self, id, wait_time, deleteFlag=False): + def _wait(self, id, wait_time, deleteFlag=False, entity="NS"): self._logger.debug("") # Endpoint to get operation status apiUrlStatus = "{}{}{}".format( @@ -50,7 +50,7 @@ class Ns(object): if isinstance(wait_time, bool): wait_time = WaitForStatus.TIMEOUT_NS_OPERATION WaitForStatus.wait_for_status( - "NS", + entity, str(id), wait_time, apiUrlStatus, @@ -278,10 +278,7 @@ class Ns(object): ) headers = self._client._headers headers["Content-Type"] = "application/yaml" - http_header = [ - "{}: {}".format(key, val) for (key, val) in list(headers.items()) - ] - self._http.set_http_header(http_header) + self._http.set_http_header(headers) http_code, resp = self._http.post_cmd( endpoint=self._apiBase, postfields_dict=ns ) @@ -434,6 +431,33 @@ class Ns(object): message = "failed to exec operation {}:\nerror:\n{}".format(name, str(exc)) raise ClientException(message) + def cancel_op(self, operation_id, cancel_mode, wait=False): + """Cancels an LCM operation""" + self._client.get_token() + self._apiResource = "/ns_lcm_op_occs" + self._apiBase = "{}{}{}".format( + self._apiName, self._apiVersion, self._apiResource + ) + endpoint = "{}/{}/cancel".format(self._apiBase, operation_id) + op_data = {"cancelMode": cancel_mode} + try: + http_code, resp = self._http.post_cmd( + endpoint=endpoint, postfields_dict=op_data + ) + if http_code == 202: + if wait: + self._wait(operation_id, wait, deleteFlag=True, entity="OPCANCEL") + else: + print("Cancellation in progress") + else: + msg = resp or "" + raise ClientException(msg) + except ClientException as exc: + message = "failed to exec operation {}:\nerror:\n{}".format( + operation_id, str(exc) + ) + raise ClientException(message) + def scale_vnf( self, ns_name, @@ -471,6 +495,54 @@ class Ns(object): ) 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() @@ -541,7 +613,7 @@ class Ns(object): http_code, resp = self._http.get2_cmd( "{}/{}".format(self._apiBase, uuid) ) - if not uuid: + else: # if not uuid http_code, resp = self._http.get2_cmd( "{}/{}/{}/{}".format(self._apiBase, uuid, project_name, ns_id) ) @@ -619,3 +691,23 @@ class Ns(object): return nsr[field] raise NotFound("failed to find {} in ns {}".format(field, ns_name)) + + def heal( + self, + ns_name, + heal_dict, + wait=False, + timeout=None, + ): + """Heals a NS""" + self._logger.debug("") + self._client.get_token() + try: + op_data = heal_dict + if timeout: + op_data["timeout_ns_heal"] = timeout + op_id = self.exec_op(ns_name, op_name="heal", op_data=op_data, wait=wait) + print(str(op_id)) + except ClientException as exc: + message = "failed to heal ns {}:\nerror:\n{}".format(ns_name, str(exc)) + raise ClientException(message)