Search in sources :

Example 56 with Prefix

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

the class BdpDataPlanePluginTest method testBgpCompareOriginId.

@Test
public void testBgpCompareOriginId() {
    String hostname = "r1";
    Configuration c = BatfishTestUtils.createTestConfiguration(hostname, ConfigurationFormat.CISCO_IOS);
    BgpProcess proc = new BgpProcess();
    c.getVrfs().computeIfAbsent(DEFAULT_VRF_NAME, Vrf::new).setBgpProcess(proc);
    Map<String, Node> nodes = new HashMap<String, Node>();
    Node node = new Node(c);
    nodes.put(hostname, node);
    VirtualRouter vr = new VirtualRouter(DEFAULT_VRF_NAME, c);
    BgpBestPathRib bbr = BgpBestPathRib.initial(vr);
    BgpMultipathRib bmr = new BgpMultipathRib(vr);
    Prefix p = Prefix.ZERO;
    BgpRoute.Builder b = new BgpRoute.Builder().setNetwork(p).setProtocol(RoutingProtocol.IBGP);
    /*
     *  Initialize with different originator ips, which should not affect comparison of routes with
     *  different origin type.
     */
    Map<OriginType, List<BgpRoute>> routesByOriginType = new LinkedHashMap<>();
    for (OriginType originType : OriginType.values()) {
        List<BgpRoute> routes = routesByOriginType.computeIfAbsent(originType, o -> new ArrayList<>());
        routes.add(b.setOriginatorIp(Ip.ZERO).setReceivedFromIp(Ip.ZERO).setOriginType(originType).build());
        routes.add(b.setOriginatorIp(Ip.MAX).setReceivedFromIp(Ip.MAX).setOriginType(originType).build());
    }
    /*
     * Whenever origin type is different, it should be overriding factor in preference.
     */
    for (OriginType o1 : OriginType.values()) {
        List<BgpRoute> lhsList = routesByOriginType.get(o1);
        for (OriginType o2 : OriginType.values()) {
            List<BgpRoute> rhsList = routesByOriginType.get(o2);
            for (BgpRoute lhs : lhsList) {
                for (BgpRoute rhs : rhsList) {
                    if (o1.getPreference() > o2.getPreference()) {
                        assertThat(bbr.comparePreference(lhs, rhs), greaterThan(0));
                        assertThat(bmr.comparePreference(lhs, rhs), greaterThan(0));
                    } else if (o1.getPreference() < o2.getPreference()) {
                        assertThat(bbr.comparePreference(lhs, rhs), lessThan(0));
                        assertThat(bmr.comparePreference(lhs, rhs), lessThan(0));
                    }
                }
            }
        }
    }
}
Also used : OriginType(org.batfish.datamodel.OriginType) Configuration(org.batfish.datamodel.Configuration) BgpProcess(org.batfish.datamodel.BgpProcess) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Prefix(org.batfish.datamodel.Prefix) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) IpAccessList(org.batfish.datamodel.IpAccessList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BgpRoute(org.batfish.datamodel.BgpRoute) Test(org.junit.Test)

Example 57 with Prefix

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

the class BdpDataPlanePluginTest method testIbgpRejectSameNeighborID.

@Test
public void testIbgpRejectSameNeighborID() throws IOException {
    String testrigName = "ibgp-reject-routerid-match";
    List<String> configurationNames = ImmutableList.of("r1", "r2", "r3", "r4");
    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> r2Routes = routes.get("r2").get(DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r3Routes = routes.get("r3").get(DEFAULT_VRF_NAME);
    Set<Prefix> r2Prefixes = r2Routes.stream().map(r -> r.getNetwork()).collect(Collectors.toSet());
    Set<Prefix> r3Prefixes = r3Routes.stream().map(r -> r.getNetwork()).collect(Collectors.toSet());
    // 9.9.9.9/32 is the prefix we test with
    Prefix r1AdvertisedPrefix = Prefix.parse("9.9.9.9/32");
    // Ensure that the prefix is accepted by r2, because router ids are different
    assertThat(r1AdvertisedPrefix, isIn(r2Prefixes));
    // Ensure that the prefix is rejected by r3, because router ids are the same
    assertThat(r1AdvertisedPrefix, not(isIn(r3Prefixes)));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.isA(org.hamcrest.Matchers.isA) SortedSet(java.util.SortedSet) Matchers.not(org.hamcrest.Matchers.not) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Interface(org.batfish.datamodel.Interface) Assert.assertThat(org.junit.Assert.assertThat) Flow(org.batfish.datamodel.Flow) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Topology(org.batfish.datamodel.Topology) Map(java.util.Map) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Vrf(org.batfish.datamodel.Vrf) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.allOf(org.hamcrest.Matchers.allOf) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) OriginType(org.batfish.datamodel.OriginType) AsPath(org.batfish.datamodel.AsPath) Settings(org.batfish.config.Settings) List(java.util.List) Stream(java.util.stream.Stream) SourceNat(org.batfish.datamodel.SourceNat) MultipathEquivalentAsPathMatchMode(org.batfish.datamodel.MultipathEquivalentAsPathMatchMode) Assert.assertFalse(org.junit.Assert.assertFalse) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) Matchers.containsString(org.hamcrest.Matchers.containsString) SetDefaultPolicy(org.batfish.datamodel.routing_policy.statement.SetDefaultPolicy) SortedMap(java.util.SortedMap) BatfishLogger(org.batfish.common.BatfishLogger) Ip(org.batfish.datamodel.Ip) BatfishTestUtils(org.batfish.main.BatfishTestUtils) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) BdpAnswerElement(org.batfish.datamodel.answers.BdpAnswerElement) HashMap(java.util.HashMap) BatfishException(org.batfish.common.BatfishException) BgpProcess(org.batfish.datamodel.BgpProcess) IpAccessList(org.batfish.datamodel.IpAccessList) DataPlanePlugin(org.batfish.common.plugin.DataPlanePlugin) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) DEFAULT_VRF_NAME(org.batfish.datamodel.Configuration.DEFAULT_VRF_NAME) ImmutableList(com.google.common.collect.ImmutableList) AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Configuration(org.batfish.datamodel.Configuration) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BgpRoute(org.batfish.datamodel.BgpRoute) LineAction(org.batfish.datamodel.LineAction) Batfish(org.batfish.main.Batfish) Matchers.isIn(org.hamcrest.Matchers.isIn) TestrigText(org.batfish.main.TestrigText) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) NetworkFactory(org.batfish.datamodel.NetworkFactory) StaticRoute(org.batfish.datamodel.StaticRoute) Route(org.batfish.datamodel.Route) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) BdpOscillationException(org.batfish.common.BdpOscillationException) FeatureMatcher(org.hamcrest.FeatureMatcher) Rule(org.junit.Rule) TreeMap(java.util.TreeMap) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Matcher(org.hamcrest.Matcher) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Prefix(org.batfish.datamodel.Prefix) 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 58 with Prefix

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

