Search in sources :

Example 6 with Route

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

the class VirtualRouterTest method testOSPFPassiveInterfaceRejection.

/**
 * Ensure no route propagation when the interfaces are disabled or passive
 */
@Test
public void testOSPFPassiveInterfaceRejection() {
    // Setup
    String testRouterName = "R1";
    String exportingRouterName = "R2";
    String exportingRouterInterfaceName = "Ethernet1";
    Map<String, Node> nodes = makeIosRouters(testRouterName, exportingRouterName);
    Map<String, VirtualRouter> routers = nodes.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue()._virtualRouters.get(Configuration.DEFAULT_VRF_NAME)));
    VirtualRouter testRouter = routers.get(testRouterName);
    VirtualRouter exportingRouter = routers.get(exportingRouterName);
    testRouter.initRibs();
    exportingRouter.initRibs();
    addInterfaces(testRouter._c, exampleInterfaceAddresses);
    addInterfaces(exportingRouter._c, ImmutableMap.of(exportingRouterInterfaceName, new InterfaceAddress("10.4.0.0/16")));
    int adminCost = RoutingProtocol.OSPF.getDefaultAdministrativeCost(testRouter._c.getConfigurationFormat());
    Prefix prefix = Prefix.parse("7.7.7.0/24");
    OspfIntraAreaRoute route = new OspfIntraAreaRoute(prefix, new Ip("7.7.1.1"), adminCost, 20, 1);
    exportingRouter._ospfIntraAreaRib.mergeRoute(route);
    // Set interaces on router 1 to be OSPF passive
    testRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(false));
    // Test 1
    testRouter.propagateOspfInternalRoutesFromNeighbor(testRouter._vrf.getOspfProcess(), nodes.get("R2"), testRouter._c.getInterfaces().firstEntry().getValue(), exportingRouter._c.getInterfaces().get(exportingRouterInterfaceName), adminCost);
    assertThat(testRouter._ospfInterAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfInterAreaRoute.class)));
    assertThat(testRouter._ospfIntraAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfIntraAreaRoute.class)));
    // Flip interfaces on router 2 to be passive now
    testRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(true));
    exportingRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(false));
    // Test 2
    testRouter.propagateOspfInternalRoutesFromNeighbor(testRouter._vrf.getOspfProcess(), nodes.get("R2"), testRouter._c.getInterfaces().firstEntry().getValue(), exportingRouter._c.getInterfaces().get(exportingRouterInterfaceName), adminCost);
    assertThat(testRouter._ospfInterAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfInterAreaRoute.class)));
    assertThat(testRouter._ospfIntraAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfIntraAreaRoute.class)));
}
Also used : OspfExternalType2Route(org.batfish.datamodel.OspfExternalType2Route) Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) BgpAdvertisementMatchers.hasDestinationIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasDestinationIp) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) BgpAdvertisementMatchers.hasOriginatorIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasOriginatorIp) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Interface(org.batfish.datamodel.Interface) Assert.assertThat(org.junit.Assert.assertThat) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) OspfRoute(org.batfish.datamodel.OspfRoute) Vrf(org.batfish.datamodel.Vrf) Statement(org.batfish.datamodel.routing_policy.statement.Statement) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) RipProcess(org.batfish.datamodel.RipProcess) BgpAdvertisementMatchers.hasSourceIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasSourceIp) Set(java.util.Set) RipRoute(org.batfish.datamodel.RipRoute) Collectors(java.util.stream.Collectors) OriginType(org.batfish.datamodel.OriginType) AsPath(org.batfish.datamodel.AsPath) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ConnectedRoute(org.batfish.datamodel.ConnectedRoute) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Entry(java.util.Map.Entry) Matchers.is(org.hamcrest.Matchers.is) OspfInternalRoute(org.batfish.datamodel.OspfInternalRoute) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) Ip(org.batfish.datamodel.Ip) BatfishTestUtils(org.batfish.main.BatfishTestUtils) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) Statements(org.batfish.datamodel.routing_policy.statement.Statements) CoreMatchers.not(org.hamcrest.CoreMatchers.not) BgpAdvertisementMatchers.hasNetwork(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasNetwork) BgpProcess(org.batfish.datamodel.BgpProcess) SetOrigin(org.batfish.datamodel.routing_policy.statement.SetOrigin) HashSet(java.util.HashSet) BgpAdvertisement(org.batfish.datamodel.BgpAdvertisement) LiteralOrigin(org.batfish.datamodel.routing_policy.expr.LiteralOrigin) ImmutableList(com.google.common.collect.ImmutableList) AbstractRoute(org.batfish.datamodel.AbstractRoute) Configuration(org.batfish.datamodel.Configuration) BgpRoute(org.batfish.datamodel.BgpRoute) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BgpAdvertisementType(org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType) NetworkFactory(org.batfish.datamodel.NetworkFactory) StaticRoute(org.batfish.datamodel.StaticRoute) Route(org.batfish.datamodel.Route) OspfExternalType1Route(org.batfish.datamodel.OspfExternalType1Route) Test(org.junit.Test) IsEmptyIterable.emptyIterableOf(org.hamcrest.collection.IsEmptyIterable.emptyIterableOf) RipInternalRoute(org.batfish.datamodel.RipInternalRoute) OspfIntraAreaRoute(org.batfish.datamodel.OspfIntraAreaRoute) OspfInterAreaRoute(org.batfish.datamodel.OspfInterAreaRoute) BgpAdvertisementMatchers.hasType(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasType) Prefix(org.batfish.datamodel.Prefix) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) OspfIntraAreaRoute(org.batfish.datamodel.OspfIntraAreaRoute) BgpAdvertisementMatchers.hasDestinationIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasDestinationIp) BgpAdvertisementMatchers.hasOriginatorIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasOriginatorIp) BgpAdvertisementMatchers.hasSourceIp(org.batfish.datamodel.matchers.BgpAdvertisementMatchers.hasSourceIp) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Test(org.junit.Test)

