+@cli_osm.command(
+ name="ns-update", short_help="executes an update of a Network Service."
+)
+@click.argument("ns_name")
+@click.option(
+ "--updatetype", required=True, type=str, help="available types: CHANGE_VNFPKG"
+)
+@click.option(
+ "--config",
+ required=True,
+ type=str,
+ help="extra information for update operation as YAML/JSON inline string as --config"
+ " '{changeVnfPackageData:[{vnfInstanceId: xxx, vnfdId: yyy}]}'",
+)
+@click.option(
+ "--timeout", required=False, default=None, type=int, help="timeout in seconds"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def update(ctx, ns_name, updatetype, config, timeout, wait):
+ """Executes an update of a Network Service.
+
+ The update will check new revisions of the Network Functions that are part of the
+ Network Service, and it will update them if needed.
+ Sample update command: osm ns-update ns_instance_id --updatetype CHANGE_VNFPKG
+ --config '{changeVnfPackageData: [{vnfInstanceId: id_x,vnfdId: id_y}]}' --timeout 300 --wait
+
+ NS_NAME: Network service instance name or ID.
+
+ """
+ op_data = {
+ "timeout": timeout,
+ "updateType": updatetype,
+ }
+ if config:
+ op_data["config"] = yaml.safe_load(config)
+
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.ns.update(ns_name, op_data, wait=wait)
+
+
+def process_common_heal_params(ctx, param, value):
+ logger.debug("")
+ if not value:
+ return
+ logger.debug(f"Param name: {param.name}")
+ if not ctx.params.get("heal_params", {}).get("healVnfData"):
+ raise ClientException(f"Expected option --vnf before {param.name}")
+ if param.name == "cause":
+ param_dict = ctx.params["heal_params"]["healVnfData"][-1]["cause"] = value
+ return
+ # If not "vnf" and not "cause", then the param lies on "additionalParams"
+ if not ctx.params["heal_params"]["healVnfData"][-1].get("additionalParams"):
+ ctx.params["heal_params"]["healVnfData"][-1]["additionalParams"] = {}
+ if param.name == "vdu":
+ # Check VDU id ?
+ if not ctx.params["heal_params"]["healVnfData"][-1]["additionalParams"].get(
+ "vdu"
+ ):
+ ctx.params["heal_params"]["healVnfData"][-1]["additionalParams"][
+ "vdu"
+ ] = []
+ vdu = {"vdu-id": value}
+ ctx.params["heal_params"]["healVnfData"][-1]["additionalParams"][
+ "vdu"
+ ].append(vdu)
+ ctx.params["heal_params"]["current_item"] = "vdu"
+ else:
+ current_item = ctx.params["heal_params"]["current_item"]
+ if current_item == "vnf":
+ param_dict = ctx.params["heal_params"]["healVnfData"][-1][
+ "additionalParams"
+ ]
+ else:
+ # if current_item == "vdu":
+ param_dict = ctx.params["heal_params"]["healVnfData"][-1][
+ "additionalParams"
+ ]["vdu"][-1]
+ if param.name == "count_index":
+ param_name = "count-index"
+ elif param.name == "run_day1":
+ param_name = "run-day1"
+ else:
+ param_name = param.name
+ param_dict[param_name] = value
+ return
+
+
+def process_ns_heal_params(ctx, param, value):
+ logger.debug("")
+ if not ctx.params.get("heal_params"):
+ ctx.params["heal_params"] = {}
+ ctx.params["heal_params"]["healVnfData"] = []
+ if param.name == "vnf" and value:
+ # Check VNF id ?
+ logger.debug(f"Param name: {param.name}")
+ vnf = {"vnfInstanceId": value}
+ ctx.params["heal_params"]["healVnfData"].append(vnf)
+ ctx.params["heal_params"]["current_item"] = "vnf"
+ else:
+ process_common_heal_params(ctx, param, value)
+
+
+def process_vnf_heal_params(ctx, param, value):
+ logger.debug("")
+ if not ctx.params.get("heal_params"):
+ ctx.params["heal_params"] = {}
+ ctx.params["heal_params"]["healVnfData"] = []
+ vnf = {"vnfInstanceId": "id_to_be_substituted"}
+ ctx.params["heal_params"]["healVnfData"].append(vnf)
+ ctx.params["heal_params"]["current_item"] = "vnf"
+ else:
+ process_common_heal_params(ctx, param, value)
+
+
+@click.command(
+ name="ns-heal", short_help="heals (recreates) VNFs or VDUs of a NS instance"
+)
+@click.argument("ns_name")
+@click.option(
+ "--vnf",
+ required=True,
+ default=None,
+ callback=process_ns_heal_params,
+ help="vnf-id if the target is a vnf instead of a ns)",
+)
+@click.option(
+ "--cause",
+ default=None,
+ callback=process_ns_heal_params,
+ help="human readable cause of the healing",
+)
+@click.option(
+ "--run-day1",
+ is_flag=True,
+ default=False,
+ callback=process_ns_heal_params,
+ help="indicates whether or not to run day1 primitives for the VNF/VDU",
+)
+@click.option(
+ "--vdu",
+ default=None,
+ callback=process_ns_heal_params,
+ help="vdu-id",
+)
+@click.option(
+ "--count-index",
+ type=int,
+ default=None,
+ callback=process_ns_heal_params,
+ help="count-index",
+)
+@click.option(
+ "--timeout",
+ type=int,
+ default=None,
+ help="timeout in seconds"
+)
+@click.option(
+ "--wait",
+ is_flag=True,
+ default=False,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def ns_heal(
+ ctx,
+ ns_name,
+ heal_params,
+ cause,
+ vnf,
+ run_day1,
+ vdu,
+ count_index,
+ wait,
+ timeout,
+):
+ """heals (recreates) VNFs or VDUs of a NS instance
+
+ NS_NAME: name or ID of the NS instance
+ """
+ logger.debug("")
+ heal_dict = ctx.params["heal_params"]
+ heal_dict.pop("current_item")
+ if cause:
+ heal_dict["cause"] = cause
+ logger.debug(f"Heal dict: {heal_dict}")
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.ns.heal(ns_name, heal_dict, wait, timeout)
+
+
+@click.command(
+ name="vnf-heal",
+ short_help="heals (recreates) a VNF instance or the VDUs of a VNF instance",
+)
+@click.argument("vnf_name")
+@click.option(
+ "--cause",
+ default=None,
+ callback=process_vnf_heal_params,
+ help="human readable cause of the healing",
+)
+@click.option(
+ "--run-day1",
+ is_flag=True,
+ default=False,
+ callback=process_vnf_heal_params,
+ help="indicates whether or not to run day1 primitives for the VNF/VDU",
+)
+@click.option(
+ "--vdu",
+ default=None,
+ callback=process_vnf_heal_params,
+ help="vdu-id",
+)
+@click.option(
+ "--count-index",
+ type=int,
+ default=None,
+ callback=process_vnf_heal_params,
+ help="count-index",
+)
+@click.option(
+ "--timeout",
+ type=int,
+ default=None,
+ help="timeout in seconds"
+)
+@click.option(
+ "--wait",
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def vnf_heal(
+ ctx,
+ vnf_name,
+ heal_params,
+ cause,
+ run_day1,
+ vdu,
+ count_index,
+ wait,
+ timeout,
+):
+ """heals (recreates) a VNF instance or the VDUs of a VNF instance
+
+ VNF_NAME: name or ID of the VNF instance
+ """
+ logger.debug("")
+ heal_dict = ctx.params["heal_params"]
+ heal_dict.pop("current_item")
+ heal_dict["healVnfData"][-1]["vnfInstanceId"] = vnf_name
+ logger.debug(f"Heal dict: {heal_dict}")
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.vnf.heal(vnf_name, heal_dict, wait, timeout)
+
+
+@cli_osm.command(name="alarm-show", short_help="show alarm details")
+@click.argument("uuid")
+@click.pass_context
+def alarm_show(ctx, uuid):
+ """Show alarm's detail information"""
+
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.ns.get_alarm(uuid=uuid)
+ alarm_filter = [
+ "uuid",
+ "name",
+ "metric",
+ "statistic",
+ "threshold",
+ "operation",
+ "ns-id",
+ "vnf-id",
+ "vdu_name",
+ "action",
+ "status",
+ ]
+ table = PrettyTable(["key", "attribute"])
+ try:
+ # Arrange and return the response data
+ alarm = resp.replace("ObjectId", "")
+ for key in alarm_filter:
+ if key == "uuid":
+ value = alarm.get(key)
+ key = "alarm-id"
+ elif key == "name":
+ value = alarm.get(key)
+ key = "alarm-name"
+ elif key == "ns-id":
+ value = alarm["tags"].get("ns_id")
+ elif key == "vdu_name":
+ value = alarm["tags"].get("vdu_name")
+ elif key == "status":
+ value = alarm["alarm_status"]
+ else:
+ value = alarm[key]
+ table.add_row([key, wrap_text(text=json.dumps(value, indent=2), width=100)])
+ table.align = "l"
+ print(table)
+ except Exception:
+ print(resp)
+
+
+# List alarm
+@cli_osm.command(name="alarm-list", short_help="list all alarms")
+@click.option(
+ "--ns_id", default=None, required=False, help="List out alarm for given ns id"
+)
+@click.pass_context
+def alarm_list(ctx, ns_id):
+ """list all alarm"""
+
+ check_client_version(ctx.obj, ctx.command.name)
+ project_name = os.getenv("OSM_PROJECT", "admin")
+ resp = ctx.obj.ns.get_alarm(project_name=project_name, ns_id=ns_id)
+
+ table = PrettyTable(
+ ["alarm-id", "metric", "threshold", "operation", "action", "status"]
+ )
+ if resp:
+ # return the response data in a table
+ resp = resp.replace("ObjectId", "")
+ for alarm in resp:
+ table.add_row(
+ [
+ wrap_text(text=str(alarm["uuid"]), width=38),
+ alarm["metric"],
+ alarm["threshold"],
+ alarm["operation"],
+ wrap_text(text=alarm["action"], width=25),
+ alarm["alarm_status"],
+ ]
+ )
+ table.align = "l"
+ print(table)
+
+
+# Update alarm
+@cli_osm.command(name="alarm-update", short_help="Update a alarm")
+@click.argument("uuid")
+@click.option("--threshold", default=None, help="Alarm threshold")
+@click.option("--is_enable", default=None, type=bool, help="enable or disable alarm")
+@click.pass_context
+def alarm_update(ctx, uuid, threshold, is_enable):
+ """
+ Update alarm
+
+ """
+ if not threshold and is_enable is None:
+ raise ClientException(
+ "Please provide option to update i.e threshold or is_enable"
+ )
+ ctx.obj.ns.update_alarm(uuid, threshold, is_enable)
+
+