Parse DHCP Table

This commit is contained in:
kellyyeh 2021-08-12 14:32:23 -07:00 committed by Ying Xie
parent 139a58be49
commit 27a2222629
4 changed files with 75 additions and 3 deletions

View File

@ -235,6 +235,29 @@ def parse_dpg(dpg, hname):
vlan_attributes['alias'] = vintfname vlan_attributes['alias'] = vintfname
vlans[sonic_vlan_name] = vlan_attributes vlans[sonic_vlan_name] = vlan_attributes
dhcp = child.find(str(QName(ns, "Dhcp")))
dhcp_table = {}
if dhcp is not None:
for vintf in dhcp.findall(str(QName(ns, "VlanInterface"))):
vintfname = vintf.find(str(QName(ns, "Name"))).text
dhcp_attributes = {}
dhcp_node = vintf.find(str(QName(ns, "Dhcpv6Relays")))
if dhcp_node is not None and dhcp_node.text is not None:
dhcpservers = dhcp_node.text
vdhcpserver_list = dhcpservers.split(';')
dhcp_attributes['dhcpv6_servers'] = vdhcpserver_list
option_linklayer_addr = vintf.find(str(QName(ns, "Dhcpv6OptionRfc6939")))
if option_linklayer_addr is not None and option_linklayer_addr.text == "true":
dhcp_attributes['dhcpv6_option|rfc6939_support'] = "true"
elif option_linklayer_addr is not None and option_linklayer_addr.text == "false":
dhcp_attributes['dhcpv6_option|rfc6939_support'] = "false"
dhcp_table[vintfname] = dhcp_attributes
aclintfs = child.find(str(QName(ns, "AclInterfaces"))) aclintfs = child.find(str(QName(ns, "AclInterfaces")))
acls = {} acls = {}
for aclintf in aclintfs.findall(str(QName(ns, "AclInterface"))): for aclintf in aclintfs.findall(str(QName(ns, "AclInterface"))):
@ -320,7 +343,7 @@ def parse_dpg(dpg, hname):
except: except:
print >> sys.stderr, "Warning: Ignoring Control Plane ACL %s without type" % aclname print >> sys.stderr, "Warning: Ignoring Control Plane ACL %s without type" % aclname
return intfs, lo_intfs, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls return intfs, lo_intfs, mgmt_intf, vlans, vlan_members, dhcp_table, pcs, pc_members, acls
return None, None, None, None, None, None, None return None, None, None, None, None, None, None
@ -532,6 +555,7 @@ def parse_xml(filename, platform=None, port_config_file=None):
pc_intfs = None pc_intfs = None
vlans = None vlans = None
vlan_members = None vlan_members = None
dhcp_table = None
pcs = None pcs = None
mgmt_intf = None mgmt_intf = None
lo_intf = None lo_intf = None
@ -569,7 +593,7 @@ def parse_xml(filename, platform=None, port_config_file=None):
port_alias_map.update(alias_map) port_alias_map.update(alias_map)
for child in root: for child in root:
if child.tag == str(QName(ns, "DpgDec")): if child.tag == str(QName(ns, "DpgDec")):
(intfs, lo_intfs, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls) = parse_dpg(child, hostname) (intfs, lo_intfs, mgmt_intf, vlans, vlan_members, dhcp_table, pcs, pc_members, acls) = parse_dpg(child, hostname)
elif child.tag == str(QName(ns, "CpgDec")): elif child.tag == str(QName(ns, "CpgDec")):
(bgp_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors) = parse_cpg(child, hostname) (bgp_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors) = parse_cpg(child, hostname)
elif child.tag == str(QName(ns, "PngDec")): elif child.tag == str(QName(ns, "PngDec")):
@ -743,7 +767,7 @@ def parse_xml(filename, platform=None, port_config_file=None):
results['DEVICE_NEIGHBOR_METADATA'] = { key:devices[key] for key in devices if key.lower() != hostname.lower() } results['DEVICE_NEIGHBOR_METADATA'] = { key:devices[key] for key in devices if key.lower() != hostname.lower() }
results['SYSLOG_SERVER'] = dict((item, {}) for item in syslog_servers) results['SYSLOG_SERVER'] = dict((item, {}) for item in syslog_servers)
results['DHCP_SERVER'] = dict((item, {}) for item in dhcp_servers) results['DHCP_SERVER'] = dict((item, {}) for item in dhcp_servers)
results['DHCPv6_SERVER'] = dict((item, {}) for item in dhcpv6_servers) results['DHCP'] = dhcp_table
results['NTP_SERVER'] = dict((item, {}) for item in ntp_servers) results['NTP_SERVER'] = dict((item, {}) for item in ntp_servers)
results['TACPLUS_SERVER'] = dict((item, {'priority': '1', 'tcp_port': '49'}) for item in tacacs_servers) results['TACPLUS_SERVER'] = dict((item, {'priority': '1', 'tcp_port': '49'}) for item in tacacs_servers)

