Skip to content

Commit f4e6e5b

Browse files
Fixing 'show ip bgp neighbor <ip>' in frr unified config mode (#3738)
* Fixing 'show ip bgp neighbor <ip>' in frr unified config mode by making sure to get the neighbor key from config_db either in 'x.x.x.x' format or alternatively in 'default|x.x.x.x' format as is the case with unified mode * fixing the neighbor table entry match to correctly show neighbor name in unified frr config mgmt mode
1 parent 9a18155 commit f4e6e5b

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

tests/mock_tables/asic0/config_db.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
"VLAN_MEMBER|Vlan1000|PortChannel1002": {
229229
"tagging_mode": "tagged"
230230
},
231-
"BGP_NEIGHBOR|10.0.0.1": {
231+
"BGP_NEIGHBOR|default|10.0.0.1": {
232232
"rrclient": "0",
233233
"name": "ARISTA01T2",
234234
"local_addr": "10.0.0.0",
@@ -238,7 +238,7 @@
238238
"asn": "65200",
239239
"keepalive": "3"
240240
},
241-
"BGP_NEIGHBOR|fc00::2": {
241+
"BGP_NEIGHBOR|default|fc00::2": {
242242
"rrclient": "0",
243243
"name": "ARISTA01T2",
244244
"local_addr": "fc00::1",

tests/mock_tables/config_db.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@
12771277
"bank": "1",
12781278
"link": "Ethernet12"
12791279
},
1280-
"BGP_NEIGHBOR|10.0.0.1": {
1280+
"BGP_NEIGHBOR|default|10.0.0.1": {
12811281
"asn": "65200",
12821282
"holdtime": "10",
12831283
"keepalive": "3",
@@ -1457,7 +1457,7 @@
14571457
"nhopself": "0",
14581458
"rrclient": "0"
14591459
},
1460-
"BGP_NEIGHBOR|10.0.0.57": {
1460+
"BGP_NEIGHBOR|default|10.0.0.57": {
14611461
"asn": "64013",
14621462
"holdtime": "10",
14631463
"keepalive": "3",
@@ -1682,7 +1682,7 @@
16821682
"nhopself": "0",
16831683
"rrclient": "0"
16841684
},
1685-
"BGP_NEIGHBOR|fc00::72": {
1685+
"BGP_NEIGHBOR|default|fc00::72": {
16861686
"asn": "64013",
16871687
"holdtime": "10",
16881688
"keepalive": "3",

utilities_common/bgp_util.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,30 @@ def get_namespace_for_bgp_neighbor(neighbor_ip):
2525

2626
def is_bgp_neigh_present(neighbor_ip, namespace=multi_asic.DEFAULT_NAMESPACE):
2727
config_db = multi_asic.connect_config_db_for_ns(namespace)
28-
#check the internal
29-
bgp_session = config_db.get_entry(multi_asic.BGP_NEIGH_CFG_DB_TABLE,
30-
neighbor_ip)
31-
if bgp_session:
32-
return True
3328

34-
bgp_session = config_db.get_entry(
35-
multi_asic.BGP_INTERNAL_NEIGH_CFG_DB_TABLE, neighbor_ip)
36-
if bgp_session:
37-
return True
29+
tables = [
30+
multi_asic.BGP_NEIGH_CFG_DB_TABLE,
31+
multi_asic.BGP_INTERNAL_NEIGH_CFG_DB_TABLE,
32+
]
33+
pattern = re.compile(rf".*\|{re.escape(neighbor_ip)}")
34+
35+
for table in tables:
36+
# Check for the neighbor_ip format
37+
if config_db.get_entry(table, neighbor_ip):
38+
return True
39+
40+
# Check for any string|neighbor_ip format using regex. This is needed
41+
# when unified routing config mode is enabled, as in that case
42+
# vrfname|neighbor_ip is the key instead of just neighbor_ip
43+
keys = config_db.get_keys(table)
44+
for key in keys:
45+
# Convert the key from tuple like ('default', 'x.x.x.x') to a string
46+
# like 'default|x.x.x.x'
47+
if isinstance(key, tuple):
48+
key_str = "|".join(key)
49+
if pattern.match(key_str) and config_db.get_entry(table, key):
50+
return True
51+
3852
return False
3953

4054

@@ -135,8 +149,18 @@ def get_bgp_neighbor_ip_to_name(ip, static_neighbors, dynamic_neighbors):
135149
:param dynamic_neighbors: subnet of dynamically defined neighbors dict
136150
:return: name of neighbor
137151
"""
152+
# Direct IP match
138153
if ip in static_neighbors:
139154
return static_neighbors[ip]
155+
# Try to find the key where IP is the second element of any tuple key.
156+
# This is to handle the case where the key is a tuple like (vrfname, IP)
157+
# when unified routing config mode is enabled
158+
elif matching_key := next(
159+
(key for key in static_neighbors.keys()
160+
if isinstance(key, tuple) and len(key) == 2 and key[1] == ip),
161+
None
162+
):
163+
return static_neighbors[matching_key]
140164
elif is_ipv4_address(ip):
141165
for subnet in dynamic_neighbors[constants.IPV4]:
142166
if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network(subnet):

0 commit comments

Comments
 (0)