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(ip_types.UnicastRoute(dest=route.prefix, nexthops=nexthops)) return shortest_routes
def get_routes(self, route_db): ''' Find all shortest routes for each prefix in routeDb ''' shortest_routes = [] for route in sorted(route_db.routes): 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( ip_types.UnicastRoute(dest=route.prefix, nexthops=nexthops)) return shortest_routes
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 ip_types.UnicastRoute (structured routes) :rtype: list ''' prefixes = [utils.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 = utils.ip_str_to_addr(addr) nexthop.ifName = iface nhs.append(nexthop) return [ip_types.UnicastRoute(dest=p, nexthops=nhs) for p in prefixes]