11import click
22import utilities_common .cli as clicommon
3+ import utilities_common .dhcp_relay_util as dhcp_relay_util
34
45from jsonpatch import JsonPatchConflict
56from time import sleep
67from .utils import log
78from .validated_config_db_connector import ValidatedConfigDBConnector
89
910ADHOC_VALIDATION = True
11+ IGNORE_DHCP_RELAY_SYSTEM_EXIT_ERROR = False
1012
1113#
1214# 'vlan' group ('config vlan ...')
@@ -16,6 +18,11 @@ def vlan():
1618 """VLAN-related configuration tasks"""
1719 pass
1820
21+
22+ def set_dhcp_relay_table (table , config_db , vlan_name , value ):
23+ config_db .set_entry (table , vlan_name , value )
24+
25+
1926@vlan .command ('add' )
2027@click .argument ('vid' , metavar = '<vid>' , required = True , type = int )
2128@clicommon .pass_db
@@ -24,22 +31,23 @@ def add_vlan(db, vid):
2431
2532 ctx = click .get_current_context ()
2633 vlan = 'Vlan{}' .format (vid )
27-
34+
2835 config_db = ValidatedConfigDBConnector (db .cfgdb )
2936 if ADHOC_VALIDATION :
3037 if not clicommon .is_vlanid_in_range (vid ):
3138 ctx .fail ("Invalid VLAN ID {} (1-4094)" .format (vid ))
3239
3340 if vid == 1 :
3441 ctx .fail ("{} is default VLAN" .format (vlan )) # TODO: MISSING CONSTRAINT IN YANG MODEL
35-
42+
3643 if clicommon .check_if_vlanid_exist (db .cfgdb , vlan ): # TODO: MISSING CONSTRAINT IN YANG MODEL
3744 ctx .fail ("{} already exists" .format (vlan ))
38-
39- try :
40- config_db .set_entry ('VLAN' , vlan , {'vlanid' : str (vid )})
41- except ValueError :
42- ctx .fail ("Invalid VLAN ID {} (1-4094)" .format (vid ))
45+ # set dhcpv4_relay table
46+ set_dhcp_relay_table ('VLAN' , config_db , vlan , {'vlanid' : str (vid )})
47+
48+ # set dhcpv6_relay table
49+ set_dhcp_relay_table ('DHCP_RELAY' , config_db , vlan , {'vlanid' : str (vid )})
50+
4351
4452@vlan .command ('del' )
4553@click .argument ('vid' , metavar = '<vid>' , required = True , type = int )
@@ -67,19 +75,23 @@ def del_vlan(db, vid):
6775 ctx .fail ("{} can not be removed. First remove IP addresses assigned to this VLAN" .format (vlan ))
6876
6977 keys = [ (k , v ) for k , v in db .cfgdb .get_table ('VLAN_MEMBER' ) if k == 'Vlan{}' .format (vid ) ]
70-
78+
7179 if keys : # TODO: MISSING CONSTRAINT IN YANG MODEL
7280 ctx .fail ("VLAN ID {} can not be removed. First remove all members assigned to this VLAN." .format (vid ))
73-
81+
7482 vxlan_table = db .cfgdb .get_table ('VXLAN_TUNNEL_MAP' )
7583 for vxmap_key , vxmap_data in vxlan_table .items ():
7684 if vxmap_data ['vlan' ] == 'Vlan{}' .format (vid ):
7785 ctx .fail ("vlan: {} can not be removed. First remove vxlan mapping '{}' assigned to VLAN" .format (vid , '|' .join (vxmap_key )) )
78-
79- try :
80- config_db .set_entry ('VLAN' , 'Vlan{}' .format (vid ), None )
81- except JsonPatchConflict :
82- ctx .fail ("{} does not exist" .format (vlan ))
86+
87+ # set dhcpv4_relay table
88+ set_dhcp_relay_table ('VLAN' , config_db , vlan , None )
89+
90+ # set dhcpv6_relay table
91+ set_dhcp_relay_table ('DHCP_RELAY' , config_db , vlan , None )
92+ # We need to restart dhcp_relay service after dhcpv6_relay config change
93+ dhcp_relay_util .handle_restart_dhcp_relay_service (IGNORE_DHCP_RELAY_SYSTEM_EXIT_ERROR )
94+
8395
8496def restart_ndppd ():
8597 verify_swss_running_cmd = "docker container inspect -f '{{.State.Status}}' swss"
0 commit comments