Skip to content

Commit ee43d68

Browse files
authored
Merge pull request #19 from srl-labs/improvements-feb24
Improvements feb24
2 parents 211585b + 262e82d commit ee43d68

File tree

3 files changed

+171
-136
lines changed

3 files changed

+171
-136
lines changed

nornir_srl/connections/srlinux.py

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ def augment_routes(d, attribs): # augment routes with attributes
200200
)
201201
if d.get("vni", 0) == 0:
202202
d["vni"] = "-"
203+
d["_rt"] = ",".join(
204+
[
205+
x_comm.split("target:")[1]
206+
for x_comm in d.get("communities", {}).get(
207+
"ext-community", []
208+
)
209+
if "target:" in x_comm
210+
]
211+
)
212+
d["_esi_lbl"] = ",".join(
213+
[
214+
str(x_comm.split("esi-label:")[1])
215+
.replace("Single-Active", "S-A")
216+
.replace("All-Active", "A-A")
217+
for x_comm in d.get("communities", {}).get(
218+
"ext-community", []
219+
)
220+
if "esi-label:" in x_comm
221+
]
222+
)
203223
return d
204224
else:
205225
return {k: augment_routes(v, attribs) for k, v in d.items()}
@@ -240,11 +260,11 @@ def augment_routes(d, attribs): # augment routes with attributes
240260
+ ROUTE_TYPE[route_type] # type: ignore
241261
+ '"[]',
242262
"RIB_EVPN_JMESPATH_ATTRS": {
243-
"1": '.{RD:"route-distinguisher", peer:neighbor, ESI:esi, Tag:"ethernet-tag-id",vni:vni, "next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
244-
"2": '.{RD:"route-distinguisher", peer:neighbor, ESI:esi, "MAC":"mac-address", "IP":"ip-address",vni:vni,"next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
245-
"3": '.{RD:"route-distinguisher", peer:neighbor, Tag:"ethernet-tag-id", "next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
246-
"4": '.{RD:"route-distinguisher", peer:neighbor, ESI:esi, "next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
247-
"5": '.{RD:"route-distinguisher", peer:neighbor, lpref:"local-pref", "IP-Pfx":"ip-prefix",vni:vni, med:med, "next-hop":"next-hop", GW:"gateway-ip",origin:origin, "0_st":"_r_state"}}',
263+
"1": '.{RD:"route-distinguisher", peer:neighbor, ESI:esi, Tag:"ethernet-tag-id",vni:vni, "NextHop":"next-hop", RT:"_rt", "esi-lbl":"_esi_lbl", "0_st":"_r_state"}}',
264+
"2": '.{RD:"route-distinguisher", RT:"_rt", peer:neighbor, ESI:esi, "MAC":"mac-address", "IP":"ip-address",vni:vni,"next-hop":"next-hop", "0_st":"_r_state"}}',
265+
"3": '.{RD:"route-distinguisher", RT:"_rt", peer:neighbor, Tag:"ethernet-tag-id", "next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
266+
"4": '.{RD:"route-distinguisher", RT:"_rt", peer:neighbor, ESI:esi, "next-hop":"next-hop", origin:origin, "0_st":"_r_state"}}',
267+
"5": '.{RD:"route-distinguisher", RT:"_rt", peer:neighbor, lpref:"local-pref", "IP-Pfx":"ip-prefix",vni:vni, med:med, "next-hop":"next-hop", GW:"gateway-ip",origin:origin, "0_st":"_r_state"}}',
248268
},
249269
},
250270
}
@@ -268,7 +288,7 @@ def augment_routes(d, attribs): # augment routes with attributes
268288
+ ROUTE_FAMILY[route_fam]
269289
+ '"."local-rib"."routes"[]'
270290
+ '.{neighbor:neighbor, "0_st":"_r_state", "Pfx":prefix, "lpref":"local-pref", med:med, "next-hop":"next-hop","as-path":"as-path".segment[0].member,\
271-
"communities":[communities.community, communities."large-community"][]|join(\',\',@)}}',
291+
"communities":[communities.community, communities."large-community"][]|join(\', \',@)}}',
272292
},
273293
3: {
274294
"RIB_IP_PATH": (
@@ -538,9 +558,9 @@ def get_rib_ipv4(
538558
continue
539559
for route in ni["route-table"]["ipv4-unicast"]["route"]:
540560
if route["active"]:
541-
route["active"] = "Yes"
561+
route["active"] = "yes"
542562
else:
543-
route["active"] = "No"
563+
route["active"] = "no"
544564
if "next-hop-group" in route:
545565
leaked = False
546566
if "origin-network-instance" in route:
@@ -552,30 +572,34 @@ def get_rib_ipv4(
552572
nh_ni = ni["name"]
553573
route["_next-hop"] = [
554574
nh.get("ip-address")
555-
for nh in nhgroup_mapping[nh_ni][route["next-hop-group"]]
575+
for nh in nhgroup_mapping[nh_ni].get(
576+
route["next-hop-group"], {}
577+
)
556578
]
557579

558580
route["_nh_itf"] = [
559581
nh.get("subinterface") + f"@vrf:{nh_ni}"
560582
if leaked
561583
else nh.get("subinterface")
562-
for nh in nhgroup_mapping[nh_ni][route["next-hop-group"]]
584+
for nh in nhgroup_mapping[nh_ni].get(
585+
route["next-hop-group"], {}
586+
)
563587
if nh.get("subinterface")
564588
]
565589
if len(route["_nh_itf"]) == 0:
566590
route["_nh_itf"] = [
567591
nh.get("tunnel")
568-
for nh in nhgroup_mapping[nh_ni][
569-
route["next-hop-group"]
570-
]
592+
for nh in nhgroup_mapping[nh_ni].get(
593+
route["next-hop-group"], {}
594+
)
571595
if nh.get("tunnel")
572596
]
573597
if len(route["_nh_itf"]) == 0:
574598
resolving_routes = [
575599
nh.get("resolving-route", {})
576-
for nh in nhgroup_mapping[nh_ni][
577-
route["next-hop-group"]
578-
]
600+
for nh in nhgroup_mapping[nh_ni].get(
601+
route["next-hop-group"], {}
602+
)
579603
if nh.get("resolving-route")
580604
]
581605
# if len(resolving_routes) > 0:
@@ -589,7 +613,7 @@ def get_nwi_itf(self, nw_instance: str = "*") -> Dict[str, Any]:
589613
path_spec = {
590614
"path": f"/network-instance[name={nw_instance}]",
591615
"jmespath": '"network-instance"[].{NI:name,oper:"oper-state",type:type,"router-id":protocols.bgp."router-id",\
592-
itfs: interface[].{Subitf:name,"if-oper":"oper-state", ipv4:ipv4.address[]."ip-prefix",\
616+
itfs: interface[].{Subitf:name,"assoc-ni":"_other_ni","if-oper":"oper-state", ipv4:ipv4.address[]."ip-prefix",\
593617
vlan:vlan.encap."single-tagged"."vlan-id", "mtu":"_mtu"}}',
594618
"datatype": "state",
595619
}
@@ -609,21 +633,31 @@ def get_nwi_itf(self, nw_instance: str = "*") -> Dict[str, Any]:
609633
for ni in resp[0].get("network-instance", {}):
610634
for ni_itf in ni.get("interface", []):
611635
ni_itf.update(subitf.get(ni_itf["name"], {}))
636+
if ni_itf["name"].startswith("irb"):
637+
ni_itf["_other_ni"] = " ".join(
638+
f"{vrf['name']}"
639+
for vrf in resp[0].get("network-instance", {})
640+
if ni_itf["name"] in [i["name"] for i in vrf["interface"]]
641+
and vrf["name"] != ni["name"]
642+
)
612643

613644
res = jmespath.search(path_spec["jmespath"], resp[0])
614645
return {"nwi_itfs": res}
615646

616647
def get_lag(self, lag_id: str = "*") -> Dict[str, Any]:
617648
path_spec = {
618649
"path": f"/interface[name=lag{lag_id}]",
619-
"jmespath": '"interface"[].{lag:name, oper:"oper-state",mtu:mtu,num:lag.member|length(@),"min":lag."min-links",desc:description, type:lag."lag-type", speed:lag."lag-speed","stby-sig":ethernet."standby-signaling",\
650+
"jmespath": '"interface"[].{lag:name, oper:"oper-state",mtu:mtu,"min":lag."min-links",desc:description, type:lag."lag-type", speed:lag."lag-speed","stby-sig":ethernet."standby-signaling",\
620651
"lacp-key":lag.lacp."admin-key","lacp-itvl":lag.lacp.interval,"lacp-mode":lag.lacp."lacp-mode","lacp-sysid":lag.lacp."system-id-mac","lacp-prio":lag.lacp."system-priority",\
621652
members:lag.member[].{"member-itf":name, "member-oper":"oper-state","act":lacp."activity"}}',
622653
"datatype": "state",
623654
}
624655
resp = self.get(
625656
paths=[path_spec.get("path", "")], datatype=path_spec["datatype"]
626657
)
658+
for itf in resp[0].get("interface", []):
659+
for member in itf.get("lag", {}).get("member", []):
660+
member["name"] = str(member.get("name", "")).replace("ethernet", "et")
627661
res = jmespath.search(path_spec["jmespath"], resp[0])
628662
return {"lag": res}
629663

0 commit comments

Comments
 (0)