Search in sources :

Example 1 with RouteContext

use of org.batfish.grammar.routing_table.ios.IosRoutingTableParser.RouteContext 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

List (java.util.List)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)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 Configuration (org.batfish.datamodel.Configuration)1 ConfigurationFormat (org.batfish.datamodel.ConfigurationFormat)1 Interface (org.batfish.datamodel.Interface)1 Ip (org.batfish.datamodel.Ip)1 Prefix (org.batfish.datamodel.Prefix)1 Route (org.batfish.datamodel.Route)1 RouteBuilder (org.batfish.datamodel.RouteBuilder)1 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)1