diff --git a/config/muxcable.py b/config/muxcable.py index c88adffe3785..e5638c1f031f 100644 --- a/config/muxcable.py +++ b/config/muxcable.py @@ -219,7 +219,7 @@ def muxcable(): platform_sfputil = platform_sfputil_helper.platform_sfputil -def lookup_statedb_and_update_configdb(per_npu_statedb, config_db, port, state_cfg_val, port_status_dict): +def lookup_statedb_and_update_configdb(db, per_npu_statedb, config_db, port, state_cfg_val, port_status_dict): muxcable_statedb_dict = per_npu_statedb.get_all(per_npu_statedb.STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) configdb_state = get_value_for_key_in_config_tbl(config_db, port, "state", "MUX_CABLE") @@ -228,15 +228,17 @@ def lookup_statedb_and_update_configdb(per_npu_statedb, config_db, port, state_c state = get_value_for_key_in_dict(muxcable_statedb_dict, port, "state", "MUX_CABLE_TABLE") + port_name = platform_sfputil_helper.get_interface_alias(port, db) + if str(state_cfg_val) == str(configdb_state): - port_status_dict[port] = 'OK' + port_status_dict[port_name] = 'OK' else: config_db.set_entry("MUX_CABLE", port, {"state": state_cfg_val, "server_ipv4": ipv4_value, "server_ipv6": ipv6_value}) if (str(state_cfg_val) == 'active' and str(state) != 'active') or (str(state_cfg_val) == 'standby' and str(state) != 'standby'): - port_status_dict[port] = 'INPROGRESS' + port_status_dict[port_name] = 'INPROGRESS' else: - port_status_dict[port] = 'OK' + port_status_dict[port_name] = 'OK' # 'muxcable' command ("config muxcable mode active|auto") @@ -248,7 +250,7 @@ def lookup_statedb_and_update_configdb(per_npu_statedb, config_db, port, state_c def mode(db, state, port, json_output): """Config muxcable mode""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) port_table_keys = {} y_cable_asic_table_keys = {} @@ -292,7 +294,7 @@ def mode(db, state, port, json_output): if logical_key in y_cable_asic_table_keys: port_status_dict = {} lookup_statedb_and_update_configdb( - per_npu_statedb[asic_index], per_npu_configdb[asic_index], port, state, port_status_dict) + db, per_npu_statedb[asic_index], per_npu_configdb[asic_index], port, state, port_status_dict) if json_output: click.echo("{}".format(json.dumps(port_status_dict, indent=4))) @@ -318,7 +320,7 @@ def mode(db, state, port, json_output): for key in port_table_keys[asic_id]: logical_port = key.split("|")[1] lookup_statedb_and_update_configdb( - per_npu_statedb[asic_id], per_npu_configdb[asic_id], logical_port, state, port_status_dict) + db, per_npu_statedb[asic_id], per_npu_configdb[asic_id], logical_port, state, port_status_dict) if json_output: click.echo("{}".format(json.dumps(port_status_dict, indent=4))) @@ -419,7 +421,7 @@ def hwmode(): def state(db, state, port): """Configure the muxcable mux state {active/standby}""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_DIR_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_CONFIG_HWMODE_DIR_RSP") @@ -437,6 +439,8 @@ def state(db, state, port): delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_DIR_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_CONFIG_HWMODE_DIR_RSP") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in toggling port {} to {}".format(port, state)) else: @@ -484,6 +488,8 @@ def state(db, state, port): delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_DIR_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_CONFIG_HWMODE_DIR_RSP") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in toggling port {} to {}".format(port, state)) else: @@ -500,7 +506,7 @@ def state(db, state, port): def setswitchmode(db, state, port): """Configure the muxcable mux switching mode {auto/manual}""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_SWMODE_CMD") @@ -520,6 +526,8 @@ def setswitchmode(db, state, port): delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_SWMODE_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_CONFIG_HWMODE_SWMODE_RSP") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in switch muxcable mode port {} to {}".format(port, state)) else: @@ -566,6 +574,8 @@ def setswitchmode(db, state, port): delete_all_keys_in_db_table("APPL_DB", "XCVRD_CONFIG_HWMODE_SWMODE_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_CONFIG_HWMODE_SWMODE_RSP") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in toggling port {} to {}".format(port, state)) else: @@ -588,7 +598,7 @@ def firmware(): def download(db, fwfile, port): """Config muxcable firmware download""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("STATE_DB", "XCVRD_DOWN_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_DOWN_FW_CMD") @@ -605,6 +615,8 @@ def download(db, fwfile, port): delete_all_keys_in_db_table("STATE_DB", "XCVRD_DOWN_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_DOWN_FW_CMD") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in downloading firmware port {} {}".format(port, fwfile)) else: @@ -652,6 +664,8 @@ def download(db, fwfile, port): delete_all_keys_in_db_table("STATE_DB", "XCVRD_DOWN_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_DOWN_FW_CMD") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in downloading firmware port {} {}".format(port, fwfile)) else: @@ -668,7 +682,7 @@ def download(db, fwfile, port): def activate(db, port, fwfile): """Config muxcable firmware activate""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("STATE_DB", "XCVRD_ACTI_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_ACTI_FW_CMD") @@ -685,6 +699,8 @@ def activate(db, port, fwfile): delete_all_keys_in_db_table("STATE_DB", "XCVRD_ACTI_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_ACTI_FW_CMD") + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in activate firmware port {} fwfile {}".format(port, fwfile)) else: @@ -731,6 +747,8 @@ def activate(db, port, fwfile): rc = res_dict[0] + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in activate firmware port {} fwfile {}".format(port, fwfile)) else: @@ -747,7 +765,7 @@ def activate(db, port, fwfile): def rollback(db, port, fwfile): """Config muxcable firmware rollback""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("STATE_DB", "XCVRD_ROLL_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_ROLL_FW_CMD") @@ -763,6 +781,9 @@ def rollback(db, port, fwfile): delete_all_keys_in_db_table("APPL_DB", "XCVRD_ROLL_FW_CMD") rc = res_dict[0] + + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in rollback firmware port {} fwfile {}".format(port, fwfile)) else: @@ -809,6 +830,8 @@ def rollback(db, port, fwfile): rc = res_dict[0] + port = platform_sfputil_helper.get_interface_alias(port, db) + if rc == 0: click.echo("Success in rollback firmware port {} fwfile {}".format(port, fwfile)) else: diff --git a/show/muxcable.py b/show/muxcable.py index ec18fe593718..6d4b1bab76f6 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -275,32 +275,35 @@ def get_switch_name(config_db): sys.exit(STATUS_FAIL) -def create_json_dump_per_port_status(port_status_dict, muxcable_info_dict, muxcable_health_dict, asic_index, port): +def create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, asic_index, port): status_value = get_value_for_key_in_dict(muxcable_info_dict[asic_index], port, "state", "MUX_CABLE_TABLE") - port_status_dict["MUX_CABLE"][port] = {} - port_status_dict["MUX_CABLE"][port]["STATUS"] = status_value + port_name = platform_sfputil_helper.get_interface_alias(port, db) + port_status_dict["MUX_CABLE"][port_name] = {} + port_status_dict["MUX_CABLE"][port_name]["STATUS"] = status_value health_value = get_value_for_key_in_dict(muxcable_health_dict[asic_index], port, "state", "MUX_LINKMGR_TABLE") - port_status_dict["MUX_CABLE"][port]["HEALTH"] = health_value + port_status_dict["MUX_CABLE"][port_name]["HEALTH"] = health_value -def create_table_dump_per_port_status(print_data, muxcable_info_dict, muxcable_health_dict, asic_index, port): +def create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcable_health_dict, asic_index, port): print_port_data = [] status_value = get_value_for_key_in_dict(muxcable_info_dict[asic_index], port, "state", "MUX_CABLE_TABLE") #status_value = get_value_for_key_in_tbl(y_cable_asic_table, port, "status") health_value = get_value_for_key_in_dict(muxcable_health_dict[asic_index], port, "state", "MUX_LINKMGR_TABLE") - print_port_data.append(port) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + print_port_data.append(port_name) print_port_data.append(status_value) print_port_data.append(health_value) print_data.append(print_port_data) -def create_table_dump_per_port_config(print_data, per_npu_configdb, asic_id, port): +def create_table_dump_per_port_config(db ,print_data, per_npu_configdb, asic_id, port): port_list = [] - port_list.append(port) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + port_list.append(port_name) state_value = get_value_for_key_in_config_tbl(per_npu_configdb[asic_id], port, "state", "MUX_CABLE") port_list.append(state_value) ipv4_value = get_value_for_key_in_config_tbl(per_npu_configdb[asic_id], port, "server_ipv4", "MUX_CABLE") @@ -310,15 +313,16 @@ def create_table_dump_per_port_config(print_data, per_npu_configdb, asic_id, por print_data.append(port_list) -def create_json_dump_per_port_config(port_status_dict, per_npu_configdb, asic_id, port): +def create_json_dump_per_port_config(db, port_status_dict, per_npu_configdb, asic_id, port): state_value = get_value_for_key_in_config_tbl(per_npu_configdb[asic_id], port, "state", "MUX_CABLE") - port_status_dict["MUX_CABLE"]["PORTS"][port] = {"STATE": state_value} - port_status_dict["MUX_CABLE"]["PORTS"][port]["SERVER"] = {} + port_name = platform_sfputil_helper.get_interface_alias(port, db) + port_status_dict["MUX_CABLE"]["PORTS"][port_name] = {"STATE": state_value} + port_status_dict["MUX_CABLE"]["PORTS"][port_name]["SERVER"] = {} ipv4_value = get_value_for_key_in_config_tbl(per_npu_configdb[asic_id], port, "server_ipv4", "MUX_CABLE") - port_status_dict["MUX_CABLE"]["PORTS"][port]["SERVER"]["IPv4"] = ipv4_value + port_status_dict["MUX_CABLE"]["PORTS"][port_name]["SERVER"]["IPv4"] = ipv4_value ipv6_value = get_value_for_key_in_config_tbl(per_npu_configdb[asic_id], port, "server_ipv6", "MUX_CABLE") - port_status_dict["MUX_CABLE"]["PORTS"][port]["SERVER"]["IPv6"] = ipv6_value + port_status_dict["MUX_CABLE"]["PORTS"][port_name]["SERVER"]["IPv6"] = ipv6_value @muxcable.command() @@ -328,7 +332,7 @@ def create_json_dump_per_port_config(port_status_dict, per_npu_configdb, asic_id def status(db, port, json_output): """Show muxcable status information""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) port_table_keys = {} port_health_table_keys = {} @@ -359,7 +363,8 @@ def status(db, port, json_output): import sonic_platform_base.sonic_sfp.sfputilhelper asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port) if asic_index is None: - click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port)) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port_name)) sys.exit(STATUS_FAIL) muxcable_info_dict[asic_index] = per_npu_statedb[asic_index].get_all( @@ -375,7 +380,7 @@ def status(db, port, json_output): port_status_dict = {} port_status_dict["MUX_CABLE"] = {} - create_json_dump_per_port_status(port_status_dict, muxcable_info_dict, + create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, asic_index, port) click.echo("{}".format(json.dumps(port_status_dict, indent=4))) @@ -383,7 +388,7 @@ def status(db, port, json_output): else: print_data = [] - create_table_dump_per_port_status(print_data, muxcable_info_dict, + create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcable_health_dict, asic_index, port) headers = ['PORT', 'STATUS', 'HEALTH'] @@ -391,7 +396,8 @@ def status(db, port, json_output): click.echo(tabulate(print_data, headers=headers)) sys.exit(STATUS_SUCCESSFUL) else: - click.echo("this is not a valid port present on mux_cable".format(port)) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("this is not a valid port present on mux_cable".format(port_name)) sys.exit(STATUS_FAIL) else: click.echo("there is not a valid asic table for this asic_index".format(asic_index)) @@ -410,7 +416,7 @@ def status(db, port, json_output): per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) muxcable_health_dict[asic_id] = per_npu_statedb[asic_id].get_all( per_npu_statedb[asic_id].STATE_DB, 'MUX_LINKMGR_TABLE|{}'.format(port)) - create_json_dump_per_port_status(port_status_dict, muxcable_info_dict, + create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, asic_id, port) click.echo("{}".format(json.dumps(port_status_dict, indent=4))) @@ -425,7 +431,7 @@ def status(db, port, json_output): muxcable_info_dict[asic_id] = per_npu_statedb[asic_id].get_all( per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) - create_table_dump_per_port_status(print_data, muxcable_info_dict, + create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcable_health_dict, asic_id, port) headers = ['PORT', 'STATUS', 'HEALTH'] @@ -441,7 +447,7 @@ def status(db, port, json_output): def config(db, port, json_output): """Show muxcable config information""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) port_mux_tbl_keys = {} asic_start_idx = None @@ -476,7 +482,8 @@ def config(db, port, json_output): import sonic_platform_base.sonic_sfp.sfputilhelper asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port) if asic_index is None: - click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port)) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port_name)) sys.exit(CONFIG_FAIL) port_status_dict = {} @@ -496,7 +503,7 @@ def config(db, port, json_output): port_status_dict["MUX_CABLE"] = {} port_status_dict["MUX_CABLE"]["PORTS"] = {} - create_json_dump_per_port_config(port_status_dict, per_npu_configdb, asic_id, port) + create_json_dump_per_port_config(db, port_status_dict, per_npu_configdb, asic_id, port) click.echo("{}".format(json.dumps(port_status_dict, indent=4))) sys.exit(CONFIG_SUCCESSFUL) @@ -504,7 +511,7 @@ def config(db, port, json_output): print_data = [] print_peer_tor = [] - create_table_dump_per_port_config(print_data, per_npu_configdb, asic_id, port) + create_table_dump_per_port_config(db, print_data, per_npu_configdb, asic_id, port) headers = ['SWITCH_NAME', 'PEER_TOR'] peer_tor_data = [] @@ -518,7 +525,8 @@ def config(db, port, json_output): sys.exit(CONFIG_SUCCESSFUL) else: - click.echo("this is not a valid port present on mux_cable".format(port)) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("this is not a valid port present on mux_cable".format(port_name)) sys.exit(CONFIG_FAIL) else: click.echo("there is not a valid asic table for this asic_index".format(asic_index)) @@ -541,7 +549,7 @@ def config(db, port, json_output): for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) for port in natsorted(port_mux_tbl_keys[asic_id]): - create_json_dump_per_port_config(port_status_dict, per_npu_configdb, asic_id, port) + create_json_dump_per_port_config(db, port_status_dict, per_npu_configdb, asic_id, port) click.echo("{}".format(json.dumps(port_status_dict, indent=4))) else: @@ -550,7 +558,7 @@ def config(db, port, json_output): for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) for port in natsorted(port_mux_tbl_keys[asic_id]): - create_table_dump_per_port_config(print_data, per_npu_configdb, asic_id, port) + create_table_dump_per_port_config(db, print_data, per_npu_configdb, asic_id, port) headers = ['SWITCH_NAME', 'PEER_TOR'] peer_tor_data = [] @@ -612,7 +620,7 @@ def eyeinfo(port, target): def cableinfo(db, port): """Show muxcable cable information""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) if platform_sfputil is not None: physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port) @@ -652,7 +660,7 @@ def hwmode(): def muxdirection(db, port): """Shows the current direction of the muxcable {active/standy}""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_DIR_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RSP") @@ -668,6 +676,7 @@ def muxdirection(db, port): body = [] temp_list = [] headers = ['Port', 'Direction'] + port = platform_sfputil_helper.get_interface_alias(port, db) temp_list.append(port) temp_list.append(res_dict[1]) body.append(temp_list) @@ -716,6 +725,7 @@ def muxdirection(db, port): res_dict[1] = "unknown" res_dict = update_and_get_response_for_xcvr_cmd( "state", "state", "True", "XCVRD_SHOW_HWMODE_DIR_CMD", "XCVRD_SHOW_HWMODE_DIR_RSP", port, 1, "probe") + port = platform_sfputil_helper.get_interface_alias(port, db) temp_list.append(port) temp_list.append(res_dict[1]) body.append(temp_list) @@ -739,7 +749,7 @@ def muxdirection(db, port): def switchmode(db, port): """Shows the current switching mode of the muxcable {auto/manual}""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_SWMODE_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_SWMODE_RSP") @@ -754,6 +764,7 @@ def switchmode(db, port): body = [] temp_list = [] headers = ['Port', 'Switching'] + port = platform_sfputil_helper.get_interface_alias(port, db) temp_list.append(port) temp_list.append(res_dict[1]) body.append(temp_list) @@ -802,6 +813,7 @@ def switchmode(db, port): res_dict[1] = "unknown" res_dict = update_and_get_response_for_xcvr_cmd( "state", "state", "True", "XCVRD_SHOW_HWMODE_SWMODE_CMD", "XCVRD_SHOW_HWMODE_SWMODE_RSP", port, 1, "probe") + port = platform_sfputil_helper.get_interface_alias(port, db) temp_list.append(port) temp_list.append(res_dict[1]) rc = res_dict[1] @@ -960,7 +972,7 @@ def firmware(): def version(db, port, active): """Show muxcable firmware version""" - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) delete_all_keys_in_db_table("APPL_DB", "XCVRD_DOWN_FW_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_DOWN_FW_RSP") delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_FW_CMD") @@ -1001,7 +1013,8 @@ def version(db, port, active): else: click.echo("{}".format(json.dumps(mux_info_dict, indent=4))) else: - click.echo("Did not get a valid Port for mux firmware version".format(port)) + port_name = platform_sfputil_helper.get_interface_name(port, db) + click.echo("Did not get a valid Port for mux firmware version".format(port_name)) sys.exit(CONFIG_FAIL) @@ -1012,7 +1025,7 @@ def version(db, port, active): def metrics(db, port, json_output): """Show muxcable metrics """ - port = platform_sfputil_helper.get_interface_alias(port, db) + port = platform_sfputil_helper.get_interface_name(port, db) metrics_table_keys = {} per_npu_statedb = {} @@ -1035,7 +1048,8 @@ def metrics(db, port, json_output): logical_port_list = platform_sfputil_helper.get_logical_list() if port not in logical_port_list: - click.echo(("ERR: Not a valid logical port for muxcable firmware {}".format(port))) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo(("ERR: Not a valid logical port for muxcable firmware {}".format(port_name))) sys.exit(CONFIG_FAIL) asic_index = None @@ -1047,7 +1061,8 @@ def metrics(db, port, json_output): import sonic_platform_base.sonic_sfp.sfputilhelper asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port) if asic_index is None: - click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port)) + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port_name)) metrics_dict[asic_index] = per_npu_statedb[asic_index].get_all( per_npu_statedb[asic_index].STATE_DB, 'MUX_METRICS_TABLE|{}'.format(port)) @@ -1059,6 +1074,7 @@ def metrics(db, port, json_output): print_data = [] for key, val in ordered_dict.items(): print_port_data = [] + port = platform_sfputil_helper.get_interface_alias(port, db) print_port_data.append(port) print_port_data.append(key) print_port_data.append(val) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 43ed0013e6b1..498b4761ef62 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -35,6 +35,17 @@ Ethernet32 active healthy """ +tabular_data_status_output_expected_alias = """\ +PORT STATUS HEALTH +------ -------- --------- +etp1 active healthy +etp2 standby healthy +etp3 standby unhealthy +etp4 unknown unhealthy +etp5 standby healthy +etp9 active healthy +""" + json_data_status_output_expected = """\ { "MUX_CABLE": { @@ -66,6 +77,37 @@ } """ +json_data_status_output_expected_alias = """\ +{ + "MUX_CABLE": { + "etp1": { + "STATUS": "active", + "HEALTH": "healthy" + }, + "etp2": { + "STATUS": "standby", + "HEALTH": "healthy" + }, + "etp3": { + "STATUS": "standby", + "HEALTH": "unhealthy" + }, + "etp4": { + "STATUS": "unknown", + "HEALTH": "unhealthy" + }, + "etp5": { + "STATUS": "standby", + "HEALTH": "healthy" + }, + "etp9": { + "STATUS": "active", + "HEALTH": "healthy" + } + } +} +""" + tabular_data_config_output_expected = """\ SWITCH_NAME PEER_TOR @@ -82,6 +124,21 @@ Ethernet32 auto 10.1.1.1 fc00::75 """ +tabular_data_config_output_expected_alias = """\ +SWITCH_NAME PEER_TOR +------------- ---------- +sonic-switch 10.2.2.2 +port state ipv4 ipv6 +------ ------- -------- -------- +etp1 active 10.2.1.1 e800::46 +etp2 auto 10.3.1.1 e801::46 +etp3 active 10.4.1.1 e802::46 +etp4 active 10.4.1.1 e802::46 +etp5 standby 10.1.1.1 fc00::75 +etp8 manual 10.1.1.1 fc00::75 +etp9 auto 10.1.1.1 fc00::75 +""" + json_data_status_config_output_expected = """\ { "MUX_CABLE": { @@ -141,6 +198,65 @@ } """ +json_data_status_config_output_expected_alias = """\ +{ + "MUX_CABLE": { + "PEER_TOR": "10.2.2.2", + "PORTS": { + "etp1": { + "STATE": "active", + "SERVER": { + "IPv4": "10.2.1.1", + "IPv6": "e800::46" + } + }, + "etp2": { + "STATE": "auto", + "SERVER": { + "IPv4": "10.3.1.1", + "IPv6": "e801::46" + } + }, + "etp3": { + "STATE": "active", + "SERVER": { + "IPv4": "10.4.1.1", + "IPv6": "e802::46" + } + }, + "etp4": { + "STATE": "active", + "SERVER": { + "IPv4": "10.4.1.1", + "IPv6": "e802::46" + } + }, + "etp5": { + "STATE": "standby", + "SERVER": { + "IPv4": "10.1.1.1", + "IPv6": "fc00::75" + } + }, + "etp8": { + "STATE": "manual", + "SERVER": { + "IPv4": "10.1.1.1", + "IPv6": "fc00::75" + } + }, + "etp9": { + "STATE": "auto", + "SERVER": { + "IPv4": "10.1.1.1", + "IPv6": "fc00::75" + } + } + } + } +} +""" + json_port_data_status_config_output_expected = """\ { "MUX_CABLE": { @@ -158,6 +274,23 @@ } """ +json_port_data_status_config_output_expected_alias = """\ +{ + "MUX_CABLE": { + "PEER_TOR": "10.2.2.2", + "PORTS": { + "etp9": { + "STATE": "auto", + "SERVER": { + "IPv4": "10.1.1.1", + "IPv6": "fc00::75" + } + } + } + } +} +""" + json_data_config_output_auto_expected = """\ { "Ethernet32": "OK", @@ -169,6 +302,17 @@ } """ +json_data_config_output_auto_expected_alias = """\ +{ + "etp9": "OK", + "etp1": "OK", + "etp2": "OK", + "etp3": "OK", + "etp5": "OK", + "etp4": "OK" +} +""" + json_data_config_output_active_expected = """\ { "Ethernet32": "OK", @@ -180,6 +324,17 @@ } """ +json_data_config_output_active_expected_alias = """\ +{ + "etp9": "OK", + "etp1": "OK", + "etp2": "INPROGRESS", + "etp3": "OK", + "etp5": "INPROGRESS", + "etp4": "OK" +} +""" + expected_muxcable_cableinfo_output = """\ Vendor Model -------- --------------- @@ -192,12 +347,24 @@ Ethernet12 active """ +show_muxcable_hwmode_muxdirection_active_expected_output_alias = """\ +Port Direction +------ ----------- +etp4 active +""" + show_muxcable_hwmode_muxdirection_standby_expected_output = """\ Port Direction ---------- ----------- Ethernet12 standby """ +show_muxcable_hwmode_muxdirection_standby_expected_output_alias = """\ +Port Direction +------ ----------- +etp4 standby +""" + show_muxcable_firmware_version_expected_output = """\ { "version_self_active": "0.6MS", @@ -229,6 +396,15 @@ Ethernet0 linkmgrd_switch_standby_end 2021-May-13 10:01:15.696728 """ +show_muxcable_metrics_expected_output_alias = """\ +PORT EVENT TIME +------ ---------------------------- --------------------------- +etp1 linkmgrd_switch_active_start 2021-May-13 10:00:21.420898 +etp1 xcvrd_switch_standby_start 2021-May-13 10:01:15.690835 +etp1 xcvrd_switch_standby_end 2021-May-13 10:01:15.696051 +etp1 linkmgrd_switch_standby_end 2021-May-13 10:01:15.696728 +""" + show_muxcable_metrics_expected_output_json = """\ { "linkmgrd_switch_active_start": "2021-May-13 10:00:21.420898", @@ -253,6 +429,16 @@ def test_muxcable_status(self): assert result.exit_code == 0 assert result.output == tabular_data_status_output_expected + def test_muxcable_status_alias(self): + runner = CliRunner() + db = Db() + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["status"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == tabular_data_status_output_expected_alias + def test_muxcable_status_json(self): runner = CliRunner() db = Db() @@ -262,6 +448,17 @@ def test_muxcable_status_json(self): assert result.exit_code == 0 assert result.output == json_data_status_output_expected + def test_muxcable_status_json_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["status"], ["--json"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == json_data_status_output_expected_alias + def test_muxcable_status_config(self): runner = CliRunner() db = Db() @@ -271,6 +468,17 @@ def test_muxcable_status_config(self): assert result.exit_code == 0 assert result.output == tabular_data_config_output_expected + def test_muxcable_status_config_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["config"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == tabular_data_config_output_expected_alias + def test_muxcable_status_config_json(self): runner = CliRunner() db = Db() @@ -280,6 +488,18 @@ def test_muxcable_status_config_json(self): assert result.exit_code == 0 assert result.output == json_data_status_config_output_expected + def test_muxcable_status_config_json_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["config"], ["--json"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == json_data_status_config_output_expected_alias + + def test_muxcable_config_json_with_incorrect_port(self): runner = CliRunner() db = Db() @@ -297,6 +517,18 @@ def test_muxcable_status_json_with_correct_port(self): assert result.exit_code == 0 + def test_muxcable_status_json_with_correct_port_alias(self): + runner = CliRunner() + db = Db() + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + with mock.patch('sonic_platform_base.sonic_sfp.sfputilhelper') as patched_util: + patched_util.SfpUtilHelper.return_value.get_asic_id_for_logical_port.return_value = 0 + result = runner.invoke(show.cli.commands["muxcable"].commands["status"], ["Ethernet0", "--json"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + + def test_muxcable_status_json_port_incorrect_index(self): runner = CliRunner() db = Db() @@ -415,6 +647,17 @@ def test_config_muxcable_mode_auto_json(self): assert result.exit_code == 0 assert result.output == json_data_config_output_auto_expected + def test_config_muxcable_mode_auto_json_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(config.config.commands["muxcable"].commands["mode"], ["auto", "all", "--json"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == json_data_config_output_auto_expected_alias + def test_config_muxcable_mode_active_json(self): runner = CliRunner() db = Db() @@ -424,6 +667,17 @@ def test_config_muxcable_mode_active_json(self): assert result.exit_code == 0 assert result.output == json_data_config_output_active_expected + def test_config_muxcable_mode_active_json_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(config.config.commands["muxcable"].commands["mode"], ["active", "all", "--json"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + + assert result.exit_code == 0 + assert result.output == json_data_config_output_active_expected_alias + def test_config_muxcable_json_port_auto_Ethernet0(self): runner = CliRunner() db = Db() @@ -661,6 +915,28 @@ def test_show_muxcable_hwmode_muxdirection_port_active(self): assert result.exit_code == 0 assert result.output == show_muxcable_hwmode_muxdirection_active_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "active"})) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet12"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(1))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_active_expected_output_alias(self): + runner = CliRunner() + db = Db() + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["hwmode"].commands["muxdirection"], + ["etp4"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + assert result.exit_code == 0 + assert result.output == show_muxcable_hwmode_muxdirection_active_expected_output_alias + + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, 1: "standby"})) @@ -699,6 +975,28 @@ def test_show_muxcable_hwmode_muxdirection_port_standby(self): assert result.exit_code == 0 assert result.output == show_muxcable_hwmode_muxdirection_standby_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet12"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_port_standby_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["hwmode"].commands["muxdirection"], + ["etp4"], obj=db) + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + assert result.exit_code == 0 + assert result.output == show_muxcable_hwmode_muxdirection_standby_expected_output_alias + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, 1: "sucess"})) @@ -937,7 +1235,23 @@ def test_show_muxcable_metrics_port(self): @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) - def test_show_muxcable_metrics_port(self): + def test_show_muxcable_metrics_port_alias(self): + runner = CliRunner() + db = Db() + + os.environ['SONIC_CLI_IFACE_MODE'] = "alias" + result = runner.invoke(show.cli.commands["muxcable"].commands["metrics"], + ["etp1"], obj=db) + + os.environ['SONIC_CLI_IFACE_MODE'] = "default" + assert result.exit_code == 0 + assert result.output == show_muxcable_metrics_expected_output_alias + + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet12"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + def test_show_muxcable_metrics_port_json(self): runner = CliRunner() db = Db() diff --git a/utilities_common/platform_sfputil_helper.py b/utilities_common/platform_sfputil_helper.py index 37bd6f4d664f..a7f4477660d3 100644 --- a/utilities_common/platform_sfputil_helper.py +++ b/utilities_common/platform_sfputil_helper.py @@ -68,15 +68,28 @@ def get_physical_to_logical(): return platform_sfputil.physical_to_logical -def get_interface_alias(port, db): +def get_interface_name(port, db): if port is not "all" and port is not None: alias = port iface_alias_converter = clicommon.InterfaceAliasConverter(db) - port = iface_alias_converter.alias_to_name(alias) - if port is None: - click.echo("cannot find port name for alias {}".format(alias)) - sys.exit(1) + if clicommon.get_interface_naming_mode() == "alias": + port = iface_alias_converter.alias_to_name(alias) + if port is None: + click.echo("cannot find port name for alias {}".format(alias)) + sys.exit(1) return port +def get_interface_alias(port, db): + + if port is not "all" and port is not None: + alias = port + iface_alias_converter = clicommon.InterfaceAliasConverter(db) + if clicommon.get_interface_naming_mode() == "alias": + port = iface_alias_converter.name_to_alias(alias) + if port is None: + click.echo("cannot find port name for alias {}".format(alias)) + sys.exit(1) + + return port