def build_routes(prefixes, nexthops): """ :param prefixes: List of prefixes in string representation :param nexthops: List of nexthops ip addresses in string presentation :returns: list network_types.UnicastRoute (structured routes) :rtype: list """ prefixes = [ipnetwork.ip_str_to_prefix(p) for p in prefixes] nhs = [] for nh_iface in nexthops: iface, addr = None, None # Nexthop may or may not be link-local. Handle it here well if "@" in nh_iface: addr, iface = nh_iface.split("@") elif "%" in nh_iface: addr, iface = nh_iface.split("%") else: addr = nh_iface nexthop = ipnetwork.ip_str_to_addr(addr) nexthop.ifName = iface nhs.append(nexthop) return [ network_types.UnicastRoute( dest=p, deprecatedNexthops=nhs, nextHops=[network_types.NextHopThrift(address=nh) for nh in nhs], ) for p in prefixes ]
def mpls_nexthop_to_nexthop_thrift( ip_addr: str, if_index: str, weight: int = 0, metric: int = 0, label: Optional[List[int]] = None, action: network_types.MplsActionCode = network_types.MplsActionCode.PHP, ) -> network_types.NextHopThrift: """ :param label: label(s) for PUSH, SWAP action :param action: label action PUSH, POP, SWAP :param ip_addr: Next hop IP address :param if_index: Next hop interface index :param weight: Next hop weigth :param metric: Cost associated with next hop """ binary_address = ip_str_to_addr(ip_addr, if_index) nexthop = network_types.NextHopThrift(address=binary_address, weight=weight, metric=metric) mpls_action = network_types.MplsAction(action=action) if action == network_types.MplsActionCode.SWAP: mpls_action.swapLabel = label[0] elif action == network_types.MplsActionCode.PUSH: mpls_action.pushLabels = label[:] nexthop.mplsAction = mpls_action return nexthop
def get_shortest_routes(route_db): """ Find all shortest routes for each prefix in routeDb :param route_db: RouteDatabase :return list of UnicastRoute of prefix & corresponding shortest nexthops """ shortest_routes = [] for route in sorted(route_db.routes, key=lambda x: x.prefix.prefixAddress.addr): if not route.paths: continue min_metric = min(route.paths, key=lambda x: x.metric).metric nexthops = [] for path in route.paths: if path.metric == min_metric: nexthops.append(path.nextHop) nexthops[-1].ifName = path.ifName shortest_routes.append( network_types.UnicastRoute( dest=route.prefix, deprecatedNexthops=nexthops, nextHops=[ network_types.NextHopThrift(address=nh) for nh in nexthops ], )) return shortest_routes
def get_route_nexthops( route: network_types.UnicastRoute ) -> List[network_types.NextHopThrift]: """ DEPRECATED: this function is meant to keep backward functionality with old vs new way of expressing route nexthops """ if route.nextHops: # Checks for both null and empty list return route.nextHops return [network_types.NextHopThrift(address=nh) for nh in route.deprecatedNexthops]
def ip_nexthop_to_nexthop_thrift( ip_addr: str, if_index: str, weight: int = 0, metric: int = 0 ) -> network_types.NextHopThrift: """ :param ip_addr: Next hop IP address :param if_index: Next hop interface index :param weight: Next hop weigth :param metric: Cost associated with next hop """ binary_address = ip_str_to_addr(ip_addr, if_index) return network_types.NextHopThrift( address=binary_address, weight=weight, metric=metric )