View File

@ -136,6 +136,18 @@
<Subnets>192.168.0.0/27</Subnets> <Subnets>192.168.0.0/27</Subnets>
</VlanInterface> </VlanInterface>
</VlanInterfaces> </VlanInterfaces>
<Dhcp>
<VlanInterface>
<Name>Vlan1000</Name>
<Dhcpv6Relays>fc02:2000::1;fc02:2000::2</Dhcpv6Relays>
<Dhcpv6OptionRfc6939>true</Dhcpv6OptionRfc6939>
</VlanInterface>
<VlanInterface>
<Name>Vlan2000</Name>
<Dhcpv6Relays>fc02:2000::3;fc02:2000::4</Dhcpv6Relays>
<Dhcpv6OptionRfc6939>false</Dhcpv6OptionRfc6939>
</VlanInterface>
</Dhcp>
<IPInterfaces> <IPInterfaces>
<IPInterface> <IPInterface>
<Name i:nil="true"/> <Name i:nil="true"/>

View File

@ -10,6 +10,7 @@ class TestCfgGen(TestCase):
self.sample_graph = os.path.join(self.test_dir, 'sample_graph.xml') self.sample_graph = os.path.join(self.test_dir, 'sample_graph.xml')
self.sample_graph_t0 = os.path.join(self.test_dir, 't0-sample-graph.xml') self.sample_graph_t0 = os.path.join(self.test_dir, 't0-sample-graph.xml')
self.sample_graph_simple = os.path.join(self.test_dir, 'simple-sample-graph.xml') self.sample_graph_simple = os.path.join(self.test_dir, 'simple-sample-graph.xml')
self.sample_graph_simple_case = os.path.join(self.test_dir, 'simple-sample-graph-case.xml')
self.sample_graph_metadata = os.path.join(self.test_dir, 'simple-sample-graph-metadata.xml') self.sample_graph_metadata = os.path.join(self.test_dir, 'simple-sample-graph-metadata.xml')
self.sample_graph_pc_test = os.path.join(self.test_dir, 'pc-test-graph.xml') self.sample_graph_pc_test = os.path.join(self.test_dir, 'pc-test-graph.xml')
self.sample_graph_bgp_speaker = os.path.join(self.test_dir, 't0-sample-bgp-speaker.xml') self.sample_graph_bgp_speaker = os.path.join(self.test_dir, 't0-sample-bgp-speaker.xml')
@ -333,3 +334,12 @@ class TestCfgGen(TestCase):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "BGP_MONITORS"' argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "BGP_MONITORS"'
output = self.run_script(argument) output = self.run_script(argument)
self.assertEqual(output.strip(), "{'10.20.30.40': {'rrclient': 0, 'name': 'BGPMonitor', 'local_addr': '10.1.0.32', 'nhopself': 0, 'holdtime': '10', 'asn': '0', 'keepalive': '3'}}") self.assertEqual(output.strip(), "{'10.20.30.40': {'rrclient': 0, 'name': 'BGPMonitor', 'local_addr': '10.1.0.32', 'nhopself': 0, 'holdtime': '10', 'asn': '0', 'keepalive': '3'}}")
def test_minigraph_dhcp(self):
argument = '-m "' + self.sample_graph_simple_case + '" -p "' + self.port_config + '" -v DHCP'
output = self.run_script(argument)
self.assertEqual(
output.strip(),
"{'Vlan1000': {'dhcpv6_option|rfc6939_support': 'true', 'dhcpv6_servers': ['fc02:2000::1', 'fc02:2000::2']}, 'Vlan2000': {'dhcpv6_option|rfc6939_support': 'false', 'dhcpv6_servers': ['fc02:2000::3', 'fc02:2000::4']}}"
)

View File

@ -138,3 +138,29 @@ class TestCfgGenCaseInsensitive(TestCase):
everflow_dscp_entry['ports'].sort(), everflow_dscp_entry['ports'].sort(),
expected_ports.sort() expected_ports.sort()
) )
def test_dhcp_table(self):
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "DHCP"'
expected = {
'Vlan1000': {
'dhcpv6_servers': [
"fc02:2000::1",
"fc02:2000::2"
],
'dhcpv6_option|rfc6939_support': 'true'
},
'Vlan2000': {
'dhcpv6_servers': [
"fc02:2000::3",
"fc02:2000::4"
],
'dhcpv6_option|rfc6939_support': 'false'
}
}
output = self.run_script(argument)
self.assertEqual(
output.strip(),
"{'Vlan1000': {'dhcpv6_option|rfc6939_support': 'true', 'dhcpv6_servers': ['fc02:2000::1', 'fc02:2000::2']}, 'Vlan2000': {'dhcpv6_option|rfc6939_support': 'false', 'dhcpv6_servers': ['fc02:2000::3', 'fc02:2000::4']}}"
)