Search in sources :

Example 1 with RouteBuilder

use of org.batfish.datamodel.RouteBuilder in project batfish by batfish.

the class BdpEngine method computeOutputRoutes.

private SortedSet<Route> computeOutputRoutes(Map<String, Node> nodes, Map<Ip, String> ipOwners) {
    SortedSet<Route> outputRoutes = new TreeSet<>();
    nodes.forEach((hostname, node) -> {
        node._virtualRouters.forEach((vrName, vr) -> {
            for (AbstractRoute route : vr._mainRib.getRoutes()) {
                RouteBuilder rb = new RouteBuilder();
                rb.setNode(hostname);
                rb.setNetwork(route.getNetwork());
                Ip nextHopIp = route.getNextHopIp();
                if (route.getProtocol() == RoutingProtocol.CONNECTED || (route.getProtocol() == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(route.getNextHopInterface())) {
                    rb.setNextHop(Configuration.NODE_NONE_NAME);
                }
                if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
                    rb.setNextHopIp(nextHopIp);
                    String nextHop = ipOwners.get(nextHopIp);
                    if (nextHop != null) {
                        rb.setNextHop(nextHop);
                    }
                }
                rb.setNextHopInterface(route.getNextHopInterface());
                rb.setAdministrativeCost(route.getAdministrativeCost());
                rb.setCost(route.getMetric());
                rb.setProtocol(route.getProtocol());
                rb.setTag(route.getTag());
                rb.setVrf(vrName);
                Route outputRoute = rb.build();
                outputRoutes.add(outputRoute);
            }
        });
    });
    return outputRoutes;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) RouteBuilder(org.batfish.datamodel.RouteBuilder) TreeSet(java.util.TreeSet) Ip(org.batfish.datamodel.Ip) AbstractRoute(org.batfish.datamodel.AbstractRoute) Route(org.batfish.datamodel.Route)

Example 2 with RouteBuilder

use of org.batfish.datamodel.RouteBuilder in project batfish by batfish.

the class NxosRoutingTableExtractor method exitRoute.

@Override
public void exitRoute(RouteContext ctx) {
    if (ctx.protocol().LOCAL() != null) {
        return;
    }
    if (_currentVrfRoutes == null) {
        initVrf(Configuration.DEFAULT_VRF_NAME);
    }
    RoutingProtocol protocol = toProtocol(ctx.protocol());
    String nextHopInterface = Route.UNSET_NEXT_HOP_INTERFACE;
    if (ctx.nexthopint != null) {
        nextHopInterface = ctx.nexthopint.getText();
    }
    int admin = toInteger(ctx.admin);
    int cost = toInteger(ctx.cost);
    Ip nextHopIp = Route.UNSET_ROUTE_NEXT_HOP_IP;
    if (protocol != RoutingProtocol.CONNECTED && ctx.nexthop != null) {
        nextHopIp = new Ip(ctx.nexthop.getText());
    }
    RouteBuilder rb = new RouteBuilder();
    rb.setNode(_hostname);
    rb.setNetwork(_currentPrefix);
    if (protocol == RoutingProtocol.CONNECTED || (protocol == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(nextHopInterface)) {
        rb.setNextHop(Configuration.NODE_NONE_NAME);
    }
    if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
        rb.setNextHopIp(nextHopIp);
        String nextHop = _ipOwners.get(nextHopIp);
        if (nextHop != null) {
            rb.setNextHop(nextHop);
        }
    }
    if (nextHopInterface != null) {
        rb.setNextHopInterface(nextHopInterface);
    }
    rb.setAdministrativeCost(admin);
    rb.setCost(cost);
    rb.setProtocol(protocol);
    rb.setTag(Route.UNSET_ROUTE_TAG);
    rb.setVrf(_currentVrfName);
    Route route = rb.build();
    _currentVrfRoutes.add(route);
}
Also used : RoutingProtocol(org.batfish.datamodel.RoutingProtocol) RouteBuilder(org.batfish.datamodel.RouteBuilder) Ip(org.batfish.datamodel.Ip) Route(org.batfish.datamodel.Route)

