Skip to content

Commit 03ef272

Browse files
authored
[202012][vlan] Remove add field of vlanid to DHCP_RELAY table while adding vlan (#2681)
What I did Remove add field of vlanid to DHCP_RELAY table while add vlan which would cause conflict with yang model. How I did it Remove add field of vlanid to DHCP_RELAY table while add vlan Signed-off-by: Yaqiang Zhu <[email protected]>
1 parent e00a81a commit 03ef272

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

config/dhcp_relay.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ def validate_ips(ctx, ips, ip_version):
2424
ctx.fail("{} is not IPv{} address".format(ip, ip_version))
2525

2626

27-
def get_dhcp_servers(db, vlan_name, ctx, table_name, dhcp_servers_str):
28-
table = db.cfgdb.get_entry(table_name, vlan_name)
29-
if len(table.keys()) == 0:
30-
ctx.fail("{} doesn't exist".format(vlan_name))
27+
def get_dhcp_servers(db, vlan_name, ctx, table_name, dhcp_servers_str, check_is_exist=True):
28+
if check_is_exist:
29+
keys = db.cfgdb.get_keys(table_name)
30+
if vlan_name not in keys:
31+
ctx.fail("{} doesn't exist".format(vlan_name))
3132

33+
table = db.cfgdb.get_entry(table_name, vlan_name)
3234
dhcp_servers = table.get(dhcp_servers_str, [])
3335

3436
return dhcp_servers, table
@@ -46,7 +48,9 @@ def add_dhcp_relay(vid, dhcp_relay_ips, db, ip_version):
4648
ctx = click.get_current_context()
4749
# Verify ip addresses are valid
4850
validate_ips(ctx, dhcp_relay_ips, ip_version)
49-
dhcp_servers, table = get_dhcp_servers(db, vlan_name, ctx, table_name, dhcp_servers_str)
51+
# It's unnecessary for DHCPv6 Relay to verify entry exist
52+
check_config_exist = True if ip_version == 4 else False
53+
dhcp_servers, table = get_dhcp_servers(db, vlan_name, ctx, table_name, dhcp_servers_str, check_config_exist)
5054
added_ips = []
5155

5256
for dhcp_relay_ip in dhcp_relay_ips:
@@ -93,6 +97,9 @@ def del_dhcp_relay(vid, dhcp_relay_ips, db, ip_version):
9397
else:
9498
table[dhcp_servers_str] = dhcp_servers
9599

100+
if ip_version == 6 and len(table.keys()) == 0:
101+
table = None
102+
96103
db.cfgdb.set_entry(table_name, vlan_name, table)
97104
click.echo("Removed DHCP relay address [{}] from {}".format(",".join(dhcp_relay_ips), vlan_name))
98105
dhcp_relay_util.handle_restart_dhcp_relay_service()

config/vlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def add_vlan(db, vid):
4040
set_dhcp_relay_table('VLAN', db.cfgdb, vlan, {'vlanid': str(vid)})
4141

4242
# set dhcpv6_relay table
43-
set_dhcp_relay_table('DHCP_RELAY', db.cfgdb, vlan, {'vlanid': str(vid)})
43+
set_dhcp_relay_table('DHCP_RELAY', db.cfgdb, vlan, None)
4444
# We need to restart dhcp_relay service after dhcpv6_relay config change
4545
dhcp_relay_util.handle_restart_dhcp_relay_service()
4646

tests/config_dhcp_relay_test.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,25 @@ def setup_class(cls):
121121
os.environ['UTILITIES_UNIT_TESTING'] = "1"
122122
print("SETUP")
123123

124-
def test_config_dhcp_relay_add_del_with_nonexist_vlanid(self, ip_version, op):
124+
def test_config_dhcp_relay_add_del_with_nonexist_vlanid_ipv4(self, op):
125125
runner = CliRunner()
126126

127+
ip_version = "ipv4"
128+
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
129+
result = runner.invoke(config.config.commands["dhcp_relay"].commands[ip_version]
130+
.commands[IP_VER_TEST_PARAM_MAP[ip_version]["command"]]
131+
.commands[op], ["1001", IP_VER_TEST_PARAM_MAP[ip_version]["ips"][0]])
132+
print(result.exit_code)
133+
print(result.stdout)
134+
assert result.exit_code != 0
135+
assert "Error: Vlan1001 doesn't exist" in result.output
136+
assert mock_run_command.call_count == 0
137+
138+
def test_config_dhcp_relay_del_with_nonexist_vlanid_ipv6(self):
139+
runner = CliRunner()
140+
141+
op = "del"
142+
ip_version = "ipv6"
127143
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
128144
result = runner.invoke(config.config.commands["dhcp_relay"].commands[ip_version]
129145
.commands[IP_VER_TEST_PARAM_MAP[ip_version]["command"]]
@@ -257,3 +273,24 @@ def test_config_add_del_duplicate_dhcp_relay(self, ip_version, op):
257273
assert result.exit_code != 0
258274
assert "Error: Find duplicate DHCP relay ip {} in {} list".format(test_ip, op) in result.output
259275
assert mock_run_command.call_count == 0
276+
277+
def test_config_add_dhcp_relay_ipv6_with_non_entry(self):
278+
op = "add"
279+
ip_version = "ipv6"
280+
test_ip = IP_VER_TEST_PARAM_MAP[ip_version]["ips"][0]
281+
runner = CliRunner()
282+
db = Db()
283+
table = IP_VER_TEST_PARAM_MAP[ip_version]["table"]
284+
db.cfgdb.set_entry(table, "Vlan1000", None)
285+
assert db.cfgdb.get_entry(table, "Vlan1000") == {}
286+
assert len(db.cfgdb.get_keys(table)) == 0
287+
288+
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
289+
result = runner.invoke(config.config.commands["dhcp_relay"].commands[ip_version]
290+
.commands[IP_VER_TEST_PARAM_MAP[ip_version]["command"]]
291+
.commands[op], ["1000", test_ip], obj=db)
292+
print(result.exit_code)
293+
print(result.output)
294+
assert result.exit_code == 0
295+
assert db.cfgdb.get_entry(table, "Vlan1000") == {"dhcpv6_servers": [test_ip]}
296+
assert mock_run_command.call_count == 3

tests/vlan_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ def test_config_add_del_vlan_dhcp_relay(self, ip_version, mock_restart_dhcp_rela
857857
print(result.output)
858858
assert result.exit_code == 0
859859

860-
assert db.cfgdb.get_entry(IP_VERSION_PARAMS_MAP[ip_version]["table"], "Vlan1001") == DHCP_RELAY_TABLE_ENTRY
860+
exp_output = {"vlanid": "1001"} if ip_version == "ipv4" else {}
861+
assert db.cfgdb.get_entry(IP_VERSION_PARAMS_MAP[ip_version]["table"], "Vlan1001") == exp_output
861862

862863
# del vlan 1001
863864
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1001"], obj=db)

0 commit comments

Comments
 (0)