From d9bcc2d0990649a678e3e6229a196544754cc886 Mon Sep 17 00:00:00 2001 From: jegan Date: Thu, 16 May 2024 05:42:05 +0000 Subject: [PATCH] Bug 2376 fixed: Performing NS-Update through CLI Change-Id: I835f37e514aa971df36a11b7da73932259b57df0 Signed-off-by: jegan --- osmclient/cli_commands/nslcm.py | 173 +++++++++++++++++++++++++++----- osmclient/sol005/ns.py | 42 +++++--- 2 files changed, 175 insertions(+), 40 deletions(-) diff --git a/osmclient/cli_commands/nslcm.py b/osmclient/cli_commands/nslcm.py index d206db2..28eaedd 100755 --- a/osmclient/cli_commands/nslcm.py +++ b/osmclient/cli_commands/nslcm.py @@ -139,17 +139,82 @@ def vnf_scale( ) -@click.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" +def process_update_params(ctx, param, value): + update_dict = {} + args = value + for i in range(len(args)): + if args[i] == "--updateType": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No updateType is provided") + else: + update_dict["updateType"] = args[i + 1] + if update_dict.get("updateType") == "OPERATE_VNF": + if i + 2 >= len(args) or args[i + 2].startswith("--"): + raise ClientException( + "Whether the operate vnf is start,stop or rebuild needs to be specified" + ) + else: + update_dict["changeStateTo"] = args[i + 2] + if args[i] == "--vdu": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No vdu-id is provided") + else: + update_dict["vdu_id"] = args[i + 1] + elif args[i] == "--count-index": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No count-index is provided") + else: + update_dict["count-index"] = int(args[i + 1]) + elif args[i] == "--run-day1": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No count-index is provided") + else: + update_dict["run-day1"] = True + elif args[i] == "--vnf": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No vnf is provided") + else: + update_dict["vnfInstanceId"] = args[i + 1] + elif args[i] == "--vnfd-name": + if i + 1 >= len(args) or args[i + 1].startswith("--"): + raise ClientException("No vnf is provided") + else: + update_dict["vnfdId"] = args[i + 1] + if not update_dict.get("updateType"): + raise ClientException("updateType should be specified") + if update_dict["updateType"] == "CHANGE_VNFPKG": + if not update_dict.get("vnfInstanceId"): + raise ClientException("vnfInstanceId should be specified") + elif not update_dict.get("vnfdId"): + raise ClientException("vnfdId should be specified") + elif update_dict["updateType"] == "REMOVE_VNF": + if not update_dict.get("vnfInstanceId"): + raise ClientException("vnfInstanceId should be specified") + elif update_dict["updateType"] == "OPERATE_VNF": + if not update_dict.get("vdu_id"): + raise ClientException("vdu_id should be specified") + elif not update_dict.get("vnfInstanceId"): + raise ClientException("vnfInstanceId should be specified") + elif update_dict["updateType"] == "VERTICAL_SCALE": + if not update_dict.get("vnfInstanceId"): + raise ClientException("vnfInstanceId should be specified") + elif not update_dict.get("vnfdId"): + raise ClientException("vnfdId should be specified") + elif not update_dict.get("vdu_id"): + raise ClientException("vdu_id should be specified") + ctx.params["update_params"] = update_dict + return + + +@click.command( + name="ns-update", + context_settings=dict( + ignore_unknown_options=True, + ), ) -@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.argument("ns_name", required=True) +@click.argument( + "args", nargs=-1, type=click.UNPROCESSED, callback=process_update_params ) @click.option( "--timeout", required=False, default=None, type=int, help="timeout in seconds" @@ -162,26 +227,84 @@ def vnf_scale( help="do not return the control immediately, but keep it until the operation is completed, or timeout", ) @click.pass_context -def ns_update(ctx, ns_name, updatetype, config, timeout, wait): - """Executes an update of a Network Service. +def ns_update(ctx, ns_name, args, update_params, timeout, wait): + """NS-Update - updates based on the updateType + NS_NAME: name or ID of the NS instance - 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 + updateType:CHANGEVNF_PKG - NS_NAME: Network service instance name or ID. + \b + Options: + --updateType CHANGE_VNFPKG + --vnf VNF instance ID or VNF id in the NS [required] + --vnfd-name VNFD ID or VNFD name [required] - """ - op_data = { - "timeout": timeout, - "updateType": updatetype, - } - if config: - op_data["config"] = yaml.safe_load(config) + updateType:REMOVE_VNF + \b + Options: + --updateType REMOVE_VNF + --vnf VNF instance ID or VNF id in the NS [required] + + updateType:OPERATE_VNF + + \b + Options: + --updateType OPERATE_VNF (start, stop or rebuild) [required] + --vnf VNF instance ID or VNF id in the NS [required] + --vdu VDU-ID which should be operated [required] + --count-index count-index of the specfied VDU-ID [0 by Default] + --run-day1 True or False to run day1 operations [False by Default] + + \b + Options: + --updateType VERTICAL_SCALE + --vnf VNF instance ID or VNF id in the NS [required] + --vnfd-name VNFD ID or VNFD name [required] + --vdu VDU-ID which should be vertical scaled [required] + + \b + Example: + + updateType: CHANGE_VNFPKG + + osm ns-update NS_NAME|NS_ID --updateType CHANGE_VNFPKG --vnf MEMBER_VNF_INDEX|VNFINSTANCEID --vnfd-name VNFDID|VNFD_NAME + + updateType: REMOVE_VNF + + osm ns-update NS_NAME|NS_ID --updateType REMOVE_VNF --vnf MEMBER_VNF_INDEX|VNFINSTANCEID + + updateType: OPERATE_VNF + + osm ns-update NS_NAME|NS_ID --updateType OPERATE_VNF (start, stop or rebuild) --vdu VDUID --count-index COUNT_INDEX + --run-day1 True|False --vnf MEMBER_VNF_INDEX|VNFINSTANCEID + + updateType: VERTICAL_SCALE + + osm ns-update NS_NAME|NS_ID --updateType VERTICAL_SCALE --vnf MEMBER_VNF_INDEX|VNFINSTANCEID --vnfd-name VNFDID|VNFD_NAME + --vdu VDUID + + """ + params = ctx.params["update_params"] + vnf_id = params["vnfInstanceId"] + if params.get("vnfdId"): + if not validate_uuid4(params.get("vnfdId")): + vnfd_list = ctx.obj.vnfd.get(params.get("vnfdId")) + params["vnfdId"] = vnfd_list["_id"] + else: + params["vnfdId"] = params["vnfdId"] + if not validate_uuid4(vnf_id): + vnf_filter = f"member-vnf-index-ref={vnf_id}" + vnf_list = ctx.obj.vnf.list(ns=ns_name, filter=vnf_filter) + if len(vnf_list) == 0: + raise ClientException( + f"No VNF found in NS {ns_name} with filter {vnf_filter}" + ) + elif len(vnf_list) == 1: + params["vnfInstanceId"] = vnf_list[0]["_id"] + params["timeout"] = timeout utils.check_client_version(ctx.obj, ctx.command.name) - ctx.obj.ns.update(ns_name, op_data, wait=wait) + ctx.obj.ns.update(ns_name, params, wait=wait) def process_common_heal_params(heal_vnf_dict, args): diff --git a/osmclient/sol005/ns.py b/osmclient/sol005/ns.py index 29232c1..d2be850 100644 --- a/osmclient/sol005/ns.py +++ b/osmclient/sol005/ns.py @@ -541,21 +541,33 @@ class Ns(object): # 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") + op_data["changeVnfPackageData"] = {} + op_data["changeVnfPackageData"]["vnfInstanceId"] = data.get( + "vnfInstanceId" + ) + op_data["changeVnfPackageData"]["vnfdId"] = data.get("vnfdId") + elif op_data["updateType"] == "REMOVE_VNF": + op_data["removeVnfInstanceId"] = data.get("vnfInstanceId") + elif op_data["updateType"] == "OPERATE_VNF": + op_data["operateVnfData"] = {} + op_data["operateVnfData"]["vnfInstanceId"] = data.get("vnfInstanceId") + op_data["operateVnfData"]["changeStateTo"] = data.get("changeStateTo") + op_data["operateVnfData"]["additionalParam"] = {} + op_data["operateVnfData"]["additionalParam"]["vdu_id"] = data.get( + "vdu_id" + ) + op_data["operateVnfData"]["additionalParam"]["count-index"] = data.get( + "count-index", 0 + ) + op_data["operateVnfData"]["additionalParam"]["run-day1"] = data.get( + "run-day1", False + ) + elif op_data["updateType"] == "VERTICAL_SCALE": + op_data["verticalScaleVnf"] = {} + op_data["verticalScaleVnf"]["vnfInstanceId"] = data.get("vnfInstanceId") + op_data["verticalScaleVnf"]["vnfdId"] = data.get("vnfdId") + op_data["verticalScaleVnf"]["vduId"] = data.get("vdu_id") + op_data["verticalScaleVnf"]["countIndex"] = 0 if data.get("timeout"): op_data["timeout_ns_update"] = data["timeout"] -- 2.25.1