the class BdpDataPlanePluginTest method testIosRtStaticMatchesBdp.

@Test
public void testIosRtStaticMatchesBdp() throws IOException {
    String testrigResourcePrefix = TESTRIGS_PREFIX + "ios-rt-static-ad";
    List<String> configurationNames = ImmutableList.of("r1");
    List<String> routingTableNames = ImmutableList.of("r1");
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(testrigResourcePrefix, configurationNames).setRoutingTablesText(testrigResourcePrefix, routingTableNames).build(), _folder);
    BdpDataPlanePlugin dataPlanePlugin = new BdpDataPlanePlugin();
    dataPlanePlugin.initialize(batfish);
    batfish.computeDataPlane(false);
    SortedMap<String, RoutesByVrf> environmentRoutes = batfish.loadEnvironmentRoutingTables();
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routes = dataPlanePlugin.getRoutes(batfish.loadDataPlane());
    Prefix staticRoutePrefix = Prefix.parse("10.0.0.0/8");
    SortedSet<AbstractRoute> r1BdpRoutes = routes.get("r1").get(DEFAULT_VRF_NAME);
    AbstractRoute r1BdpRoute = r1BdpRoutes.stream().filter(r -> r.getNetwork().equals(staticRoutePrefix)).findFirst().get();
    SortedSet<Route> r1EnvironmentRoutes = environmentRoutes.get("r1").get(DEFAULT_VRF_NAME);
    Route r1EnvironmentRoute = r1EnvironmentRoutes.stream().filter(r -> r.getNetwork().equals(staticRoutePrefix)).findFirst().get();
    assertThat(r1BdpRoute.getAdministrativeCost(), equalTo(r1EnvironmentRoute.getAdministrativeCost()));
    assertThat(r1BdpRoute.getMetric(), equalTo(r1EnvironmentRoute.getMetric()));
    assertThat(r1BdpRoute.getProtocol(), equalTo(r1EnvironmentRoute.getProtocol()));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.containsString(org.hamcrest.Matchers.containsString) Prefix(org.batfish.datamodel.Prefix) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Batfish(org.batfish.main.Batfish) AbstractRoute(org.batfish.datamodel.AbstractRoute) BgpRoute(org.batfish.datamodel.BgpRoute) StaticRoute(org.batfish.datamodel.StaticRoute) Route(org.batfish.datamodel.Route) Test(org.junit.Test)

