Skip to content

Commit

Permalink
Merge pull request #16 from srl-labs/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
wdesmedt authored Jan 16, 2024
2 parents d5852f5 + f9fe573 commit 6b0eb8c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
34 changes: 21 additions & 13 deletions nornir_srl/connections/srlinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ def augment_routes(d, attribs): # augment routes with attributes
"RIB_IP_JMESPATH": '"network-instance"[].{NI:name, Rib:"bgp-rib"."afi-safi"[]."'
+ ROUTE_FAMILY[route_fam]
+ '"."local-rib"."routes"[]'
+ '.{neighbor:neighbor, "0_st":"_r_state", "Pfx":prefix, "lpref":"local-pref", med:med, "next-hop":"next-hop","as-path":"as-path".segment[0].member, "communities":communities.community}}',
+ '.{neighbor:neighbor, "0_st":"_r_state", "Pfx":prefix, "lpref":"local-pref", med:med, "next-hop":"next-hop","as-path":"as-path".segment[0].member,\
"communities":[communities.community, communities."large-community"][]|join(\',\',@)}}',
},
3: {
"RIB_IP_PATH": (
Expand All @@ -277,7 +278,8 @@ def augment_routes(d, attribs): # augment routes with attributes
"RIB_IP_JMESPATH": '"network-instance"[].{NI:name, Rib:"bgp-rib"."afi-safi"[]."'
+ ROUTE_FAMILY[route_fam]
+ '"."local-rib"."route"[]'
+ '.{neighbor:neighbor, "0_st":"_r_state", "Pfx":prefix, "lpref":"local-pref", med:med, "next-hop":"next-hop","as-path":"as-path".segment[0].member, "communities":communities.community}}',
+ '.{neighbor:neighbor, "0_st":"_r_state", "Pfx":prefix, "lpref":"local-pref", med:med, "next-hop":"next-hop","as-path":"as-path".segment[0].member,\
"communities":[communities.community, communities."large-community"][]|join(\',\',@)}}',
},
}

Expand Down Expand Up @@ -452,7 +454,7 @@ def get_rib_ipv4(
path_spec = {
"path": f"/network-instance[name={network_instance}]/route-table/ipv4-unicast",
"jmespath": '"network-instance"[?_hasrib].{NI:name, Rib:"route-table"."ipv4-unicast".route[].{"Prefix":"ipv4-prefix",\
"next-hop":"_next-hop",type:"route-type", Act:active, metric:metric, pref:preference, itf:"_nh_itf"}}',
"next-hop":"_next-hop",type:"route-type", Act:active, "orig-vrf":"_orig_vrf",metric:metric, pref:preference, itf:"_nh_itf"}}',
"datatype": "state",
}

Expand Down Expand Up @@ -504,7 +506,7 @@ def get_rib_ipv4(
# tmp_map[nhgroup["index"]] = [ nh["next-hop"] for nh in nhgroup["next-hop"] ]
nh_map[nhgroup["index"]] = [
nh_mapping[network_instance][nh.get("next-hop")]
for nh in nhgroup["next-hop"]
for nh in nhgroup.get("next-hop", [])
]
nhgroup_mapping.update({ni["name"]: nh_map})

Expand Down Expand Up @@ -540,32 +542,38 @@ def get_rib_ipv4(
else:
route["active"] = "No"
if "next-hop-group" in route:
leaked = False
if "origin-network-instance" in route:
nh_ni = route["origin-network-instance"]
if nh_ni != ni["name"]:
leaked = True
route["_orig_vrf"] = nh_ni
else:
nh_ni = ni["name"]
route["_next-hop"] = [
nh.get("ip-address")
for nh in nhgroup_mapping[ni["name"]][
route["next-hop-group"]
]
for nh in nhgroup_mapping[nh_ni][route["next-hop-group"]]
]

route["_nh_itf"] = [
nh.get("subinterface")
for nh in nhgroup_mapping[ni["name"]][
route["next-hop-group"]
]
nh.get("subinterface") + f"@vrf:{nh_ni}"
if leaked
else nh.get("subinterface")
for nh in nhgroup_mapping[nh_ni][route["next-hop-group"]]
if nh.get("subinterface")
]
if len(route["_nh_itf"]) == 0:
route["_nh_itf"] = [
nh.get("tunnel")
for nh in nhgroup_mapping[ni["name"]][
for nh in nhgroup_mapping[nh_ni][
route["next-hop-group"]
]
if nh.get("tunnel")
]
if len(route["_nh_itf"]) == 0:
resolving_routes = [
nh.get("resolving-route", {})
for nh in nhgroup_mapping[ni["name"]][
for nh in nhgroup_mapping[nh_ni][
route["next-hop-group"]
]
if nh.get("resolving-route")
Expand Down
36 changes: 27 additions & 9 deletions nornir_srl/fsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ def get_fields(b, depth=0):
def pass_filter(row, filter):
if filter == None:
return True
filter = {str(k).lower(): v for k, v in filter.items()}
if len(
{
k: v
for k, v in row.items()
if filter.get(k) and fnmatch.fnmatch(str(row[k]), str(filter[k]))
if filter.get(str(k).lower())
and fnmatch.fnmatch(str(row[k]), str(filter[str(k).lower()]))
}
) < len(filter):
return False
Expand Down Expand Up @@ -242,6 +244,14 @@ def pass_filter(row, filter):
multiple=True,
help="inventory filter, e.g. -i site=lab -i role=leaf. Possible filter-fields are defined in inventory. Multiple filters are ANDed",
)
# @click.option(
# "--format",
# "-f",
# multiple=False,
# type=click.Choice(["table", "json", "yaml"]),
# default="table",
# help="Output format",
# )
@click.option(
"--box-type",
"-b",
Expand All @@ -266,6 +276,7 @@ def pass_filter(row, filter):
def cli(
ctx: Context,
cfg: str,
format: Optional[str] = None,
inv_filter: Optional[List] = None,
# field_filter: Optional[List] = None,
box_type: Optional[str] = None,
Expand Down Expand Up @@ -364,6 +375,7 @@ def cli(
if box_type:
box_type = box_type.upper()
ctx.obj["box_type"] = box_type
ctx.obj["format"] = format


def print_report(
Expand Down Expand Up @@ -411,14 +423,20 @@ def _bgp_peers(task: Task) -> Result:
result = ctx.obj["target"].run(
task=_bgp_peers, name="bgp_peers", raise_on_error=False
)
print_report(
result=result,
name="BGP Peers",
failed_hosts=result.failed_hosts,
box_type=ctx.obj["box_type"],
f_filter=f_filter,
i_filter=ctx.obj["i_filter"],
)
if ctx.obj["format"] == "json":
print_result(result)
elif ctx.obj["format"] == "yaml":
yaml = YAML(typ="safe")
yaml.dump(result, sys.stdout)
else:
print_report(
result=result,
name="BGP Peers",
failed_hosts=result.failed_hosts,
box_type=ctx.obj["box_type"],
f_filter=f_filter,
i_filter=ctx.obj["i_filter"],
)


@cli.command()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nornir-srl"
version = "0.2.5"
version = "0.2.6"
description = "Nornir connection plugin for SRLinux"
authors = ["Walter De Smedt <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 6b0eb8c

Please sign in to comment.