Example 3 with RouteBuilder

use of org.batfish.datamodel.RouteBuilder in project batfish by batfish.

the class EosRoutingTableExtractor method exitRoute.

@Override
public void exitRoute(RouteContext ctx) {
    if (_currentVrfName == null) {
        _currentVrfName = Configuration.DEFAULT_VRF_NAME;
        initVrf(_currentVrfName);
    }
    RoutingProtocol protocol = toProtocol(ctx.protocol());
    Prefix prefix = Prefix.parse(ctx.IP_PREFIX().getText());
    int admin;
    int cost;
    Ip nextHopIp;
    for (int i = 0; i < ctx.nexthopifaces.size(); i++) {
        String nextHopInterface = ctx.nexthopifaces.get(i).getText();
        // TODO: support EOS route tags
        int tag = Route.UNSET_ROUTE_TAG;
        // TODO: support EOS next hop
        if (ctx.IS_DIRECTLY_CONNECTED() != null) {
            admin = 0;
            cost = 0;
            nextHopIp = Route.UNSET_ROUTE_NEXT_HOP_IP;
        } else {
            admin = toInteger(ctx.admin);
            cost = toInteger(ctx.cost);
            nextHopIp = new Ip(ctx.nexthops.get(i).getText());
        }
        RouteBuilder rb = new RouteBuilder();
        rb.setNode(_hostname);
        rb.setNetwork(prefix);
        if (protocol == RoutingProtocol.CONNECTED || (protocol == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(nextHopInterface)) {
            rb.setNextHop(Configuration.NODE_NONE_NAME);
        }
        if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
            rb.setNextHopIp(nextHopIp);
            String nextHop = _ipOwners.get(nextHopIp);
            if (nextHop != null) {
                rb.setNextHop(nextHop);
            }
        }
        if (nextHopInterface != null) {
            rb.setNextHopInterface(nextHopInterface);
        }
        rb.setAdministrativeCost(admin);
        rb.setCost(cost);
        rb.setProtocol(protocol);
        rb.setTag(tag);
        rb.setVrf(_currentVrfName);
        Route route = rb.build();
        _currentVrfRoutes.add(route);
    }
}
Also used : RoutingProtocol(org.batfish.datamodel.RoutingProtocol) RouteBuilder(org.batfish.datamodel.RouteBuilder) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Route(org.batfish.datamodel.Route)

Example 4 with RouteBuilder

use of org.batfish.datamodel.RouteBuilder in project batfish by batfish.

the class IosRoutingTableExtractor method exitRoute.

