Skip to content

Commit

Permalink
[vlan][dhcp_relay] Clear dhcpv6 relay counter while deleting vlan (#2…
Browse files Browse the repository at this point in the history
…852)

What I did
Fix this issue: sonic-net/sonic-buildimage#15047
Show dhcp_relay ipv6 counter will display vlan which has been deleted.

How I did it
Remove related info in state_db while deleting a vlan

How to verify it
Add unit test
Build utilities and run cmd to verify

Signed-off-by: Yaqiang Zhu <yaqiangzhu@microsoft.com>
  • Loading branch information
yaqiangz authored and yxieca committed Jun 2, 2023
1 parent 051f28c commit d1f4413
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 11 additions & 0 deletions config/vlan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import utilities_common.cli as clicommon
import utilities_common.dhcp_relay_util as dhcp_relay_util
from swsscommon.swsscommon import SonicV2Connector

from time import sleep
from .utils import log
Expand Down Expand Up @@ -64,6 +65,14 @@ 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))
if exists:
state_db.delete(state_db.STATE_DB, 'DHCPv6_COUNTER_TABLE|{}'.format(entry_name))


@vlan.command('del')
@click.argument('vid', metavar='<vid>', required=True, type=int)
@click.option('--no_restart_dhcp_relay', is_flag=True, type=click.BOOL, required=False, default=False,
Expand Down Expand Up @@ -106,6 +115,8 @@ def del_vlan(db, vid, no_restart_dhcp_relay):
# set dhcpv4_relay table
set_dhcp_relay_table('VLAN', db.cfgdb, vlan, None)

delete_state_db_entry(vlan)

if not no_restart_dhcp_relay and is_dhcpv6_relay_config_exist(db, vlan):
# set dhcpv6_relay table
set_dhcp_relay_table(DHCP_RELAY_TABLE, db.cfgdb, vlan, None)
Expand Down
10 changes: 6 additions & 4 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,12 @@ def test_config_vlan_del_vlan(self, mock_restart_dhcp_relay_service):
print(result.output)
assert result.exit_code == 0

result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], 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")

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

0 comments on commit d1f4413

Please sign in to comment.