Skip to content

Commit

Permalink
[vlan] Refresh dhcpv6_relay config while adding/deleting a vlan (soni…
Browse files Browse the repository at this point in the history
…c-net#2660)

What I did
Currently, add/del a vlan doesn't change related dhcpv6_relay config, which is incorrect.

How I did it
1. Add dhcp_relay table init entry while adding vlan
2. Delete dhcp_relay related config while deleting vlan
3. Add unitest

How to verify it
1. By unitest
2. install whl and run cli

Signed-off-by: Yaqiang Zhu <[email protected]>
  • Loading branch information
yaqiangz committed Feb 10, 2023
1 parent 39cdb49 commit 0a74c18
Show file tree
Hide file tree
Showing 6 changed files with 774 additions and 11 deletions.
30 changes: 26 additions & 4 deletions config/vlan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import ipaddress
import utilities_common.cli as clicommon
import utilities_common.dhcp_relay_util as dhcp_relay_util

from time import sleep
from .utils import log
Expand All @@ -13,6 +14,11 @@ def vlan():
"""VLAN-related configuration tasks"""
pass


def set_dhcp_relay_table(table, config_db, vlan_name, value):
config_db.set_entry(table, vlan_name, value)


@vlan.command('add')
@click.argument('vid', metavar='<vid>', required=True, type=int)
@clicommon.pass_db
Expand All @@ -27,8 +33,17 @@ def add_vlan(db, vid):
vlan = 'Vlan{}'.format(vid)
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan):
ctx.fail("{} already exists".format(vlan))
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan, "DHCP_RELAY"):
ctx.fail("DHCPv6 relay config for {} already exists".format(vlan))

# set dhcpv4_relay table
set_dhcp_relay_table('VLAN', db.cfgdb, vlan, {'vlanid': str(vid)})

# set dhcpv6_relay table
set_dhcp_relay_table('DHCP_RELAY', db.cfgdb, vlan, {'vlanid': str(vid)})
# We need to restart dhcp_relay service after dhcpv6_relay config change
dhcp_relay_util.handle_restart_dhcp_relay_service()

db.cfgdb.set_entry('VLAN', vlan, {'vlanid': vid})

@vlan.command('del')
@click.argument('vid', metavar='<vid>', required=True, type=int)
Expand All @@ -54,11 +69,18 @@ def del_vlan(db, vid):
ctx.fail("{} can not be removed. First remove IP addresses assigned to this VLAN".format(vlan))

keys = [ (k, v) for k, v in db.cfgdb.get_table('VLAN_MEMBER') if k == 'Vlan{}'.format(vid) ]

if keys:
ctx.fail("VLAN ID {} can not be removed. First remove all members assigned to this VLAN.".format(vid))

db.cfgdb.set_entry('VLAN', 'Vlan{}'.format(vid), None)

# set dhcpv4_relay table
set_dhcp_relay_table('VLAN', db.cfgdb, vlan, None)

# set dhcpv6_relay table
set_dhcp_relay_table('DHCP_RELAY', db.cfgdb, vlan, None)
# We need to restart dhcp_relay service after dhcpv6_relay config change
dhcp_relay_util.handle_restart_dhcp_relay_service()


def restart_ndppd():
verify_swss_running_cmd = "docker container inspect -f '{{.State.Status}}' swss"
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .mock_tables import dbconnector
from . import show_ip_route_common
import utilities_common.constants as constants
import config.main as config

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
Expand Down Expand Up @@ -218,3 +219,14 @@ def setup_ip_route_commands():
import show.main as show

return show


@pytest.fixture(scope='function')
def mock_restart_dhcp_relay_service():
print("We are mocking restart dhcp_relay")
origin_func = config.vlan.dhcp_relay_util.handle_restart_dhcp_relay_service
config.vlan.dhcp_relay_util.handle_restart_dhcp_relay_service = mock.MagicMock(return_value=0)

yield

config.vlan.dhcp_relay_util.handle_restart_dhcp_relay_service = origin_func
Loading

0 comments on commit 0a74c18

Please sign in to comment.