@Override
public void exitRoute(RouteContext ctx) {
    if (_currentVrfName == null) {
        _currentVrfName = Configuration.DEFAULT_VRF_NAME;
        initVrf(_currentVrfName);
    }
    RoutingProtocol protocol = toProtocol(ctx.protocol());
    if (protocol == RoutingProtocol.LOCAL) {
        return;
    }
    Prefix prefix = Prefix.parse(ctx.IP_PREFIX().getText());
    int admin;
    int cost;
    List<String> nextHopInterfaces = ctx.nexthopifaces.stream().map(nextHopIfaceCtx -> nextHopIfaceCtx.getText()).collect(Collectors.toList());
    List<Ip> nextHopIps = ctx.nexthops.stream().map(nextHopIpCtx -> new Ip(nextHopIpCtx.getText())).collect(Collectors.toList());
    int numIterations = Math.max(nextHopIps.size(), nextHopInterfaces.size());
    for (int i = 0; i < numIterations; i++) {
        String nextHopInterface;
        Ip nextHopIp;
        if (nextHopInterfaces.isEmpty()) {
            nextHopInterface = Route.UNSET_NEXT_HOP_INTERFACE;
        } else {
            nextHopInterface = nextHopInterfaces.get(i);
        }
        if (ctx.IS_DIRECTLY_CONNECTED() != null) {
            if (protocol == RoutingProtocol.STATIC) {
                // static
                if (ctx.admin != null) {
                    admin = toInteger(ctx.admin);
                } else {
                    admin = 1;
                }
                if (ctx.cost != null) {
                    cost = toInteger(ctx.cost);
                } else {
                    cost = 0;
                }
            } else {
                // connected
                admin = 0;
                cost = 0;
            }
            nextHopIp = Route.UNSET_ROUTE_NEXT_HOP_IP;
        } else {
            admin = toInteger(ctx.admin);
            cost = toInteger(ctx.cost);
            nextHopIp = nextHopIps.get(i);
        }
        // TODO: support IOS route tags
        int tag = Route.UNSET_ROUTE_TAG;
        // TODO: support IOS next hop
        if (protocol == RoutingProtocol.BGP && admin > RoutingProtocol.BGP.getDefaultAdministrativeCost(ConfigurationFormat.CISCO_IOS)) {
            protocol = RoutingProtocol.IBGP;
        }
        RouteBuilder rb = new RouteBuilder();
        rb.setNode(_hostname);
        rb.setNetwork(prefix);
        if (protocol == RoutingProtocol.CONNECTED || (protocol == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(nextHopInterface)) {
            rb.setNextHop(Configuration.NODE_NONE_NAME);
        }
        if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
            rb.setNextHopIp(nextHopIp);
            String nextHop = _ipOwners.get(nextHopIp);
            if (nextHop != null) {
                rb.setNextHop(nextHop);
            }
        }
        if (nextHopInterface != null) {
            rb.setNextHopInterface(nextHopInterface);
        }
        rb.setAdministrativeCost(admin);
        rb.setCost(cost);
        rb.setProtocol(protocol);
        rb.setTag(tag);
        rb.setVrf(_currentVrfName);
        Route route = rb.build();
        _currentVrfRoutes.add(route);
    }
}
Also used : SortedSet(java.util.SortedSet) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) CommonUtil(org.batfish.common.util.CommonUtil) Token(org.antlr.v4.runtime.Token) BatfishException(org.batfish.common.BatfishException) ProtocolContext(org.batfish.grammar.routing_table.ios.IosRoutingTableParser.ProtocolContext) TreeSet(java.util.TreeSet) Interface(org.batfish.datamodel.Interface) RouteContext(org.batfish.grammar.routing_table.ios.IosRoutingTableParser.RouteContext) Map(java.util.Map) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Configuration(org.batfish.datamodel.Configuration) Vrf_declarationContext(org.batfish.grammar.routing_table.ios.IosRoutingTableParser.Vrf_declarationContext) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Route(org.batfish.datamodel.Route) Ios_routing_tableContext(org.batfish.grammar.routing_table.ios.IosRoutingTableParser.Ios_routing_tableContext) Collectors(java.util.stream.Collectors) IBatfish(org.batfish.common.plugin.IBatfish) List(java.util.List) Warnings(org.batfish.common.Warnings) RoutingTableExtractor(org.batfish.grammar.RoutingTableExtractor) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) RouteBuilder(org.batfish.datamodel.RouteBuilder) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) RouteBuilder(org.batfish.datamodel.RouteBuilder) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Route(org.batfish.datamodel.Route)

Aggregations

Ip (org.batfish.datamodel.Ip)4 Route (org.batfish.datamodel.Route)4 RouteBuilder (org.batfish.datamodel.RouteBuilder)4 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)3 TreeSet (java.util.TreeSet)2 Prefix (org.batfish.datamodel.Prefix)2 List (java.util.List)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 Collectors (java.util.stream.Collectors)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 Token (org.antlr.v4.runtime.Token)1 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)1 BatfishException (org.batfish.common.BatfishException)1 Warnings (org.batfish.common.Warnings)1 IBatfish (org.batfish.common.plugin.IBatfish)1 CommonUtil (org.batfish.common.util.CommonUtil)1 AbstractRoute (org.batfish.datamodel.AbstractRoute)1 Configuration (org.batfish.datamodel.Configuration)1 ConfigurationFormat (org.batfish.datamodel.ConfigurationFormat)1