From 293b57c846fc59abaa39818ca6ca9908bd90c541 Mon Sep 17 00:00:00 2001 From: Yaqiang Zhu Date: Thu, 6 Jun 2024 00:22:24 +0800 Subject: [PATCH] [dhcp_relay] Revert cli for dhcp per-interface counter (#19154) * Revert "add show dhcp_relay ipv4 counter entry, fix interface name offset issue (#16507)" This reverts commit 9c1c82e9ff708616017d391213751a3b02492e6d. * Revert "[dhcp-relay]: dhcp/dhcpv6 per interface counter support (#16377)" This reverts commit a522a63e25e2a17ce04c57540117d339fadfb5c5. * Remove test_show_dhcp_relay_ipv4_counter_with_enabled_dhcp_server --- dockers/docker-dhcp-relay/Dockerfile.j2 | 2 - .../test_show_dhcp6relay_counters.py | 44 +++--- .../cli-plugin-tests/test_show_dhcp_relay.py | 10 -- .../cli/show/plugins/show_dhcp_relay.py | 131 +++--------------- sonic-slave-bullseye/Dockerfile.j2 | 1 - 5 files changed, 38 insertions(+), 150 deletions(-) diff --git a/dockers/docker-dhcp-relay/Dockerfile.j2 b/dockers/docker-dhcp-relay/Dockerfile.j2 index a406af404065..a126e6924a46 100644 --- a/dockers/docker-dhcp-relay/Dockerfile.j2 +++ b/dockers/docker-dhcp-relay/Dockerfile.j2 @@ -19,8 +19,6 @@ RUN apt-get install -y libjsoncpp-dev \ RUN pip3 install psutil -RUN apt-get install -y libjsoncpp-dev - {% if docker_dhcp_relay_debs.strip() -%} # Copy built Debian packages {{ copy_files("debs/", docker_dhcp_relay_debs.split(' '), "/debs/") }} diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py index 7f7edca64d0d..3cb2d2bb5443 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py @@ -17,21 +17,24 @@ except KeyError: pass -expected_counts_v6 = """\ - Message Type Vlan1000(RX) --------------- --------------- - - Message Type Vlan1000(TX) --------------- --------------- - -""" - -expected_counts_v4 = """\ - Message Type Vlan1000(RX) --------------- --------------- - - Message Type Vlan1000(TX) --------------- --------------- +expected_counts = """\ + Message Type Vlan1000 +------------------- ----------- + Unknown + Solicit + Advertise + Request + Confirm + Renew + Rebind + Reply + Release + Decline + Reconfigure +Information-Request + Relay-Forward + Relay-Reply + Malformed """ @@ -40,14 +43,5 @@ class TestDhcp6RelayCounters(object): def test_show_counts(self): runner = CliRunner() result = runner.invoke(show.dhcp6relay_counters.commands["counts"], ["-i Vlan1000"]) - print(result.output) - assert result.output == expected_counts_v6 - -class TestDhcpRelayCounters(object): - - def test_show_counts(self): - runner = CliRunner() - result = runner.invoke(show.dhcp4relay_counters.commands["counts"], ["-i Vlan1000"]) - print(result.output) - assert result.output == expected_counts_v4 + assert result.output == expected_counts diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py index 387e052c143a..de55af82d9d4 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py @@ -173,16 +173,6 @@ def test_show_multi_dhcp_relay(test_name, test_data, fs): assert result == expected_output -def test_show_dhcp_relay_ipv4_counter_with_enabled_dhcp_server(): - with mock.patch.object(show, "is_dhcp_server_enabled", return_value=True), \ - mock.patch.object(swsscommon.ConfigDBConnector, "connect", return_value=None), \ - mock.patch.object(swsscommon.ConfigDBConnector, "get_table", return_value=None), \ - mock.patch.object(click, "echo", return_value=None) as mock_echo: - show.ipv4_counters("Etherner1") - expected_param = "Unsupport to check dhcp_relay ipv4 counter when dhcp_server feature is enabled" - mock_echo.assert_called_once_with(expected_param) - - @pytest.mark.parametrize("enable_dhcp_server", [True, False]) def test_is_dhcp_server_enabled(enable_dhcp_server): result = show.is_dhcp_server_enabled({"dhcp_server": {"state": "enabled" if enable_dhcp_server else "disabled"}}) diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py index 7f99102f9917..d9e87b2b347c 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py @@ -1,5 +1,4 @@ import click -import ast from natsort import natsorted from tabulate import tabulate import show.vlan as show_vlan @@ -8,20 +7,13 @@ from swsscommon.swsscommon import ConfigDBConnector from swsscommon.swsscommon import SonicV2Connector + # STATE_DB Table -DHCPv4_COUNTER_TABLE = 'DHCP_COUNTER_TABLE' DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE' -# DHCPv4 Counter Messages -dhcpv4_messages = [ - "Unknown", "Discover", "Offer", "Request", "Decline", "Ack", "Nack", "Release", "Inform" -] - # DHCPv6 Counter Messages -dhcpv6_messages = [ - "Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", - "Decline", "Reconfigure", "Information-Request", "Relay-Forward", "Relay-Reply", "Malformed" -] +messages = ["Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", + "Reconfigure", "Information-Request", "Relay-Forward", "Relay-Reply", "Malformed"] # DHCP_RELAY Config Table DHCP_RELAY = 'DHCP_RELAY' @@ -47,80 +39,6 @@ def get_dhcp_helper_address(ctx, vlan): show_vlan.VlanBrief.register_column('DHCP Helper Address', get_dhcp_helper_address) -class DHCPv4_Counter(object): - def __init__(self): - self.db = SonicV2Connector(use_unix_socket_path=False) - self.db.connect(self.db.STATE_DB) - self.table_name = DHCPv4_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB) - - def get_interface(self): - """ Get all names of all interfaces in DHCPv4_COUNTER_TABLE """ - interfaces = [] - for key in self.db.keys(self.db.STATE_DB): - if DHCPv4_COUNTER_TABLE in key: - interfaces.append(key[19:]) - return interfaces - - def get_dhcp4relay_msg_count(self, interface, dir): - """ Get count of a dhcprelay message """ - value = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(dir)) - cnts = ast.literal_eval(str(value)) - data = [] - if cnts is not None: - for k, v in cnts.items(): - data.append([k, v]) - return data - - def clear_table(self, interface): - """ Reset all message counts to 0 """ - v4_cnts = {} - for msg in dhcpv4_messages: - v4_cnts[msg] = '0' - self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("RX"), str(v4_cnts)) - self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("TX"), str(v4_cnts)) - -def print_dhcpv4_count(counter, intf): - """Print count of each message""" - rx_data = counter.get_dhcp4relay_msg_count(intf, "RX") - print(tabulate(rx_data, headers=["Message Type", intf+"(RX)"], tablefmt='simple', stralign='right') + "\n") - tx_data = counter.get_dhcp4relay_msg_count(intf, "TX") - print(tabulate(tx_data, headers=["Message Type", intf+"(TX)"], tablefmt='simple', stralign='right') + "\n") - -# -# 'dhcp4relay_counters' group ### -# - - -@click.group(cls=clicommon.AliasedGroup, name="dhcp4relay_counters") -def dhcp4relay_counters(): - """Show DHCPv4 counter""" - pass - - -def ipv4_counters(interface): - config_db.connect() - feature_tbl = config_db.get_table("FEATURE") - if is_dhcp_server_enabled(feature_tbl): - click.echo("Unsupport to check dhcp_relay ipv4 counter when dhcp_server feature is enabled") - return - counter = DHCPv4_Counter() - counter_intf = counter.get_interface() - - if interface: - print_dhcpv4_count(counter, interface) - else: - for intf in counter_intf: - print_dhcpv4_count(counter, intf) - - -# 'counts' subcommand ("show dhcp4relay_counters counts") -@dhcp4relay_counters.command('counts') -@click.option('-i', '--interface', required=False) -@click.option('--verbose', is_flag=True, help="Enable verbose output") -def counts(interface, verbose): - """Show dhcp4relay message counts""" - ipv4_counters(interface) - class DHCPv6_Counter(object): def __init__(self): @@ -130,37 +48,30 @@ def __init__(self): def get_interface(self): """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """ - interfaces = [] + vlans = [] for key in self.db.keys(self.db.STATE_DB): if DHCPv6_COUNTER_TABLE in key: - interfaces.append(key[21:]) - return interfaces + vlans.append(key[21:]) + return vlans - def get_dhcp6relay_msg_count(self, interface, dir): + def get_dhcp6relay_msg_count(self, interface, msg): """ Get count of a dhcp6relay message """ - value = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(dir)) - cnts = ast.literal_eval(str(value)) - data = [] - if cnts is not None: - for k, v in cnts.items(): - data.append([k, v]) + count = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(msg)) + data = [str(msg), count] return data def clear_table(self, interface): """ Reset all message counts to 0 """ - v6_cnts = {} - for msg in dhcpv6_messages: - v6_cnts[msg] = '0' - self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("RX"), str(v6_cnts)) - self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("TX"), str(v6_cnts)) + for msg in messages: + self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') -def print_dhcpv6_count(counter, intf): +def print_count(counter, intf): """Print count of each message""" - rx_data = counter.get_dhcp6relay_msg_count(intf, "RX") - print(tabulate(rx_data, headers=["Message Type", intf+"(RX)"], tablefmt='simple', stralign='right') + "\n") - tx_data = counter.get_dhcp6relay_msg_count(intf, "TX") - print(tabulate(tx_data, headers=["Message Type", intf+"(TX)"], tablefmt='simple', stralign='right') + "\n") + data = [] + for i in messages: + data.append(counter.get_dhcp6relay_msg_count(intf, i)) + print(tabulate(data, headers=["Message Type", intf], tablefmt='simple', stralign='right') + "\n") # @@ -179,10 +90,10 @@ def ipv6_counters(interface): counter_intf = counter.get_interface() if interface: - print_dhcpv6_count(counter, interface) + print_count(counter, interface) else: for intf in counter_intf: - print_dhcpv6_count(counter, intf) + print_count(counter, intf) # 'counts' subcommand ("show dhcp6relay_counters counts") @@ -191,6 +102,7 @@ def ipv6_counters(interface): @click.option('--verbose', is_flag=True, help="Enable verbose output") def counts(interface, verbose): """Show dhcp6relay message counts""" + ipv6_counters(interface) @@ -296,10 +208,6 @@ def dhcp_relay_ipv4_destination(): def dhcp_relay_ipv6_destination(): get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS, with_header=True) -@dhcp_relay_ipv4.command("counters") -@click.option('-i', '--interface', required=False) -def dhcp_relay_ip4counters(interface): - ipv4_counters(interface) @dhcp_relay_ipv6.command("counters") @click.option('-i', '--interface', required=False) @@ -308,7 +216,6 @@ def dhcp_relay_ip6counters(interface): def register(cli): - cli.add_command(dhcp4relay_counters) cli.add_command(dhcp6relay_counters) cli.add_command(dhcp_relay_helper) cli.add_command(dhcp_relay) diff --git a/sonic-slave-bullseye/Dockerfile.j2 b/sonic-slave-bullseye/Dockerfile.j2 index 7bcf438cca82..786ba28733ed 100644 --- a/sonic-slave-bullseye/Dockerfile.j2 +++ b/sonic-slave-bullseye/Dockerfile.j2 @@ -357,7 +357,6 @@ RUN apt-get update && apt-get install -y eatmydata && eatmydata apt-get install # For DHCP Monitor tool libexplain-dev \ libevent-dev \ - libjsoncpp-dev \ # For libyang swig \ # For build dtb