Skip to content

Commit

Permalink
[bgp][minigraph] Add support for parsing bgp_router_id in minigraph p…
Browse files Browse the repository at this point in the history
…arser
  • Loading branch information
yaqiangz committed Apr 25, 2024
1 parent 6f86bc2 commit e89628c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ def parse_cpg(cpg, hname, local_devices=[]):
myasn = None
bgp_peers_with_range = {}
bgp_sentinel_sessions = {}
bgp_router_id = None
for child in cpg:
tag = child.tag
if tag == str(QName(ns, "PeeringSessions")):
Expand Down Expand Up @@ -969,6 +970,9 @@ def parse_cpg(cpg, hname, local_devices=[]):
hostname = router.find(str(QName(ns1, "Hostname"))).text
if hostname.lower() == hname.lower():
myasn = asn
router_id = router.find(str(QName(ns1, "RouterID")))
if router_id is not None:
bgp_router_id = router_id.text
peers = router.find(str(QName(ns1, "Peers")))
for bgpPeer in peers.findall(str(QName(ns, "BGPPeer"))):
addr = bgpPeer.find(str(QName(ns, "Address"))).text
Expand Down Expand Up @@ -1013,7 +1017,7 @@ def filter_bad_asn(table):
bgp_internal_sessions = filter_bad_asn(bgp_internal_sessions)
bgp_voq_chassis_sessions = filter_bad_asn(bgp_voq_chassis_sessions)

return bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, myasn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions
return bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, myasn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions, bgp_router_id


def parse_meta(meta, hname):
Expand Down Expand Up @@ -1566,6 +1570,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
redundancy_type = None
qos_profile = None
rack_mgmt_map = None
bgp_router_id = None

hwsku_qn = QName(ns, "HwSku")
hostname_qn = QName(ns, "Hostname")
Expand All @@ -1592,7 +1597,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
if child.tag == str(QName(ns, "DpgDec")):
(intfs, lo_intfs, mvrf, mgmt_intf, voq_inband_intfs, vlans, vlan_members, dhcp_relay_table, pcs, pc_members, acls, acl_table_types, vni, tunnel_intfs, dpg_ecmp_content, static_routes, tunnel_intfs_qos_remap_config) = parse_dpg(child, hostname)
elif child.tag == str(QName(ns, "CpgDec")):
(bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions) = parse_cpg(child, hostname)
(bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions, bgp_router_id) = parse_cpg(child, hostname)
elif child.tag == str(QName(ns, "PngDec")):
(neighbors, devices, console_dev, console_port, mgmt_dev, mgmt_port, port_speed_png, console_ports, mux_cable_ports, png_ecmp_content) = parse_png(child, hostname, dpg_ecmp_content)
elif child.tag == str(QName(ns, "UngDec")):
Expand All @@ -1608,7 +1613,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
(intfs, lo_intfs, mvrf, mgmt_intf, voq_inband_intfs, vlans, vlan_members, dhcp_relay_table, pcs, pc_members, acls, acl_table_types, vni, tunnel_intfs, dpg_ecmp_content, static_routes, tunnel_intfs_qos_remap_config) = parse_dpg(child, asic_name)
host_lo_intfs = parse_host_loopback(child, hostname)
elif child.tag == str(QName(ns, "CpgDec")):
(bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions) = parse_cpg(child, asic_name, local_devices)
(bgp_sessions, bgp_internal_sessions, bgp_voq_chassis_sessions, bgp_asn, bgp_peers_with_range, bgp_monitors, bgp_sentinel_sessions, bgp_router_id) = parse_cpg(child, asic_name, local_devices)
elif child.tag == str(QName(ns, "PngDec")):
(neighbors, devices, port_speed_png) = parse_asic_png(child, asic_name, hostname)
elif child.tag == str(QName(ns, "MetadataDeclaration")):
Expand Down Expand Up @@ -1643,6 +1648,9 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
'yang_config_validation': 'disable'
}
}

if bgp_router_id is not None:
results['DEVICE_METADATA']['localhost']['bgp_router_id'] = bgp_router_id

if deployment_id is not None:
results['DEVICE_METADATA']['localhost']['deployment_id'] = deployment_id
Expand Down
1 change: 1 addition & 0 deletions src/sonic-config-engine/tests/simple-sample-graph-case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Routers xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:BGPRouterDeclaration>
<a:ASN>65100</a:ASN>
<a:RouterID>8.8.8.8</a:RouterID>
<a:Hostname>switch-t0</a:Hostname>
<a:Peers>
<BGPPeer>
Expand Down
5 changes: 5 additions & 0 deletions src/sonic-config-engine/tests/test_minigraph_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ def test_minigraph_rack_mgmt_map(self):
output = self.run_script(argument)
self.assertEqual(output.strip(), "dummy_value")

def test_minigraph_bgp_router_id(self):
argument = ['-m', self.sample_graph, '-p', self.port_config, '-v', "DEVICE_METADATA[\'localhost\'][\'bgp_router_id\']"]
output = self.run_script(argument)
self.assertEqual(output.strip(), "8.8.8.8")

def test_minigraph_cluster(self):
argument = ['-m', self.sample_graph, '-p', self.port_config, '-v', "DEVICE_METADATA[\'localhost\'][\'cluster\']"]
output = self.run_script(argument)
Expand Down

0 comments on commit e89628c

Please sign in to comment.