Search in sources :

Example 21 with AbstractRoute

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

the class BdpEngine method getRoutes.

SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> getRoutes(BdpDataPlane dp) {
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByHostname = new TreeMap<>();
    for (Entry<String, Node> e : dp._nodes.entrySet()) {
        String hostname = e.getKey();
        Node node = e.getValue();
        for (Entry<String, VirtualRouter> e2 : node._virtualRouters.entrySet()) {
            String vrfName = e2.getKey();
            VirtualRouter vrf = e2.getValue();
            for (AbstractRoute route : vrf._mainRib.getRoutes()) {
                SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = routesByHostname.computeIfAbsent(hostname, k -> new TreeMap<>());
                SortedSet<AbstractRoute> routes = routesByVrf.computeIfAbsent(vrfName, k -> new TreeSet<>());
                routes.add(route);
            }
        }
    }
    return routesByHostname;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) SortedMap(java.util.SortedMap)

Example 22 with AbstractRoute

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

the class BdpDataPlanePluginTest method testIbgpRejectOwnAs.

@Test
public void testIbgpRejectOwnAs() throws IOException {
    String testrigName = "ibgp-reject-own-as";
    List<String> configurationNames = ImmutableList.of("r1", "r2a", "r2b");
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(TESTRIGS_PREFIX + testrigName, configurationNames).build(), _folder);
    BdpDataPlanePlugin dataPlanePlugin = new BdpDataPlanePlugin();
    dataPlanePlugin.initialize(batfish);
    batfish.computeDataPlane(false);
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routes = dataPlanePlugin.getRoutes(batfish.loadDataPlane());
    SortedSet<AbstractRoute> r2aRoutes = routes.get("r2a").get(DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r2bRoutes = routes.get("r2b").get(DEFAULT_VRF_NAME);
    Set<Prefix> r2aPrefixes = r2aRoutes.stream().map(AbstractRoute::getNetwork).collect(Collectors.toSet());
    Set<Prefix> r2bPrefixes = r2bRoutes.stream().map(AbstractRoute::getNetwork).collect(Collectors.toSet());
    Prefix r1Loopback0Prefix = Prefix.parse("1.0.0.1/32");
    Prefix r1Loopback1Prefix = Prefix.parse("1.0.0.2/32");
    assertTrue(r2aPrefixes.contains(r1Loopback0Prefix));
    assertTrue(r2aPrefixes.contains(r1Loopback1Prefix));
    /*
     * 1.0.0.2/32 should be accepted r2b as a normal iBGP route forwarded from r1.
     */
    assertTrue(r2bPrefixes.contains(r1Loopback1Prefix));
    /*
     * 1.0.0.1/32 should be rejected by r2b since it already contains AS#2 in its AS-path due to
     * r2a prepending 2 in the matching route-map clause.
     */
    assertFalse(r2bPrefixes.contains(r1Loopback0Prefix));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Prefix(org.batfish.datamodel.Prefix) Batfish(org.batfish.main.Batfish) Test(org.junit.Test)

Example 23 with AbstractRoute

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

the class OspfTest method assertRoute.

private static void assertRoute(SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByNode, RoutingProtocol protocol, String hostname, InterfaceAddress address, long expectedCost) {
    Prefix prefix = address.getPrefix();
    assertThat(routesByNode, hasKey(hostname));
    SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = routesByNode.get(hostname);
    assertThat(routesByVrf, hasKey(Configuration.DEFAULT_VRF_NAME));
    SortedSet<AbstractRoute> routes = routesByVrf.get(Configuration.DEFAULT_VRF_NAME);
    assertThat(routes, hasItem(hasPrefix(prefix)));
    AbstractRoute route = routes.stream().filter(r -> r.getNetwork().equals(prefix)).findAny().get();
    assertThat(route, hasMetric(expectedCost));
    assertThat(route, hasProtocol(protocol));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) AbstractRouteMatchers.hasPrefix(org.batfish.datamodel.matchers.AbstractRouteMatchers.hasPrefix) Prefix(org.batfish.datamodel.Prefix) SortedSet(java.util.SortedSet)

Example 24 with AbstractRoute

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

the class RipAndBgpTest method testOutputRoutes.

@Test
public void testOutputRoutes() throws IOException {
    String testrigResourcePrefix = TESTRIGS_PREFIX + "rip";
    Set<String> configurations = ImmutableSet.of("r1", "r2", "r3");
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(testrigResourcePrefix, configurations).build(), _folder);
    BdpDataPlanePlugin dataPlanePlugin = new BdpDataPlanePlugin();
    dataPlanePlugin.initialize(batfish);
    batfish.computeDataPlane(false);
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routes = dataPlanePlugin.getRoutes(batfish.loadDataPlane());
    SortedSet<AbstractRoute> r1Routes = routes.get("r1").get(Configuration.DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r2Routes = routes.get("r2").get(Configuration.DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r3Routes = routes.get("r3").get(Configuration.DEFAULT_VRF_NAME);
    Set<Prefix> r1Prefixes = r1Routes.stream().map(AbstractRoute::getNetwork).collect(Collectors.toSet());
    Set<Prefix> r2Prefixes = r2Routes.stream().map(AbstractRoute::getNetwork).collect(Collectors.toSet());
    Set<Prefix> r3Prefixes = r3Routes.stream().map(AbstractRoute::getNetwork).collect(Collectors.toSet());
    Prefix prefix1 = Prefix.parse("1.0.0.0/8");
    Prefix prefix2 = Prefix.parse("2.0.0.0/8");
    Prefix prefix3 = Prefix.parse("3.0.0.0/8");
    assertThat(r1Prefixes, containsInAnyOrder(prefix1, prefix2, prefix3));
    assertThat(r2Prefixes, containsInAnyOrder(prefix1, prefix2, prefix3));
    assertThat(r3Prefixes, containsInAnyOrder(prefix1, prefix3));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) SortedMap(java.util.SortedMap) Prefix(org.batfish.datamodel.Prefix) Batfish(org.batfish.main.Batfish) Test(org.junit.Test)

Example 25 with AbstractRoute

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

the class RouteReflectionTest method assertIbgpRoute.

private static void assertIbgpRoute(SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByNode, String hostname, Prefix prefix) {
    assertThat(routesByNode, hasKey(hostname));
    SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = routesByNode.get(hostname);
    assertThat(routesByVrf, hasKey(Configuration.DEFAULT_VRF_NAME));
    SortedSet<AbstractRoute> routes = routesByVrf.get(Configuration.DEFAULT_VRF_NAME);
    assertThat(routes, hasItem(hasPrefix(prefix)));
    AbstractRoute route = routes.stream().filter(r -> r.getNetwork().equals(prefix)).findAny().get();
    assertThat(route, hasProtocol(RoutingProtocol.IBGP));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet)

Aggregations

AbstractRoute (org.batfish.datamodel.AbstractRoute)27 Prefix (org.batfish.datamodel.Prefix)18 SortedMap (java.util.SortedMap)16 SortedSet (java.util.SortedSet)13 TreeMap (java.util.TreeMap)12 Set (java.util.Set)11 Ip (org.batfish.datamodel.Ip)10 Batfish (org.batfish.main.Batfish)10 TreeSet (java.util.TreeSet)9 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)9 Test (org.junit.Test)9 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)8 Map (java.util.Map)8 Configuration (org.batfish.datamodel.Configuration)8 Interface (org.batfish.datamodel.Interface)8 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 StaticRoute (org.batfish.datamodel.StaticRoute)7 Vrf (org.batfish.datamodel.Vrf)7 ImmutableList (com.google.common.collect.ImmutableList)6 IOException (java.io.IOException)6