Skip to content

Commit d07b13c

Browse files
yaqiangzmssonicbld
authored andcommitted
[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 9c1c82e. * Revert "[dhcp-relay]: dhcp/dhcpv6 per interface counter support (#16377)" This reverts commit a522a63. * Remove test_show_dhcp_relay_ipv4_counter_with_enabled_dhcp_server
1 parent be9c647 commit d07b13c

File tree

5 files changed

+38
-150
lines changed

5 files changed

+38
-150
lines changed

dockers/docker-dhcp-relay/Dockerfile.j2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ RUN apt-get install -y libjsoncpp-dev \
1919

2020
RUN pip3 install psutil
2121

22-
RUN apt-get install -y libjsoncpp-dev
23-
2422
{% if docker_dhcp_relay_debs.strip() -%}
2523
# Copy built Debian packages
2624
{{ copy_files("debs/", docker_dhcp_relay_debs.split(' '), "/debs/") }}

dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp6relay_counters.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
except KeyError:
1818
pass
1919

20-
expected_counts_v6 = """\
21-
Message Type Vlan1000(RX)
22-
-------------- ---------------
23-
24-
Message Type Vlan1000(TX)
25-
-------------- ---------------
26-
27-
"""
28-
29-
expected_counts_v4 = """\
30-
Message Type Vlan1000(RX)
31-
-------------- ---------------
32-
33-
Message Type Vlan1000(TX)
34-
-------------- ---------------
20+
expected_counts = """\
21+
Message Type Vlan1000
22+
------------------- -----------
23+
Unknown
24+
Solicit
25+
Advertise
26+
Request
27+
Confirm
28+
Renew
29+
Rebind
30+
Reply
31+
Release
32+
Decline
33+
Reconfigure
34+
Information-Request
35+
Relay-Forward
36+
Relay-Reply
37+
Malformed
3538
3639
"""
3740

@@ -40,14 +43,5 @@ class TestDhcp6RelayCounters(object):
4043
def test_show_counts(self):
4144
runner = CliRunner()
4245
result = runner.invoke(show.dhcp6relay_counters.commands["counts"], ["-i Vlan1000"])
43-
print(result.output)
44-
assert result.output == expected_counts_v6
45-
46-
class TestDhcpRelayCounters(object):
47-
48-
def test_show_counts(self):
49-
runner = CliRunner()
50-
result = runner.invoke(show.dhcp4relay_counters.commands["counts"], ["-i Vlan1000"])
51-
print(result.output)
52-
assert result.output == expected_counts_v4
46+
assert result.output == expected_counts
5347

dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ def test_show_multi_dhcp_relay(test_name, test_data, fs):
173173
assert result == expected_output
174174

175175

176-
def test_show_dhcp_relay_ipv4_counter_with_enabled_dhcp_server():
177-
with mock.patch.object(show, "is_dhcp_server_enabled", return_value=True), \
178-
mock.patch.object(swsscommon.ConfigDBConnector, "connect", return_value=None), \
179-
mock.patch.object(swsscommon.ConfigDBConnector, "get_table", return_value=None), \
180-
mock.patch.object(click, "echo", return_value=None) as mock_echo:
181-
show.ipv4_counters("Etherner1")
182-
expected_param = "Unsupport to check dhcp_relay ipv4 counter when dhcp_server feature is enabled"
183-
mock_echo.assert_called_once_with(expected_param)
184-
185-
186176
@pytest.mark.parametrize("enable_dhcp_server", [True, False])
187177
def test_is_dhcp_server_enabled(enable_dhcp_server):
188178
result = show.is_dhcp_server_enabled({"dhcp_server": {"state": "enabled" if enable_dhcp_server else "disabled"}})

dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py

Lines changed: 19 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import click
2-
import ast
32
from natsort import natsorted
43
from tabulate import tabulate
54
import show.vlan as show_vlan
@@ -8,20 +7,13 @@
87
from swsscommon.swsscommon import ConfigDBConnector
98
from swsscommon.swsscommon import SonicV2Connector
109

10+
1111
# STATE_DB Table
12-
DHCPv4_COUNTER_TABLE = 'DHCP_COUNTER_TABLE'
1312
DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE'
1413

15-
# DHCPv4 Counter Messages
16-
dhcpv4_messages = [
17-
"Unknown", "Discover", "Offer", "Request", "Decline", "Ack", "Nack", "Release", "Inform"
18-
]
19-
2014
# DHCPv6 Counter Messages
21-
dhcpv6_messages = [
22-
"Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release",
23-
"Decline", "Reconfigure", "Information-Request", "Relay-Forward", "Relay-Reply", "Malformed"
24-
]
15+
messages = ["Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline",
16+
"Reconfigure", "Information-Request", "Relay-Forward", "Relay-Reply", "Malformed"]
2517

2618
# DHCP_RELAY Config Table
2719
DHCP_RELAY = 'DHCP_RELAY'
@@ -47,80 +39,6 @@ def get_dhcp_helper_address(ctx, vlan):
4739

4840
show_vlan.VlanBrief.register_column('DHCP Helper Address', get_dhcp_helper_address)
4941

50-
class DHCPv4_Counter(object):
51-
def __init__(self):
52-
self.db = SonicV2Connector(use_unix_socket_path=False)
53-
self.db.connect(self.db.STATE_DB)
54-
self.table_name = DHCPv4_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB)
55-
56-
def get_interface(self):
57-
""" Get all names of all interfaces in DHCPv4_COUNTER_TABLE """
58-
interfaces = []
59-
for key in self.db.keys(self.db.STATE_DB):
60-
if DHCPv4_COUNTER_TABLE in key:
61-
interfaces.append(key[19:])
62-
return interfaces
63-
64-
def get_dhcp4relay_msg_count(self, interface, dir):
65-
""" Get count of a dhcprelay message """
66-
value = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(dir))
67-
cnts = ast.literal_eval(str(value))
68-
data = []
69-
if cnts is not None:
70-
for k, v in cnts.items():
71-
data.append([k, v])
72-
return data
73-
74-
def clear_table(self, interface):
75-
""" Reset all message counts to 0 """
76-
v4_cnts = {}
77-
for msg in dhcpv4_messages:
78-
v4_cnts[msg] = '0'
79-
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("RX"), str(v4_cnts))
80-
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("TX"), str(v4_cnts))
81-
82-
def print_dhcpv4_count(counter, intf):
83-
"""Print count of each message"""
84-
rx_data = counter.get_dhcp4relay_msg_count(intf, "RX")
85-
print(tabulate(rx_data, headers=["Message Type", intf+"(RX)"], tablefmt='simple', stralign='right') + "\n")
86-
tx_data = counter.get_dhcp4relay_msg_count(intf, "TX")
87-
print(tabulate(tx_data, headers=["Message Type", intf+"(TX)"], tablefmt='simple', stralign='right') + "\n")
88-
89-
#
90-
# 'dhcp4relay_counters' group ###
91-
#
92-
93-
94-
@click.group(cls=clicommon.AliasedGroup, name="dhcp4relay_counters")
95-
def dhcp4relay_counters():
96-
"""Show DHCPv4 counter"""
97-
pass
98-
99-
100-
def ipv4_counters(interface):
101-
config_db.connect()
102-
feature_tbl = config_db.get_table("FEATURE")
103-
if is_dhcp_server_enabled(feature_tbl):
104-
click.echo("Unsupport to check dhcp_relay ipv4 counter when dhcp_server feature is enabled")
105-
return
106-
counter = DHCPv4_Counter()
107-
counter_intf = counter.get_interface()
108-
109-
if interface:
110-
print_dhcpv4_count(counter, interface)
111-
else:
112-
for intf in counter_intf:
113-
print_dhcpv4_count(counter, intf)
114-
115-
116-
# 'counts' subcommand ("show dhcp4relay_counters counts")
117-
@dhcp4relay_counters.command('counts')
118-
@click.option('-i', '--interface', required=False)
119-
@click.option('--verbose', is_flag=True, help="Enable verbose output")
120-
def counts(interface, verbose):
121-
"""Show dhcp4relay message counts"""
122-
ipv4_counters(interface)
123-
12442

12543
class DHCPv6_Counter(object):
12644
def __init__(self):
@@ -130,37 +48,30 @@ def __init__(self):
13048

13149
def get_interface(self):
13250
""" Get all names of all interfaces in DHCPv6_COUNTER_TABLE """
133-
interfaces = []
51+
vlans = []
13452
for key in self.db.keys(self.db.STATE_DB):
13553
if DHCPv6_COUNTER_TABLE in key:
136-
interfaces.append(key[21:])
137-
return interfaces
54+
vlans.append(key[21:])
55+
return vlans
13856

139-
def get_dhcp6relay_msg_count(self, interface, dir):
57+
def get_dhcp6relay_msg_count(self, interface, msg):
14058
""" Get count of a dhcp6relay message """
141-
value = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(dir))
142-
cnts = ast.literal_eval(str(value))
143-
data = []
144-
if cnts is not None:
145-
for k, v in cnts.items():
146-
data.append([k, v])
59+
count = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(msg))
60+
data = [str(msg), count]
14761
return data
14862

14963
def clear_table(self, interface):
15064
""" Reset all message counts to 0 """
151-
v6_cnts = {}
152-
for msg in dhcpv6_messages:
153-
v6_cnts[msg] = '0'
154-
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("RX"), str(v6_cnts))
155-
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str("TX"), str(v6_cnts))
65+
for msg in messages:
66+
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0')
15667

15768

158-
def print_dhcpv6_count(counter, intf):
69+
def print_count(counter, intf):
15970
"""Print count of each message"""
160-
rx_data = counter.get_dhcp6relay_msg_count(intf, "RX")
161-
print(tabulate(rx_data, headers=["Message Type", intf+"(RX)"], tablefmt='simple', stralign='right') + "\n")
162-
tx_data = counter.get_dhcp6relay_msg_count(intf, "TX")
163-
print(tabulate(tx_data, headers=["Message Type", intf+"(TX)"], tablefmt='simple', stralign='right') + "\n")
71+
data = []
72+
for i in messages:
73+
data.append(counter.get_dhcp6relay_msg_count(intf, i))
74+
print(tabulate(data, headers=["Message Type", intf], tablefmt='simple', stralign='right') + "\n")
16475

16576

16677
#
@@ -179,10 +90,10 @@ def ipv6_counters(interface):
17990
counter_intf = counter.get_interface()
18091

18192
if interface:
182-
print_dhcpv6_count(counter, interface)
93+
print_count(counter, interface)
18394
else:
18495
for intf in counter_intf:
185-
print_dhcpv6_count(counter, intf)
96+
print_count(counter, intf)
18697

18798

18899
# 'counts' subcommand ("show dhcp6relay_counters counts")
@@ -191,6 +102,7 @@ def ipv6_counters(interface):
191102
@click.option('--verbose', is_flag=True, help="Enable verbose output")
192103
def counts(interface, verbose):
193104
"""Show dhcp6relay message counts"""
105+
194106
ipv6_counters(interface)
195107

196108

@@ -296,10 +208,6 @@ def dhcp_relay_ipv4_destination():
296208
def dhcp_relay_ipv6_destination():
297209
get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS, with_header=True)
298210

299-
@dhcp_relay_ipv4.command("counters")
300-
@click.option('-i', '--interface', required=False)
301-
def dhcp_relay_ip4counters(interface):
302-
ipv4_counters(interface)
303211

304212
@dhcp_relay_ipv6.command("counters")
305213
@click.option('-i', '--interface', required=False)
@@ -308,7 +216,6 @@ def dhcp_relay_ip6counters(interface):
308216

309217

310218
def register(cli):
311-
cli.add_command(dhcp4relay_counters)
312219
cli.add_command(dhcp6relay_counters)
313220
cli.add_command(dhcp_relay_helper)
314221
cli.add_command(dhcp_relay)

sonic-slave-bullseye/Dockerfile.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ RUN apt-get update && apt-get install -y eatmydata && eatmydata apt-get install
357357
# For DHCP Monitor tool
358358
libexplain-dev \
359359
libevent-dev \
360-
libjsoncpp-dev \
361360
# For libyang
362361
swig \
363362
# For build dtb

0 commit comments

Comments
 (0)