Skip to content

Commit ea064e2

Browse files
authored
Parse DHCP_RELAY table (#8877)
* Cherry-pick Parse DHCP Table * Cherry pick changing DHCP table to DHCP_RELAY
1 parent 90cfc08 commit ea064e2

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/sonic-config-engine/minigraph.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def parse_dpg(dpg, hname):
516516
vlanintfs = child.find(str(QName(ns, "VlanInterfaces")))
517517
vlans = {}
518518
vlan_members = {}
519+
dhcp_relay_table = {}
519520
vlantype_name = ""
520521
intf_vlan_mbr = defaultdict(list)
521522
for vintf in vlanintfs.findall(str(QName(ns, "VlanInterface"))):
@@ -543,6 +544,7 @@ def parse_dpg(dpg, hname):
543544
vlan_members[(sonic_vlan_member_name, vmbr_list[i])] = {'tagging_mode': 'untagged'}
544545

545546
vlan_attributes = {'vlanid': vlanid, 'members': vmbr_list }
547+
dhcp_attributes = {}
546548

547549
# If this VLAN requires a DHCP relay agent, it will contain a <DhcpRelays> element
548550
# containing a list of DHCP server IPs
@@ -557,6 +559,9 @@ def parse_dpg(dpg, hname):
557559
vintfdhcpservers = vintf_node.text
558560
vdhcpserver_list = vintfdhcpservers.split(';')
559561
vlan_attributes['dhcpv6_servers'] = vdhcpserver_list
562+
dhcp_attributes['dhcpv6_servers'] = vdhcpserver_list
563+
sonic_vlan_member_name = "Vlan%s" % (vlanid)
564+
dhcp_relay_table[sonic_vlan_member_name] = dhcp_attributes
560565

561566
vlanmac = vintf.find(str(QName(ns, "MacAddress")))
562567
if vlanmac is not None and vlanmac.text is not None:
@@ -681,8 +686,8 @@ def parse_dpg(dpg, hname):
681686
if mg_key in mg_tunnel.attrib:
682687
tunnelintfs[tunnel_type][tunnel_name][table_key] = mg_tunnel.attrib[mg_key]
683688

684-
return intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls, vni, tunnelintfs, dpg_ecmp_content
685-
return None, None, None, None, None, None, None, None, None, None
689+
return intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, dhcp_relay_table, pcs, pc_members, acls, vni, tunnelintfs, dpg_ecmp_content
690+
return None, None, None, None, None, None, None, None, None, None, None, None, None
686691

687692
def parse_host_loopback(dpg, hname):
688693
for child in dpg:
@@ -1108,6 +1113,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
11081113
tunnel_intfs = None
11091114
vlans = None
11101115
vlan_members = None
1116+
dhcp_relay_table = None
11111117
pcs = None
11121118
mgmt_intf = None
11131119
lo_intfs = None
@@ -1167,7 +1173,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
11671173
for child in root:
11681174
if asic_name is None:
11691175
if child.tag == str(QName(ns, "DpgDec")):
1170-
(intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls, vni, tunnel_intfs, dpg_ecmp_content) = parse_dpg(child, hostname)
1176+
(intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, dhcp_relay_table, pcs, pc_members, acls, vni, tunnel_intfs, dpg_ecmp_content) = parse_dpg(child, hostname)
11711177
elif child.tag == str(QName(ns, "CpgDec")):
11721178
(bgp_sessions, bgp_internal_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors) = parse_cpg(child, hostname)
11731179
elif child.tag == str(QName(ns, "PngDec")):
@@ -1182,7 +1188,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
11821188
(port_speeds_default, port_descriptions) = parse_deviceinfo(child, hwsku)
11831189
else:
11841190
if child.tag == str(QName(ns, "DpgDec")):
1185-
(intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls, vni, tunnel_intfs, dpg_ecmp_content) = parse_dpg(child, asic_name)
1191+
(intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, dhcp_relay_table, pcs, pc_members, acls, vni, tunnel_intfs, dpg_ecmp_content) = parse_dpg(child, asic_name)
11861192
host_lo_intfs = parse_host_loopback(child, hostname)
11871193
elif child.tag == str(QName(ns, "CpgDec")):
11881194
(bgp_sessions, bgp_internal_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors) = parse_cpg(child, asic_name, local_devices)
@@ -1499,7 +1505,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
14991505
results['DEVICE_NEIGHBOR_METADATA'] = { key:devices[key] for key in devices if key in {device['name'] for device in neighbors.values()} }
15001506
results['SYSLOG_SERVER'] = dict((item, {}) for item in syslog_servers)
15011507
results['DHCP_SERVER'] = dict((item, {}) for item in dhcp_servers)
1502-
results['DHCPv6_SERVER'] = dict((item, {}) for item in dhcpv6_servers)
1508+
results['DHCP_RELAY'] = dhcp_relay_table
15031509
results['NTP_SERVER'] = dict((item, {}) for item in ntp_servers)
15041510
results['TACPLUS_SERVER'] = dict((item, {'priority': '1', 'tcp_port': '49'}) for item in tacacs_servers)
15051511
results['ACL_TABLE'] = filter_acl_table_bindings(acls, neighbors, pcs, sub_role)

src/sonic-config-engine/tests/simple-sample-graph-case.xml

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Name>ab1</Name>
135135
<AttachTo>fortyGigE0/8</AttachTo>
136136
<DhcpRelays>192.0.0.1;192.0.0.2</DhcpRelays>
137+
<Dhcpv6Relays>fc02:2000::1;fc02:2000::2</Dhcpv6Relays>
137138
<VlanID>1000</VlanID>
138139
<Tag>1000</Tag>
139140
<Subnets>192.168.0.0/27</Subnets>
@@ -143,6 +144,7 @@
143144
<Name>ab2</Name>
144145
<AttachTo>fortyGigE0/4</AttachTo>
145146
<DhcpRelays>192.0.0.1</DhcpRelays>
147+
<Dhcpv6Relays>fc02:2000::3;fc02:2000::4</Dhcpv6Relays>
146148
<VlanID>2000</VlanID>
147149
<Tag>2000</Tag>
148150
<MacAddress i:nil="true"/>

src/sonic-config-engine/tests/test_cfggen.py

+11
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,14 @@ def test_show_run_interfaces(self):
684684
argument = '-a \'{"key1":"value"}\' --var-json INTERFACE'
685685
output = self.run_script(argument)
686686
self.assertEqual(output, '')
687+
688+
def test_minigraph_dhcp(self):
689+
argument = '-m "' + self.sample_graph_simple_case + '" -p "' + self.port_config + '" -v DHCP_RELAY'
690+
output = self.run_script(argument)
691+
self.assertEqual(
692+
utils.to_dict(output.strip()),
693+
utils.to_dict(
694+
"{'Vlan1000': {'dhcpv6_servers': ['fc02:2000::1', 'fc02:2000::2']}, "
695+
"'Vlan2000': {'dhcpv6_servers': ['fc02:2000::3', 'fc02:2000::4']}}"
696+
)
697+
)

src/sonic-config-engine/tests/test_minigraph_case.py

+26
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ def test_minigraph_vlans(self):
102102
'Vlan1000': {
103103
'alias': 'ab1',
104104
'dhcp_servers': ['192.0.0.1', '192.0.0.2'],
105+
'dhcpv6_servers': ['fc02:2000::1', 'fc02:2000::2'],
105106
'vlanid': '1000',
106107
'mac': '00:aa:bb:cc:dd:ee',
107108
'members': ['Ethernet8']
108109
},
109110
'Vlan2000': {
110111
'alias': 'ab2',
111112
'dhcp_servers': ['192.0.0.1'],
113+
'dhcpv6_servers': ['fc02:2000::3', 'fc02:2000::4'],
112114
'members': ['Ethernet4'],
113115
'vlanid': '2000'
114116
}
@@ -357,3 +359,27 @@ def test_minigraph_mux_cable_table(self):
357359
utils.to_dict(output.strip()),
358360
expected_table
359361
)
362+
363+
def test_dhcp_table(self):
364+
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "DHCP_RELAY"'
365+
expected = {
366+
'Vlan1000': {
367+
'dhcpv6_servers': [
368+
"fc02:2000::1",
369+
"fc02:2000::2"
370+
]
371+
},
372+
'Vlan2000': {
373+
'dhcpv6_servers': [
374+
"fc02:2000::3",
375+
"fc02:2000::4"
376+
]
377+
}
378+
}
379+
output = self.run_script(argument)
380+
self.assertEqual(
381+
utils.to_dict(output.strip()),
382+
expected
383+
)
384+
385+

0 commit comments

Comments
 (0)