Skip to content

Commit

Permalink
[dhcp_relay] Fix dhcp_relay counter display issue (#3054)
Browse files Browse the repository at this point in the history
Why I did it
Fix issue: sonic-net/sonic-buildimage#15047 of after deleting vlan member and vlan, the counters for for vlan / vlan member are still seen.

How I did it
Delete related counter entry in state_db when deleting vlan and vlan members.

How to verify it
All UTs passed
Manually test

Signed-off-by: Yaqiang Zhu <[email protected]>
  • Loading branch information
yaqiangz authored Nov 29, 2023
1 parent 6dfeee6 commit 96dd555
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
13 changes: 7 additions & 6 deletions config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ def is_dhcpv6_relay_config_exist(db, vlan_name):
return True


def delete_state_db_entry(entry_name):
state_db = SonicV2Connector()
state_db.connect(state_db.STATE_DB)
exists = state_db.exists(state_db.STATE_DB, 'DHCPv6_COUNTER_TABLE|{}'.format(entry_name))
def delete_db_entry(entry_name, db_connector, db_name):
exists = db_connector.exists(db_name, entry_name)
if exists:
state_db.delete(state_db.STATE_DB, 'DHCPv6_COUNTER_TABLE|{}'.format(entry_name))
db_connector.delete(db_name, entry_name)


@vlan.command('del')
Expand Down Expand Up @@ -125,7 +123,8 @@ def del_vlan(db, vid, no_restart_dhcp_relay):
# We need to restart dhcp_relay service after dhcpv6_relay config change
if is_dhcp_relay_running():
dhcp_relay_util.handle_restart_dhcp_relay_service()
delete_state_db_entry(vlan)
delete_db_entry("DHCPv6_COUNTER_TABLE|{}".format(vlan), db.db, db.db.STATE_DB)
delete_db_entry("DHCP_COUNTER_TABLE|{}".format(vlan), db.db, db.db.STATE_DB)

vlans = db.cfgdb.get_keys('VLAN')
if not vlans:
Expand Down Expand Up @@ -279,6 +278,8 @@ def del_vlan_member(db, vid, port):

try:
config_db.set_entry('VLAN_MEMBER', (vlan, port), None)
delete_db_entry("DHCPv6_COUNTER_TABLE|{}".format(port), db.db, db.db.STATE_DB)
delete_db_entry("DHCP_COUNTER_TABLE|{}".format(port), db.db, db.db.STATE_DB)
except JsonPatchConflict:
ctx.fail("{} invalid or does not exist, or {} is not a member of {}".format(vlan, port, vlan))

29 changes: 20 additions & 9 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,31 @@ def test_config_vlan_del_vlan(self, mock_restart_dhcp_relay_service):
assert result.exit_code != 0
assert "Error: VLAN ID 1000 can not be removed. First remove all members assigned to this VLAN." in result.output

vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
keys = [ (k, v) for k, v in vlan_member if k == 'Vlan{}'.format(1000) ]
for k,v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
with mock.patch("config.vlan.delete_db_entry") as delete_db_entry:
vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
keys = [ (k, v) for k, v in vlan_member if k == 'Vlan{}'.format(1000) ]
for k,v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0

with mock.patch("config.vlan.delete_state_db_entry") as delete_state_db_entry:
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
delete_state_db_entry.assert_called_once_with("Vlan1000")
delete_db_entry.assert_has_calls([
mock.call("DHCPv6_COUNTER_TABLE|Vlan1000", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet4", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet8", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet12", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet16", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Vlan1000", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet4", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet8", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet12", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet16", mock.ANY, db.db.STATE_DB)
], any_order=True)

# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
Expand Down

0 comments on commit 96dd555

Please sign in to comment.