Example 7 with Route

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

the class BdpEngine method collectOscillatingPrefixes.

private SortedSet<Prefix> collectOscillatingPrefixes(Map<Integer, SortedSet<Route>> iterationRoutes, Map<Integer, SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>>> iterationAbsRoutes, int first, int last) {
    SortedSet<Prefix> oscillatingPrefixes = new TreeSet<>();
    if (_settings.getBdpDetail()) {
        for (int i = first + 1; i <= last; i++) {
            SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> baseRoutesByHostname = iterationAbsRoutes.get(i - 1);
            SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> deltaRoutesByHostname = iterationAbsRoutes.get(i);
            SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByHostname = new TreeMap<>();
            Set<String> hosts = new LinkedHashSet<>();
            hosts.addAll(baseRoutesByHostname.keySet());
            hosts.addAll(deltaRoutesByHostname.keySet());
            for (String hostname : hosts) {
                SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = new TreeMap<>();
                routesByHostname.put(hostname, routesByVrf);
                SortedMap<String, SortedSet<AbstractRoute>> baseRoutesByVrf = baseRoutesByHostname.get(hostname);
                SortedMap<String, SortedSet<AbstractRoute>> deltaRoutesByVrf = deltaRoutesByHostname.get(hostname);
                if (baseRoutesByVrf == null) {
                    for (Entry<String, SortedSet<AbstractRoute>> e : deltaRoutesByVrf.entrySet()) {
                        String vrfName = e.getKey();
                        SortedSet<AbstractRoute> deltaRoutes = e.getValue();
                        SortedSet<AbstractRoute> routes = new TreeSet<>();
                        routesByVrf.put(vrfName, routes);
                        for (AbstractRoute deltaRoute : deltaRoutes) {
                            oscillatingPrefixes.add(deltaRoute.getNetwork());
                        }
                    }
                } else if (deltaRoutesByVrf == null) {
                    for (Entry<String, SortedSet<AbstractRoute>> e : baseRoutesByVrf.entrySet()) {
                        String vrfName = e.getKey();
                        SortedSet<AbstractRoute> baseRoutes = e.getValue();
                        SortedSet<AbstractRoute> routes = new TreeSet<>();
                        routesByVrf.put(vrfName, routes);
                        for (AbstractRoute baseRoute : baseRoutes) {
                            oscillatingPrefixes.add(baseRoute.getNetwork());
                        }
                    }
                } else {
                    Set<String> vrfNames = new LinkedHashSet<>();
                    vrfNames.addAll(baseRoutesByVrf.keySet());
                    vrfNames.addAll(deltaRoutesByVrf.keySet());
                    for (String vrfName : vrfNames) {
                        SortedSet<AbstractRoute> baseRoutes = baseRoutesByVrf.get(vrfName);
                        SortedSet<AbstractRoute> deltaRoutes = deltaRoutesByVrf.get(vrfName);
                        if (baseRoutes == null) {
                            for (AbstractRoute deltaRoute : deltaRoutes) {
                                oscillatingPrefixes.add(deltaRoute.getNetwork());
                            }
                        } else if (deltaRoutes == null) {
                            for (AbstractRoute baseRoute : baseRoutes) {
                                oscillatingPrefixes.add(baseRoute.getNetwork());
                            }
                        } else {
                            SortedSet<AbstractRoute> prunedBaseRoutes = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet::new);
                            SortedSet<AbstractRoute> prunedDeltaRoutes = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet::new);
                            for (AbstractRoute baseRoute : prunedBaseRoutes) {
                                oscillatingPrefixes.add(baseRoute.getNetwork());
                            }
                            for (AbstractRoute deltaRoute : prunedDeltaRoutes) {
                                oscillatingPrefixes.add(deltaRoute.getNetwork());
                            }
                        }
                    }
                }
            }
        }
    } else {
        for (int i = first + 1; i <= last; i++) {
            SortedSet<Route> baseRoutes = iterationRoutes.get(i - 1);
            SortedSet<Route> deltaRoutes = iterationRoutes.get(i);
            SortedSet<Route> added = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet<Route>::new);
            SortedSet<Route> removed = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet<Route>::new);
            SortedSet<Route> changed = CommonUtil.union(added, removed, TreeSet<Route>::new);
            for (Route route : changed) {
                oscillatingPrefixes.add(route.getNetwork());
            }
        }
    }
    return oscillatingPrefixes;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) SortedMap(java.util.SortedMap) AbstractRoute(org.batfish.datamodel.AbstractRoute) Route(org.batfish.datamodel.Route)

Example 8 with Route

use of org.batfish.datamodel.Route 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 9 with Route

use of org.batfish.datamodel.Route 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

Route (org.batfish.datamodel.Route)9 AbstractRoute (org.batfish.datamodel.AbstractRoute)6 Ip (org.batfish.datamodel.Ip)6 Prefix (org.batfish.datamodel.Prefix)6 TreeSet (java.util.TreeSet)5 RouteBuilder (org.batfish.datamodel.RouteBuilder)5 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)5 SortedSet (java.util.SortedSet)4 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 SortedMap (java.util.SortedMap)3 Configuration (org.batfish.datamodel.Configuration)3 Interface (org.batfish.datamodel.Interface)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 BgpAdvertisement (org.batfish.datamodel.BgpAdvertisement)2