def print_routes_table(route_db, prefixes=None): """ print the the routes from Decision/Fib module """ networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] route_strs = [] for route in sorted(route_db.routes, key=lambda x: x.prefix.prefixAddress.addr): prefix_str = ipnetwork.sprint_prefix(route.prefix) if not ipnetwork.contain_any_prefix(prefix_str, networks): continue paths_str = "\n".join([ "via {}%{} metric {}".format( ipnetwork.sprint_addr(path.nextHop.addr), path.ifName, path.metric) for path in route.paths ]) route_strs.append((prefix_str, paths_str)) caption = "Routes for {}".format(route_db.thisNodeName) if not route_strs: route_strs.append(["No routes found."]) print(printing.render_vertical_table(route_strs, caption=caption))
def print_routes_json( route_db_dict, prefixes: List[str] = None, labels: List[int] = None ): """ Print json representation of routes. Takes prefixes and labels to filter """ networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] # Filter out all routes based on prefixes and labels for routes in route_db_dict.values(): filtered_unicast_routes = [] for route in routes["unicastRoutes"]: if labels or networks: if networks and ipnetwork.contain_any_prefix(route["dest"], networks): filtered_unicast_routes.append(route) else: filtered_unicast_routes.append(route) routes["unicastRoutes"] = filtered_unicast_routes filtered_mpls_routes = [] for route in routes["mplsRoutes"]: if labels or prefixes: if labels and int(route["topLabel"]) in labels: filtered_mpls_routes.append(route) else: filtered_mpls_routes.append(route) routes["mplsRoutes"] = filtered_mpls_routes # Filter print(json_dumps(route_db_dict))
def print_unicast_routes( caption: str, unicast_routes: List[network_types.UnicastRoute], prefixes: List[str] = None, ): """ Print unicast routes. Subset specified by prefixes will be printed if specified """ networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] route_strs = [] for route in unicast_routes: dest = ipnetwork.sprint_prefix(route.dest) if not ipnetwork.contain_any_prefix(dest, networks): continue paths_str = "\n".join( ["via {}".format(ip_nexthop_to_str(nh)) for nh in get_route_nexthops(route)] ) route_strs.append([dest, paths_str]) print(printing.render_vertical_table(route_strs, caption=caption))
def build_unicast_route( route: object, filter_for_networks: Optional[List[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]] = None, ) -> Tuple[str, List[str]]: dest = ipnetwork.sprint_prefix(route.dest) if filter_for_networks and not ipnetwork.contain_any_prefix( dest, filter_for_networks): return None nexthops = [ip_nexthop_to_str(nh) for nh in get_route_nexthops(route)] return dest, nexthops
def print_routes_json(route_db_dict, prefixes=None): networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] # Filter out all routes based on prefixes! for routes in route_db_dict.values(): filtered_routes = [] for route in routes["routes"]: if not ipnetwork.contain_any_prefix(route["prefix"], networks): continue filtered_routes.append(route) routes["routes"] = filtered_routes print(json_dumps(route_db_dict))
def print_routes(caption, routes, prefixes=None): networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] route_strs = [] for route in routes: dest = ipnetwork.sprint_prefix(route.dest) if not ipnetwork.contain_any_prefix(dest, networks): continue paths_str = '\n'.join(["via {}".format(ip_nexthop_to_str(nh)) for nh in route.nexthops]) route_strs.append((dest, paths_str)) print(printing.render_vertical_table(route_strs, caption=caption))
def get_routes_json(host, client, routes, prefixes=None): networks = None if prefixes: networks = [ipaddress.ip_network(p) for p in prefixes] data = {"host": host, "client": client, "routes": []} for route in routes: dest = ipnetwork.sprint_prefix(route.dest) if not ipnetwork.contain_any_prefix(dest, networks): continue route_data = { "dest": dest, "nexthops": [ip_nexthop_to_str(nh) for nh in route.nexthops], } data["routes"].append(route_data) return data