Example 59 with Prefix

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

the class BdpDataPlanePluginTest method testBgpOscillationRecovery.

private void testBgpOscillationRecovery(MockBdpSettings bdpSettings) throws IOException {
    String testrigName = "bgp-oscillation";
    List<String> configurationNames = ImmutableList.of("r1", "r2", "r3");
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(TESTRIGS_PREFIX + testrigName, configurationNames).build(), _folder);
    Settings settings = batfish.getSettings();
    settings.setBdpDetail(bdpSettings.getBdpDetail());
    settings.setBdpMaxOscillationRecoveryAttempts(bdpSettings.getBdpMaxOscillationRecoveryAttempts());
    settings.setBdpMaxRecordedIterations(bdpSettings.getBdpMaxRecordedIterations());
    settings.setBdpPrintAllIterations(bdpSettings.getBdpPrintAllIterations());
    settings.setBdpPrintOscillatingIterations(bdpSettings.getBdpPrintOscillatingIterations());
    settings.setBdpRecordAllIterations(bdpSettings.getBdpRecordAllIterations());
    BdpDataPlanePlugin dataPlanePlugin = new BdpDataPlanePlugin();
    dataPlanePlugin.initialize(batfish);
    /*
     * Data plane computation succeeds iff recovery is enabled. If disabled, an exception is thrown
     * and should be expected by caller.
     */
    batfish.computeDataPlane(false);
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routes = dataPlanePlugin.getRoutes(batfish.loadDataPlane());
    Prefix bgpPrefix = Prefix.parse("1.1.1.1/32");
    SortedSet<AbstractRoute> r2Routes = routes.get("r2").get(DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r3Routes = routes.get("r3").get(DEFAULT_VRF_NAME);
    Stream<AbstractRoute> r2MatchingRoutes = r2Routes.stream().filter(r -> r.getNetwork().equals(bgpPrefix));
    Stream<AbstractRoute> r3MatchingRoutes = r3Routes.stream().filter(r -> r.getNetwork().equals(bgpPrefix));
    AbstractRoute r2Route = r2Routes.stream().filter(r -> r.getNetwork().equals(bgpPrefix)).findAny().get();
    AbstractRoute r3Route = r3Routes.stream().filter(r -> r.getNetwork().equals(bgpPrefix)).findAny().get();
    String r2NextHop = r2Route.getNextHop();
    String r3NextHop = r3Route.getNextHop();
    int routesWithR1AsNextHop = 0;
    if (r2Route.getNextHop().equals("r1")) {
        routesWithR1AsNextHop++;
    }
    if (r3Route.getNextHop().equals("r1")) {
        routesWithR1AsNextHop++;
    }
    boolean r2AsNextHop = r3NextHop.equals("r2");
    boolean r3AsNextHop = r2NextHop.equals("r3");
    /*
     * Data plane computation should succeed as follows if recovery is enabled.
     */
    assertThat(r2MatchingRoutes.count(), equalTo(1L));
    assertThat(r3MatchingRoutes.count(), equalTo(1L));
    assertThat(routesWithR1AsNextHop, equalTo(1));
    assertTrue((r2AsNextHop && !r3AsNextHop) || (!r2AsNextHop && r3AsNextHop));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.containsString(org.hamcrest.Matchers.containsString) Prefix(org.batfish.datamodel.Prefix) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Batfish(org.batfish.main.Batfish) Settings(org.batfish.config.Settings)

Example 60 with Prefix

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

the class BdpDataPlanePluginTest method testEbgpAcceptSameNeighborID.

@Test
public void testEbgpAcceptSameNeighborID() throws IOException {
    String testrigName = "ebgp-accept-routerid-match";
    List<String> configurationNames = ImmutableList.of("r1", "r2", "r3");
    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> r1Routes = routes.get("r1").get(DEFAULT_VRF_NAME);
    SortedSet<AbstractRoute> r3Routes = routes.get("r3").get(DEFAULT_VRF_NAME);
    Set<Prefix> r1Prefixes = r1Routes.stream().map(r -> r.getNetwork()).collect(Collectors.toSet());
    Set<Prefix> r3Prefixes = r3Routes.stream().map(r -> r.getNetwork()).collect(Collectors.toSet());
    Prefix r1Loopback0Prefix = Prefix.parse("1.0.0.1/32");
    Prefix r3Loopback0Prefix = Prefix.parse("3.0.0.3/32");
    // Ensure that r3loopback was accepted by r1
    assertThat(r3Loopback0Prefix, isIn(r1Prefixes));
    // Check the other direction (r1loopback is accepted by r3)
    assertThat(r1Loopback0Prefix, isIn(r3Prefixes));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.isA(org.hamcrest.Matchers.isA) SortedSet(java.util.SortedSet) Matchers.not(org.hamcrest.Matchers.not) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Interface(org.batfish.datamodel.Interface) Assert.assertThat(org.junit.Assert.assertThat) Flow(org.batfish.datamodel.Flow) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Topology(org.batfish.datamodel.Topology) Map(java.util.Map) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Vrf(org.batfish.datamodel.Vrf) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.allOf(org.hamcrest.Matchers.allOf) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) OriginType(org.batfish.datamodel.OriginType) AsPath(org.batfish.datamodel.AsPath) Settings(org.batfish.config.Settings) List(java.util.List) Stream(java.util.stream.Stream) SourceNat(org.batfish.datamodel.SourceNat) MultipathEquivalentAsPathMatchMode(org.batfish.datamodel.MultipathEquivalentAsPathMatchMode) Assert.assertFalse(org.junit.Assert.assertFalse) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) Matchers.containsString(org.hamcrest.Matchers.containsString) SetDefaultPolicy(org.batfish.datamodel.routing_policy.statement.SetDefaultPolicy) SortedMap(java.util.SortedMap) BatfishLogger(org.batfish.common.BatfishLogger) Ip(org.batfish.datamodel.Ip) BatfishTestUtils(org.batfish.main.BatfishTestUtils) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) BdpAnswerElement(org.batfish.datamodel.answers.BdpAnswerElement) HashMap(java.util.HashMap) BatfishException(org.batfish.common.BatfishException) BgpProcess(org.batfish.datamodel.BgpProcess) IpAccessList(org.batfish.datamodel.IpAccessList) DataPlanePlugin(org.batfish.common.plugin.DataPlanePlugin) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) DEFAULT_VRF_NAME(org.batfish.datamodel.Configuration.DEFAULT_VRF_NAME) ImmutableList(com.google.common.collect.ImmutableList) AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Configuration(org.batfish.datamodel.Configuration) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BgpRoute(org.batfish.datamodel.BgpRoute) LineAction(org.batfish.datamodel.LineAction) Batfish(org.batfish.main.Batfish) Matchers.isIn(org.hamcrest.Matchers.isIn) TestrigText(org.batfish.main.TestrigText) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) NetworkFactory(org.batfish.datamodel.NetworkFactory) StaticRoute(org.batfish.datamodel.StaticRoute) Route(org.batfish.datamodel.Route) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) BdpOscillationException(org.batfish.common.BdpOscillationException) FeatureMatcher(org.hamcrest.FeatureMatcher) Rule(org.junit.Rule) TreeMap(java.util.TreeMap) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Matcher(org.hamcrest.Matcher) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Prefix(org.batfish.datamodel.Prefix) 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)

Aggregations

Prefix (org.batfish.datamodel.Prefix)133 Ip (org.batfish.datamodel.Ip)53 Configuration (org.batfish.datamodel.Configuration)33 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)29 Interface (org.batfish.datamodel.Interface)28 BatfishException (org.batfish.common.BatfishException)22 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)20 SubRange (org.batfish.datamodel.SubRange)19 HashMap (java.util.HashMap)18 StaticRoute (org.batfish.datamodel.StaticRoute)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)17 BgpProcess (org.batfish.datamodel.BgpProcess)17 SortedSet (java.util.SortedSet)16 TreeSet (java.util.TreeSet)16 AbstractRoute (org.batfish.datamodel.AbstractRoute)16 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)16 TreeMap (java.util.TreeMap)14 HashSet (java.util.HashSet)13