Exemple #1
0
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
    ]
Exemple #2
0
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
Exemple #3
0
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.unicastRoutes, key=lambda x: x.dest.prefixAddress.addr
    ):
        if not route.nextHops:
            continue

        min_metric = min(route.nextHops, key=lambda x: x.metric).metric
        nextHops = [nh for nh in route.nextHops if nh.metric == min_metric]
        shortest_routes.append(
            network_types.UnicastRoute(
                dest=route.dest,
                deprecatedNexthops=[nh.address for nh in nextHops],
                nextHops=nextHops,
            )
        )

    return shortest_routes
Exemple #4
0
def ip_to_unicast_route(
        ip_prefix: str, nexthops: List[network_types.NextHopThrift]
) -> network_types.UnicastRoute:
    """
    :param ip_prefix: IP prefix
    :param nexthops: List of next hops
    """

    return network_types.UnicastRoute(dest=ip_str_to_prefix(ip_prefix),
                                      nextHops=nexthops)