From 453d41907f9a1d33741494eab5a16323e7f7060b Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Wed, 25 Aug 2021 17:52:25 +0000 Subject: [PATCH 01/13] Add CLI support for ipv6 helper and DHCPv6 Relay counter --- .../clear/plugins/dhcp6relay_counter_clear.py | 23 ++++++ .../show/plugins/show_dhcp6relay_counters.py | 78 +++++++++++++++++++ .../cli/show/plugins/show_dhcpv6_helper.py | 31 ++++++++ 3 files changed, 132 insertions(+) create mode 100644 dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py create mode 100644 dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py create mode 100644 dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py new file mode 100644 index 000000000000..13becf0f1d4f --- /dev/null +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py @@ -0,0 +1,23 @@ +import click +sys.path.insert(0, '../../show/plugins/') +import show_dhcp6relay_counters + + +# sonic-clear dhcp6relay_counters +@click.command() +@click.option('-i', '--interface', required=False) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def dhcp6relay_counter_clear(interface, verbose): + """Clear dhcp6relay message counts""" + + counter = DHCPv6_Counter() + counter_intf = counter.get_interface() + + if interface: + counter.clear_table(interface) + else: + for intf in counter_intf: + counter.clear_table(intf) + +def register(cli): + cli.add_command(dhcp6relay_counter_clear) \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py new file mode 100644 index 000000000000..9bf4c38f4b87 --- /dev/null +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py @@ -0,0 +1,78 @@ +import click +import utilities_common.cli as clicommon + +import argparse +import sys +from tabulate import tabulate + +from swsscommon.swsscommon import SonicV2Connector + + +# STATE_DB Table +DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE' + +# DHCPv6 Counter Messages +messages = ["Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", "Relay-Forward", "Relay-Reply"] + +class DHCPv6_Counter(object): + def __init__(self): + self.db = SonicV2Connector(use_unix_socket_path=False) + self.db.connect(self.db.STATE_DB) + self.table_name = DHCPv6_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB) + + + def get_interface(self): + vlans = [] + for key in self.db.keys(self.db.STATE_DB): + if DHCPv6_COUNTER_TABLE in key: + vlans.append(key[21:]) + return vlans + + + def get_dhcp6relay_msg_count(self, interface, msg): + 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): + for msg in messages: + self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') + +def print_count(counter, intf): + 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") + + +# +# 'dhcp6relay_counters' group ### +# + + +@click.group(cls=clicommon.AliasedGroup) +def dhcp6relay_counters(): + """Show DHCPv6 counter""" + pass + + +# 'counts' subcommand ("show dhcp6relay_counters counts") +@dhcp6relay_counters.command('counts') +@click.option('-i', '--interface', required=False) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def counts(interface, verbose): + """Show dhcp6relay message counts""" + + counter = DHCPv6_Counter() + counter_intf = counter.get_interface() + + if interface: + print_count(counter, interface) + else: + for intf in counter_intf: + print_count(counter, intf) + + +def register(cli): + cli.commands['dhcp6realy_counters'].add_command(dhcp6relay_counters) \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py new file mode 100644 index 000000000000..e1af8cc35124 --- /dev/null +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -0,0 +1,31 @@ +import click +from tabulate import tabulate +from swsscommon.swsscommon import ConfigDBConnector + +import utilities_common.cli as clicommon + +DHCP_RELAY = 'DHCP_RELAY' + +@click.group(cls=clicommon.AliasedGroup, name="dhcp_relay_helper") +def dhcp_relay_helper(): + """Show DHCP_Relay helper information""" + pass + +@dhcp_relay_helper.command('ip') +def get_dhcpv6_helper_address(): + config_db = ConfigDBConnector() + if config_db is not None: + config_db.connect() + table_data = config_db.get_table(DHCP_RELAY) + if table_data is not None: + for vlan in config_db.get_keys(DHCP_RELAY): + vlan_data = table_data.get(vlan) + helpers_data = vlan_data.get('dhcpv6_server') + if helpers_data is not None: + addr = {vlan:[]} + for ip in helpers_data.split(','): + addr[vlan].append(ip) + print(tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n') + +def register(cli): + cli.commands['dhcp_relay_helper'].add_command(dhcp_relay_helper) \ No newline at end of file From 41cb3c230d3d4294da7e56ed623e275983221c4b Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Wed, 25 Aug 2021 17:54:36 +0000 Subject: [PATCH 02/13] Add new lines at the end --- .../cli/clear/plugins/dhcp6relay_counter_clear.py | 3 ++- .../cli/show/plugins/show_dhcp6relay_counters.py | 3 ++- .../docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py index 13becf0f1d4f..69933ba29661 100644 --- a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py @@ -20,4 +20,5 @@ def dhcp6relay_counter_clear(interface, verbose): counter.clear_table(intf) def register(cli): - cli.add_command(dhcp6relay_counter_clear) \ No newline at end of file + cli.add_command(dhcp6relay_counter_clear) + \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py index 9bf4c38f4b87..bbaba69d7f83 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py @@ -75,4 +75,5 @@ def counts(interface, verbose): def register(cli): - cli.commands['dhcp6realy_counters'].add_command(dhcp6relay_counters) \ No newline at end of file + cli.commands['dhcp6realy_counters'].add_command(dhcp6relay_counters) + \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index e1af8cc35124..dd0baabbd08e 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -28,4 +28,5 @@ def get_dhcpv6_helper_address(): print(tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n') def register(cli): - cli.commands['dhcp_relay_helper'].add_command(dhcp_relay_helper) \ No newline at end of file + cli.commands['dhcp_relay_helper'].add_command(dhcp_relay_helper) + \ No newline at end of file From 4308fda7e680da6426aff40864dd8cef71341f3e Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Wed, 25 Aug 2021 18:00:27 +0000 Subject: [PATCH 03/13] Updated naming --- .../cli/clear/plugins/dhcp6relay_counter_clear.py | 5 ++--- .../docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py index 69933ba29661..76265c59fa60 100644 --- a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py @@ -7,7 +7,7 @@ @click.command() @click.option('-i', '--interface', required=False) @click.option('--verbose', is_flag=True, help="Enable verbose output") -def dhcp6relay_counter_clear(interface, verbose): +def dhcp6relay_counters(interface, verbose): """Clear dhcp6relay message counts""" counter = DHCPv6_Counter() @@ -20,5 +20,4 @@ def dhcp6relay_counter_clear(interface, verbose): counter.clear_table(intf) def register(cli): - cli.add_command(dhcp6relay_counter_clear) - \ No newline at end of file + cli.add_command(dhcp6relay_counters) diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index dd0baabbd08e..68220413cc63 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -11,7 +11,7 @@ def dhcp_relay_helper(): """Show DHCP_Relay helper information""" pass -@dhcp_relay_helper.command('ip') +@dhcp_relay_helper.command('ipv6') def get_dhcpv6_helper_address(): config_db = ConfigDBConnector() if config_db is not None: @@ -29,4 +29,3 @@ def get_dhcpv6_helper_address(): def register(cli): cli.commands['dhcp_relay_helper'].add_command(dhcp_relay_helper) - \ No newline at end of file From 3d257b8f3a81b686ac31905b3aa76b5d262497fa Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Fri, 27 Aug 2021 20:27:49 +0000 Subject: [PATCH 04/13] Added code comments and updated docker files --- ...unter_clear.py => clear_dhcp6relay_counter.py} | 15 +++++++++------ .../cli/show/plugins/show_dhcp6relay_counters.py | 11 +++++++---- .../cli/show/plugins/show_dhcpv6_helper.py | 6 +++++- files/build_templates/manifest.json.j2 | 3 ++- rules/docker-dhcp-relay.mk | 4 ++++ rules/functions | 1 + 6 files changed, 28 insertions(+), 12 deletions(-) rename dockers/docker-dhcp-relay/cli/clear/plugins/{dhcp6relay_counter_clear.py => clear_dhcp6relay_counter.py} (56%) diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py similarity index 56% rename from dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py rename to dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py index 76265c59fa60..0dd2dd242981 100644 --- a/dockers/docker-dhcp-relay/cli/clear/plugins/dhcp6relay_counter_clear.py +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py @@ -1,14 +1,14 @@ +import sys import click sys.path.insert(0, '../../show/plugins/') -import show_dhcp6relay_counters +from show_dhcp6relay_counters impot DHCPv6_Counter # sonic-clear dhcp6relay_counters -@click.command() +@click.command('dhcp6relay_counters') @click.option('-i', '--interface', required=False) -@click.option('--verbose', is_flag=True, help="Enable verbose output") -def dhcp6relay_counters(interface, verbose): - """Clear dhcp6relay message counts""" +def dhcp6relay_clear_counters(interface): + """ Clear dhcp6relay message counts """ counter = DHCPv6_Counter() counter_intf = counter.get_interface() @@ -20,4 +20,7 @@ def dhcp6relay_counters(interface, verbose): counter.clear_table(intf) def register(cli): - cli.add_command(dhcp6relay_counters) + cli.add_command(dhcp6relay_clear_counters) + +if __name__ == '__main__': + dhcp6relay_clear_counters() diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py index bbaba69d7f83..676f2513fd22 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py @@ -1,8 +1,6 @@ import click import utilities_common.cli as clicommon -import argparse -import sys from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector @@ -22,6 +20,7 @@ def __init__(self): def get_interface(self): + """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """ vlans = [] for key in self.db.keys(self.db.STATE_DB): if DHCPv6_COUNTER_TABLE in key: @@ -30,12 +29,14 @@ def get_interface(self): def get_dhcp6relay_msg_count(self, interface, msg): + """ Get count of a dhcp6relay message """ 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 """ for msg in messages: self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') @@ -75,5 +76,7 @@ def counts(interface, verbose): def register(cli): - cli.commands['dhcp6realy_counters'].add_command(dhcp6relay_counters) - \ No newline at end of file + cli.add_command(dhcp6relay_counters) + +if __name__ == '__main__': + dhcp6relay_counters() diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index 68220413cc63..0ae4e37b97e6 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -28,4 +28,8 @@ def get_dhcpv6_helper_address(): print(tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n') def register(cli): - cli.commands['dhcp_relay_helper'].add_command(dhcp_relay_helper) + cli.add_command(dhcp_relay_helper) + +if __name__ == '__main__': + dhcp_relay_helper() + \ No newline at end of file diff --git a/files/build_templates/manifest.json.j2 b/files/build_templates/manifest.json.j2 index 9f3e872d2e65..431b2dd22432 100644 --- a/files/build_templates/manifest.json.j2 +++ b/files/build_templates/manifest.json.j2 @@ -29,6 +29,7 @@ }, "cli": { "config": "{{ config_cli_plugin|default('') }}", - "show": "{{ show_cli_plugin|default('') }}" + "show": "{{ show_cli_plugin|default('') }}", + "clear": "{{ clear_cli_plugin|default('') }}" } } diff --git a/rules/docker-dhcp-relay.mk b/rules/docker-dhcp-relay.mk index a8b33c5bc74c..aeeb3c5ddf0a 100644 --- a/rules/docker-dhcp-relay.mk +++ b/rules/docker-dhcp-relay.mk @@ -53,5 +53,9 @@ $(DOCKER_DHCP_RELAY)_CONTAINER_TMPFS += /var/tmp/ $(DOCKER_DHCP_RELAY)_CLI_CONFIG_PLUGIN = /cli/config/plugins/dhcp_relay.py $(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp_relay.py +$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp6relay_counters.py +$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp_helper.py +$(DOCKER_DHCP_RELAY)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/clear_dhcp6relay_counter.py +$(DOCKER_DHCP_RELAY)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/ipv7.py $(DOCKER_DHCP_RELAY)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT) diff --git a/rules/functions b/rules/functions index 5873b2db1b87..6f62b87797c8 100644 --- a/rules/functions +++ b/rules/functions @@ -196,6 +196,7 @@ define generate_manifest $(eval export tmpfs=$($(1).gz_CONTAINER_TMPFS)) $(eval export config_cli_plugin=$($(1).gz_CLI_CONFIG_PLUGIN)) $(eval export show_cli_plugin=$($(1).gz_CLI_SHOW_PLUGIN)) + $(eval export clear_cli_plugin=$($(1).gz_CLI_CLEAR_PLUGIN)) j2 $($*.gz_PATH)/Dockerfile$(2).j2 > $($(1).gz_PATH)/Dockerfile$(2) j2 --customize scripts/j2cli/json_filter.py files/build_templates/manifest.json.j2 > $($(1).gz_PATH)/manifest.common.json if [ -f $($*.gz_PATH)/manifest.part.json.j2 ]; then From 20424a05baf88202681d1d1f7533c5dfa8d65b84 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Fri, 27 Aug 2021 21:26:53 +0000 Subject: [PATCH 05/13] Updated clear counter --- .../cli/clear/plugins/clear_dhcp6relay_counter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py index 0dd2dd242981..b38c3909b040 100644 --- a/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py @@ -1,7 +1,7 @@ import sys import click sys.path.insert(0, '../../show/plugins/') -from show_dhcp6relay_counters impot DHCPv6_Counter +from show_dhcp6relay_counters import DHCPv6_Counter # sonic-clear dhcp6relay_counters From 3669d737e1ab909a91733b27947f253853c8d252 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Fri, 27 Aug 2021 23:09:35 +0000 Subject: [PATCH 06/13] Updated show_dhcpv6_helper naming --- rules/docker-dhcp-relay.mk | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rules/docker-dhcp-relay.mk b/rules/docker-dhcp-relay.mk index aeeb3c5ddf0a..2f07b12586c2 100644 --- a/rules/docker-dhcp-relay.mk +++ b/rules/docker-dhcp-relay.mk @@ -54,8 +54,7 @@ $(DOCKER_DHCP_RELAY)_CONTAINER_TMPFS += /var/tmp/ $(DOCKER_DHCP_RELAY)_CLI_CONFIG_PLUGIN = /cli/config/plugins/dhcp_relay.py $(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp_relay.py $(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp6relay_counters.py -$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp_helper.py +$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcpv6_helper.py $(DOCKER_DHCP_RELAY)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/clear_dhcp6relay_counter.py -$(DOCKER_DHCP_RELAY)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/ipv7.py $(DOCKER_DHCP_RELAY)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT) From dc5ec41a9cbebd06b28911232d84db23c9c93f47 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Tue, 7 Sep 2021 16:06:10 +0000 Subject: [PATCH 07/13] Added cli-plugin-tests --- .../test_show_dhcp6relay_counters.py | 29 +++++++++++++++++++ .../test_show_dhcpv6_helper.py | 19 ++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py create mode 100644 dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py 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 new file mode 100644 index 000000000000..2b604f3a61bd --- /dev/null +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py @@ -0,0 +1,29 @@ +import show.main as show +from click.testing import CliRunner + + +expected_counts = """\ + Message Type +-------------- -- + Solicit + Advertise + Request + Confirm + Renew + Rebind + Reply + Release + Decline + Relay-Forward + Relay-Reply + +""" +class TestDhcp6RelayCounters(object): + + def test_show_counts(self): + runner = CliRunner() + #runner.invoke(clear.cli.commands["dhcp6relay-counters"], []) + result = runner.invoke(show.cli.commands["dhcp6relay-counters"].commands["counts"], []) + print(result.output) + assert result.output == expected_counts + diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py new file mode 100644 index 000000000000..72f55a4af5c2 --- /dev/null +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -0,0 +1,19 @@ +import subprocess +import show.main as show +from click.testing import CliRunner + +expected_table = """\ +-------- ------------ +Vlan1000 fc02:2000::1 +-------- ------------ + +""" +class TestDhcpRelayHelper(object): + + def test_show_dhcpv6_helper(self): + runner = CliRunner() + subprocess.run('sonic-db-cli CONFIG_DB hmset "DHCP_RELAY|Vlan1000" "fc02:2000::1"', shell=True) + result = runner.invoke(show.cli.commands["dhcp_relay_helper"].commands["ipv6"], []) + print(result.output) + assert result.output == expected_table + From ca16c7a241fe0ef04c112200c24ce370e9004ea7 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Thu, 16 Sep 2021 23:23:59 +0000 Subject: [PATCH 08/13] Update dhcp relay CLI test --- .../test_show_dhcp6relay_counters.py | 28 ++++++++++++++----- .../test_show_dhcpv6_helper.py | 23 +++++++++++---- .../clear/plugins/clear_dhcp6relay_counter.py | 8 +++++- .../show/plugins/show_dhcp6relay_counters.py | 1 + .../cli/show/plugins/show_dhcpv6_helper.py | 8 ++++-- 5 files changed, 53 insertions(+), 15 deletions(-) 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 2b604f3a61bd..25494e9a17b6 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 @@ -1,10 +1,25 @@ -import show.main as show +import sys +import os +from unittest import mock +sys.path.append('../cli/show/plugins/') +import show_dhcp6relay_counters as show + from click.testing import CliRunner +try: + modules_path = os.path.join(os.path.dirname(__file__), "../../../src/sonic-utilities") + test_path = os.path.join(modules_path, "tests") + mock_table_path = os.path.join(test_path, "mock_tables") + sys.path.insert(0, modules_path) + sys.path.insert(0, test_path) + sys.path.insert(0, mock_table_path) + import dbconnector +except KeyError: + pass expected_counts = """\ - Message Type --------------- -- + Message Type Vlan1000 +-------------- ----------- Solicit Advertise Request @@ -18,12 +33,11 @@ Relay-Reply """ + class TestDhcp6RelayCounters(object): - def test_show_counts(self): + def test_show_counts(self): runner = CliRunner() - #runner.invoke(clear.cli.commands["dhcp6relay-counters"], []) - result = runner.invoke(show.cli.commands["dhcp6relay-counters"].commands["counts"], []) - print(result.output) + result = runner.invoke(show.dhcp6relay_counters.commands["counts"], ["-i Vlan1000"]) assert result.output == expected_counts diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index 72f55a4af5c2..0ea3552230e5 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -1,10 +1,25 @@ -import subprocess -import show.main as show +import sys +import os +from unittest import mock +sys.path.append('../cli/show/plugins/') +import show_dhcpv6_helper as show from click.testing import CliRunner +try: + modules_path = os.path.join(os.path.dirname(__file__), "../../../src/sonic-utilities") + test_path = os.path.join(modules_path, "tests") + mock_table_path = os.path.join(test_path, "mock_tables") + sys.path.insert(0, modules_path) + sys.path.insert(0, test_path) + sys.path.insert(0, mock_table_path) + import dbconnector +except KeyError: + pass + expected_table = """\ -------- ------------ Vlan1000 fc02:2000::1 + fc02:2000::2 -------- ------------ """ @@ -12,8 +27,6 @@ class TestDhcpRelayHelper(object): def test_show_dhcpv6_helper(self): runner = CliRunner() - subprocess.run('sonic-db-cli CONFIG_DB hmset "DHCP_RELAY|Vlan1000" "fc02:2000::1"', shell=True) - result = runner.invoke(show.cli.commands["dhcp_relay_helper"].commands["ipv6"], []) - print(result.output) + result = runner.invoke(show.dhcp_relay_helper.commands["ipv6"], []) assert result.output == expected_table diff --git a/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py index b38c3909b040..7b0d5097d2ec 100644 --- a/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py +++ b/dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py @@ -3,9 +3,15 @@ sys.path.insert(0, '../../show/plugins/') from show_dhcp6relay_counters import DHCPv6_Counter +import utilities_common.cli as clicommon + # sonic-clear dhcp6relay_counters -@click.command('dhcp6relay_counters') +@click.group(cls=clicommon.AliasedGroup) +def dhcp6relay_clear(): + pass + +@dhcp6relay_clear.command('dhcp6relay_counters') @click.option('-i', '--interface', required=False) def dhcp6relay_clear_counters(interface): """ Clear dhcp6relay message counts """ diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py index 676f2513fd22..d97a51d0fa78 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py @@ -41,6 +41,7 @@ def clear_table(self, interface): self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') def print_count(counter, intf): + """Print count of each message""" data = [] for i in messages: data.append(counter.get_dhcp6relay_msg_count(intf, i)) diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index 0ae4e37b97e6..684b3625f659 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -1,6 +1,8 @@ import click from tabulate import tabulate from swsscommon.swsscommon import ConfigDBConnector +import os +import sys import utilities_common.cli as clicommon @@ -13,14 +15,16 @@ def dhcp_relay_helper(): @dhcp_relay_helper.command('ipv6') def get_dhcpv6_helper_address(): + """Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format""" config_db = ConfigDBConnector() if config_db is not None: config_db.connect() table_data = config_db.get_table(DHCP_RELAY) if table_data is not None: - for vlan in config_db.get_keys(DHCP_RELAY): + vlans = config_db.get_keys(DHCP_RELAY) + for vlan in vlans: vlan_data = table_data.get(vlan) - helpers_data = vlan_data.get('dhcpv6_server') + helpers_data = vlan_data.get('dhcpv6_servers') if helpers_data is not None: addr = {vlan:[]} for ip in helpers_data.split(','): From 9e6e1edc087fee614f5427989aa5a7f0f3ddc099 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Tue, 21 Sep 2021 20:28:23 +0000 Subject: [PATCH 09/13] Add mock config db --- .../cli-plugin-tests/mock_config.py | 17 ++++++++++ .../test_show_dhcpv6_helper.py | 32 ++++++++++++------- .../cli/show/plugins/show_dhcpv6_helper.py | 22 ++++++++----- sonic-slave-buster/Dockerfile.j2 | 4 +++ sonic-slave-stretch/Dockerfile.j2 | 4 +++ 5 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py new file mode 100644 index 000000000000..3279457f4339 --- /dev/null +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py @@ -0,0 +1,17 @@ +from unittest.mock import call + +TEST_DATA = [ + [ + "DHCPv6_Helpers", + { + "config_db": { + "DHCP_RELAY": { + "Vlan1000": { + "dhcpv6_servers": "fc02:2000::1,fc02:2000::2", + "dhcpv6_option|rfc6939_support": "true" + } + } + }, + }, + ], +] \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index 0ea3552230e5..aab587ecaef6 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -1,18 +1,18 @@ import sys import os -from unittest import mock +import pytest sys.path.append('../cli/show/plugins/') import show_dhcpv6_helper as show from click.testing import CliRunner +from swsscommon import swsscommon +from mock_config import TEST_DATA +from parameterized import parameterized +from pyfakefs.fake_filesystem_unittest import patchfs try: - modules_path = os.path.join(os.path.dirname(__file__), "../../../src/sonic-utilities") - test_path = os.path.join(modules_path, "tests") - mock_table_path = os.path.join(test_path, "mock_tables") - sys.path.insert(0, modules_path) - sys.path.insert(0, test_path) - sys.path.insert(0, mock_table_path) - import dbconnector + sys.path.insert(0, '../../../src/sonic-host-services/tests/common') + from mock_configdb import MockConfigDb + swsscommon.ConfigDBConnector = MockConfigDb except KeyError: pass @@ -21,12 +21,20 @@ Vlan1000 fc02:2000::1 fc02:2000::2 -------- ------------ - """ + +DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json' + class TestDhcpRelayHelper(object): - def test_show_dhcpv6_helper(self): + @parameterized.expand(TEST_DATA) + @patchfs + def test_show_dhcpv6_helper(self, test_name, test_data, fs): + if not os.path.exists(DBCONFIG_PATH): + fs.create_file(DBCONFIG_PATH) + MockConfigDb.set_config_db(test_data["config_db"]) runner = CliRunner() - result = runner.invoke(show.dhcp_relay_helper.commands["ipv6"], []) - assert result.output == expected_table + table = MockConfigDb.get_table(self, "DHCP_RELAY") + result = show.get_data(table, "Vlan1000") + assert result == expected_table diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index 684b3625f659..f22907df294d 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -7,6 +7,7 @@ import utilities_common.cli as clicommon DHCP_RELAY = 'DHCP_RELAY' +config_db = ConfigDBConnector() @click.group(cls=clicommon.AliasedGroup, name="dhcp_relay_helper") def dhcp_relay_helper(): @@ -16,20 +17,25 @@ def dhcp_relay_helper(): @dhcp_relay_helper.command('ipv6') def get_dhcpv6_helper_address(): """Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format""" - config_db = ConfigDBConnector() if config_db is not None: config_db.connect() table_data = config_db.get_table(DHCP_RELAY) if table_data is not None: vlans = config_db.get_keys(DHCP_RELAY) for vlan in vlans: - vlan_data = table_data.get(vlan) - helpers_data = vlan_data.get('dhcpv6_servers') - if helpers_data is not None: - addr = {vlan:[]} - for ip in helpers_data.split(','): - addr[vlan].append(ip) - print(tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n') + output = get_data(table_data, vlan) + print(output) + + +def get_data(table_data, vlan): + vlan_data = table_data.get(vlan) + helpers_data = vlan_data.get('dhcpv6_servers') + if helpers_data is not None: + addr = {vlan:[]} + for ip in helpers_data.split(','): + addr[vlan].append(ip) + output = tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n' + return output def register(cli): cli.add_command(dhcp_relay_helper) diff --git a/sonic-slave-buster/Dockerfile.j2 b/sonic-slave-buster/Dockerfile.j2 index bbd68a31396e..f23c1aeb8c17 100644 --- a/sonic-slave-buster/Dockerfile.j2 +++ b/sonic-slave-buster/Dockerfile.j2 @@ -486,6 +486,10 @@ EXPOSE 22 RUN git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git /usr/share/depot_tools ENV PATH /usr/share/depot_tools:$PATH +# Install dependencies for dhcp relay test +RUN pip3 install parameterized==0.8.1 +RUN pip3 install pyfakefs + # Install docker engine 17.03.2~ce-0 inside docker and enable experimental feature RUN apt-get update RUN apt-get install -y \ diff --git a/sonic-slave-stretch/Dockerfile.j2 b/sonic-slave-stretch/Dockerfile.j2 index cc45afbc6f82..e077c69e01b3 100644 --- a/sonic-slave-stretch/Dockerfile.j2 +++ b/sonic-slave-stretch/Dockerfile.j2 @@ -291,6 +291,10 @@ RUN apt-get update && apt-get install -y \ python-lxml \ libexpat1-dev +# Install dependencies for dhcp relay test +RUN pip3 install parameterized==0.8.1 +RUN pip3 install pyfakefs + ## Config dpkg ## install the configuration file if it’s currently missing RUN sudo augtool --autosave "set /files/etc/dpkg/dpkg.cfg/force-confmiss" From 9abff86de8cb526634e5258b0cf7ef768d1ead88 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Tue, 21 Sep 2021 21:27:00 +0000 Subject: [PATCH 10/13] Remove unused import --- dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py | 2 -- .../cli-plugin-tests/test_show_dhcpv6_helper.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py index 3279457f4339..850debdce313 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py @@ -1,5 +1,3 @@ -from unittest.mock import call - TEST_DATA = [ [ "DHCPv6_Helpers", diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index aab587ecaef6..af1c0689d12a 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -1,5 +1,3 @@ -import sys -import os import pytest sys.path.append('../cli/show/plugins/') import show_dhcpv6_helper as show From 35bbde9f23fa1766c736fe2f0c5f52f156f8e90b Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Wed, 22 Sep 2021 10:33:47 +0000 Subject: [PATCH 11/13] Remove unused import --- .../cli-plugin-tests/test_show_dhcpv6_helper.py | 2 ++ .../docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index af1c0689d12a..b39666c28aeb 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -1,4 +1,6 @@ import pytest +import sys +import os sys.path.append('../cli/show/plugins/') import show_dhcpv6_helper as show from click.testing import CliRunner diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index f22907df294d..e3b33b475033 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -1,8 +1,6 @@ import click from tabulate import tabulate from swsscommon.swsscommon import ConfigDBConnector -import os -import sys import utilities_common.cli as clicommon From 38a0c9af2a02edac8305e4fd3d29fb4668af23db Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Thu, 23 Sep 2021 11:38:26 +0000 Subject: [PATCH 12/13] Merge show plugins into show_dhcp_relay.py --- .../cli-plugin-tests/mock_config.py | 5 +- .../test_show_dhcp6relay_counters.py | 2 +- .../test_show_dhcpv6_helper.py | 2 +- .../show/plugins/show_dhcp6relay_counters.py | 83 ------------- .../cli/show/plugins/show_dhcp_relay.py | 113 +++++++++++++++++- .../cli/show/plugins/show_dhcpv6_helper.py | 43 ------- rules/docker-dhcp-relay.mk | 2 - 7 files changed, 118 insertions(+), 132 deletions(-) delete mode 100644 dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py delete mode 100644 dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py index 850debdce313..9954c750b4d9 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py @@ -5,7 +5,10 @@ "config_db": { "DHCP_RELAY": { "Vlan1000": { - "dhcpv6_servers": "fc02:2000::1,fc02:2000::2", + "dhcpv6_servers": [ + "fc02:2000::1", + "fc02:2000::2" + ], "dhcpv6_option|rfc6939_support": "true" } } 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 25494e9a17b6..f640ef1de6ce 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 @@ -2,7 +2,7 @@ import os from unittest import mock sys.path.append('../cli/show/plugins/') -import show_dhcp6relay_counters as show +import show_dhcp_relay as show from click.testing import CliRunner diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index b39666c28aeb..1f079c5ac965 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -2,7 +2,7 @@ import sys import os sys.path.append('../cli/show/plugins/') -import show_dhcpv6_helper as show +import show_dhcp_relay as show from click.testing import CliRunner from swsscommon import swsscommon from mock_config import TEST_DATA diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py deleted file mode 100644 index d97a51d0fa78..000000000000 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp6relay_counters.py +++ /dev/null @@ -1,83 +0,0 @@ -import click -import utilities_common.cli as clicommon - -from tabulate import tabulate - -from swsscommon.swsscommon import SonicV2Connector - - -# STATE_DB Table -DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE' - -# DHCPv6 Counter Messages -messages = ["Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", "Relay-Forward", "Relay-Reply"] - -class DHCPv6_Counter(object): - def __init__(self): - self.db = SonicV2Connector(use_unix_socket_path=False) - self.db.connect(self.db.STATE_DB) - self.table_name = DHCPv6_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB) - - - def get_interface(self): - """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """ - vlans = [] - for key in self.db.keys(self.db.STATE_DB): - if DHCPv6_COUNTER_TABLE in key: - vlans.append(key[21:]) - return vlans - - - def get_dhcp6relay_msg_count(self, interface, msg): - """ Get count of a dhcp6relay message """ - 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 """ - for msg in messages: - self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') - -def print_count(counter, intf): - """Print count of each message""" - 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") - - -# -# 'dhcp6relay_counters' group ### -# - - -@click.group(cls=clicommon.AliasedGroup) -def dhcp6relay_counters(): - """Show DHCPv6 counter""" - pass - - -# 'counts' subcommand ("show dhcp6relay_counters counts") -@dhcp6relay_counters.command('counts') -@click.option('-i', '--interface', required=False) -@click.option('--verbose', is_flag=True, help="Enable verbose output") -def counts(interface, verbose): - """Show dhcp6relay message counts""" - - counter = DHCPv6_Counter() - counter_intf = counter.get_interface() - - if interface: - print_count(counter, interface) - else: - for intf in counter_intf: - print_count(counter, intf) - - -def register(cli): - cli.add_command(dhcp6relay_counters) - -if __name__ == '__main__': - dhcp6relay_counters() 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 ae8c453e45f3..c66a584d6ee2 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,22 @@ +import click from natsort import natsorted +from tabulate import tabulate import show.vlan as vlan +import utilities_common.cli as clicommon + +from swsscommon.swsscommon import ConfigDBConnector +from swsscommon.swsscommon import SonicV2Connector + + +# STATE_DB Table +DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE' + +# DHCPv6 Counter Messages +messages = ["Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", "Relay-Forward", "Relay-Reply"] + +# DHCP_RELAY Config Table +DHCP_RELAY = 'DHCP_RELAY' +config_db = ConfigDBConnector() def get_dhcp_helper_address(ctx, vlan): cfg, _ = ctx @@ -16,5 +33,99 @@ def get_dhcp_helper_address(ctx, vlan): vlan.VlanBrief.register_column('DHCP Helper Address', get_dhcp_helper_address) -def register(cli): +class DHCPv6_Counter(object): + def __init__(self): + self.db = SonicV2Connector(use_unix_socket_path=False) + self.db.connect(self.db.STATE_DB) + self.table_name = DHCPv6_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB) + + + def get_interface(self): + """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """ + vlans = [] + for key in self.db.keys(self.db.STATE_DB): + if DHCPv6_COUNTER_TABLE in key: + vlans.append(key[21:]) + return vlans + + + def get_dhcp6relay_msg_count(self, interface, msg): + """ Get count of a dhcp6relay message """ + 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 """ + for msg in messages: + self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') + +def print_count(counter, intf): + """Print count of each message""" + 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") + + +# +# 'dhcp6relay_counters' group ### +# + + +@click.group(cls=clicommon.AliasedGroup, name="dhcp6relay_counters") +def dhcp6relay_counters(): + """Show DHCPv6 counter""" pass + + +# 'counts' subcommand ("show dhcp6relay_counters counts") +@dhcp6relay_counters.command('counts') +@click.option('-i', '--interface', required=False) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def counts(interface, verbose): + """Show dhcp6relay message counts""" + + counter = DHCPv6_Counter() + counter_intf = counter.get_interface() + + if interface: + print_count(counter, interface) + else: + for intf in counter_intf: + print_count(counter, intf) + + + +@click.group(cls=clicommon.AliasedGroup, name="dhcprelay_helper") +def dhcp_relay_helper(): + """Show DHCP_Relay helper information""" + pass + +@dhcp_relay_helper.command('ipv6') +def get_dhcpv6_helper_address(): + """Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format""" + if config_db is not None: + config_db.connect() + table_data = config_db.get_table(DHCP_RELAY) + if table_data is not None: + vlans = config_db.get_keys(DHCP_RELAY) + for vlan in vlans: + output = get_data(table_data, vlan) + print(output) + + +def get_data(table_data, vlan): + vlan_data = table_data.get(vlan) + helpers_data = vlan_data.get('dhcpv6_servers') + if helpers_data is not None: + addr = {vlan:[]} + for ip in helpers_data: + addr[vlan].append(ip) + output = tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n' + return output + +def register(cli): + cli.add_command(dhcp6relay_counters) + cli.add_command(dhcp_relay_helper) diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py deleted file mode 100644 index e3b33b475033..000000000000 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ /dev/null @@ -1,43 +0,0 @@ -import click -from tabulate import tabulate -from swsscommon.swsscommon import ConfigDBConnector - -import utilities_common.cli as clicommon - -DHCP_RELAY = 'DHCP_RELAY' -config_db = ConfigDBConnector() - -@click.group(cls=clicommon.AliasedGroup, name="dhcp_relay_helper") -def dhcp_relay_helper(): - """Show DHCP_Relay helper information""" - pass - -@dhcp_relay_helper.command('ipv6') -def get_dhcpv6_helper_address(): - """Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format""" - if config_db is not None: - config_db.connect() - table_data = config_db.get_table(DHCP_RELAY) - if table_data is not None: - vlans = config_db.get_keys(DHCP_RELAY) - for vlan in vlans: - output = get_data(table_data, vlan) - print(output) - - -def get_data(table_data, vlan): - vlan_data = table_data.get(vlan) - helpers_data = vlan_data.get('dhcpv6_servers') - if helpers_data is not None: - addr = {vlan:[]} - for ip in helpers_data.split(','): - addr[vlan].append(ip) - output = tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n' - return output - -def register(cli): - cli.add_command(dhcp_relay_helper) - -if __name__ == '__main__': - dhcp_relay_helper() - \ No newline at end of file diff --git a/rules/docker-dhcp-relay.mk b/rules/docker-dhcp-relay.mk index 2f07b12586c2..a8e67559b5bc 100644 --- a/rules/docker-dhcp-relay.mk +++ b/rules/docker-dhcp-relay.mk @@ -53,8 +53,6 @@ $(DOCKER_DHCP_RELAY)_CONTAINER_TMPFS += /var/tmp/ $(DOCKER_DHCP_RELAY)_CLI_CONFIG_PLUGIN = /cli/config/plugins/dhcp_relay.py $(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp_relay.py -$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcp6relay_counters.py -$(DOCKER_DHCP_RELAY)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_dhcpv6_helper.py $(DOCKER_DHCP_RELAY)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/clear_dhcp6relay_counter.py $(DOCKER_DHCP_RELAY)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT) From 057b55d1b6b49dce5f4e4ddb03c4706cc081db12 Mon Sep 17 00:00:00 2001 From: kellyyeh Date: Thu, 23 Sep 2021 11:39:42 +0000 Subject: [PATCH 13/13] Add new line at end --- dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py index 9954c750b4d9..ed04367fbba7 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py @@ -15,4 +15,4 @@ }, }, ], -] \ No newline at end of file +]