From a425ca26a89ec0821cd41d827a904da7678ba801 Mon Sep 17 00:00:00 2001 From: vdahiya12 <67608553+vdahiya12@users.noreply.github.com> Date: Thu, 10 Jun 2021 11:12:18 -0700 Subject: [PATCH] [config] support for configuring muxcable to manual mode of operation (#1642) What I did This PR adds support for an option to configure muxcable mode to a manual mode. The manual mode is in addition to auto/active mode. The new output would look like this in case an active flag is passed to the command line admin@sonic:~$ sudo config muxcable mode manual Ethernet0 admin@sonic:~$ sudo config muxcable mode manual all added an option to set muxcable mode to manual mode, in addition to existing auto/active modes. How I did it added the changes in config/muxcable.py and added testcases How to verify it Ran the unit tests Signed-off-by: vaibhav-dahiya --- config/muxcable.py | 36 ++++++++------------------------ tests/mock_tables/config_db.json | 5 +++++ tests/muxcable_test.py | 20 ++++++++++++++++-- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/config/muxcable.py b/config/muxcable.py index 965bceb6de1e..6bd139b992cf 100644 --- a/config/muxcable.py +++ b/config/muxcable.py @@ -73,43 +73,25 @@ def lookup_statedb_and_update_configdb(per_npu_statedb, config_db, port, state_c ipv6_value = get_value_for_key_in_config_tbl(config_db, port, "server_ipv6", "MUX_CABLE") state = get_value_for_key_in_dict(muxcable_statedb_dict, port, "state", "MUX_CABLE_TABLE") - if (state == "active" and configdb_state == "active") or (state == "standby" and configdb_state == "active") or (state == "unknown" and configdb_state == "active"): - if state_cfg_val == "active": - # status is already active, so right back error - port_status_dict[port] = 'OK' - if state_cfg_val == "auto": - # display ok and write to cfgdb auto - port_status_dict[port] = 'OK' - config_db.set_entry("MUX_CABLE", port, {"state": "auto", - "server_ipv4": ipv4_value, "server_ipv6": ipv6_value}) - elif state == "active" and configdb_state == "auto": - if state_cfg_val == "active": - # make the state active and write back OK - config_db.set_entry("MUX_CABLE", port, {"state": "active", - "server_ipv4": ipv4_value, "server_ipv6": ipv6_value}) - port_status_dict[port] = 'OK' - if state_cfg_val == "auto": - # dont write anything to db, write OK to user - port_status_dict[port] = 'OK' - elif (state == "standby" and configdb_state == "auto") or (state == "unknown" and configdb_state == "auto"): - if state_cfg_val == "active": - # make the state active - config_db.set_entry("MUX_CABLE", port, {"state": "active", - "server_ipv4": ipv4_value, "server_ipv6": ipv6_value}) + if str(state_cfg_val) == str(configdb_state): + port_status_dict[port] = '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': port_status_dict[port] = 'INPROGRESS' - if state_cfg_val == "auto": - # dont write anything to db + else: port_status_dict[port] = 'OK' # 'muxcable' command ("config muxcable mode active|auto") @muxcable.command() -@click.argument('state', metavar='', required=True, type=click.Choice(["active", "auto"])) +@click.argument('state', metavar='', required=True, type=click.Choice(["active", "auto", "manual"])) @click.argument('port', metavar='', required=True, default=None) @click.option('--json', 'json_output', required=False, is_flag=True, type=click.BOOL) def mode(state, port, json_output): - """Show muxcable summary information""" + """Config muxcable mode""" port_table_keys = {} y_cable_asic_table_keys = {} diff --git a/tests/mock_tables/config_db.json b/tests/mock_tables/config_db.json index e4be4606dabd..24bab1f8b9fe 100644 --- a/tests/mock_tables/config_db.json +++ b/tests/mock_tables/config_db.json @@ -1577,6 +1577,11 @@ "server_ipv4": "10.1.1.1", "server_ipv6": "fc00::75" }, + "MUX_CABLE|Ethernet28": { + "state": "manual", + "server_ipv4": "10.1.1.1", + "server_ipv6": "fc00::75" + }, "MUX_CABLE|Ethernet0": { "state": "active", "server_ipv4": "10.2.1.1", diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 0385ffeae598..2d63a5d4823b 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -72,6 +72,7 @@ Ethernet4 auto 10.3.1.1 e801::46 Ethernet8 active 10.4.1.1 e802::46 Ethernet12 active 10.4.1.1 e802::46 +Ethernet28 manual 10.1.1.1 fc00::75 Ethernet32 auto 10.1.1.1 fc00::75 """ @@ -108,6 +109,13 @@ "IPv6": "e802::46" } }, + "Ethernet28": { + "STATE": "manual", + "SERVER": { + "IPv4": "10.1.1.1", + "IPv6": "fc00::75" + } + }, "Ethernet32": { "STATE": "auto", "SERVER": { @@ -363,6 +371,16 @@ def test_config_muxcable_tabular_port_Ethernet8_auto(self): assert result.exit_code == 0 + def test_config_muxcable_tabular_port_Ethernet8_manual(self): + runner = CliRunner() + db = Db() + + 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(config.config.commands["muxcable"].commands["mode"], ["manual", "Ethernet8"], obj=db) + + assert result.exit_code == 0 + def test_config_muxcable_mode_auto_json(self): runner = CliRunner() db = Db() @@ -377,8 +395,6 @@ def test_config_muxcable_mode_active_json(self): db = Db() result = runner.invoke(config.config.commands["muxcable"].commands["mode"], ["active", "all", "--json"], obj=db) - f = open("newfile1", "w") - f.write(result.output) assert result.exit_code == 0 assert result.output == json_data_config_output_active_expected