/** Constructs a BgpProcess */ public BgpProcess() { _activeNeighbors = new TreeMap<>(); _generatedRoutes = new TreeSet<>(); _tieBreaker = BgpTieBreaker.ARRIVAL_ORDER; _clusterIds = new ClusterIdsSupplier(); _originationSpace = new PrefixSpace(); _activeNeighbors = new TreeMap<>(); _passiveNeighbors = new TreeMap<>(); }
@Before public void constructPrefixSpace() { _ps = new PrefixSpace(); }
@Override public BooleanExpr toBooleanExpr(JuniperConfiguration jc, Configuration c, Warnings warnings) { Interface iface = c.getAllInterfaces().get(_name); if (iface == null) { // No such interface, won't match anything return BooleanExprs.FALSE; } // Convert to conjunction of connected protocol and matching at least one of the interface // prefixes return new Conjunction( ImmutableList.of( new MatchProtocol(RoutingProtocol.CONNECTED), new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet( new PrefixSpace( c.getAllInterfaces().get(_name).getAllAddresses().stream() .map(InterfaceAddress::getPrefix) .map(PrefixRange::fromPrefix) .collect(ImmutableSet.toImmutableSet())))))); } }
/** * Returns a new {@link PrefixSpace} containing all {@link Prefix}es shared between this {@link * PrefixSpace} and the one given. * * @param intersectSpace {@link PrefixSpace} with which to find intersecting {@link Prefix}es * @return A new {@link PrefixSpace} containing all shared {@link Prefix}es */ public PrefixSpace intersection(PrefixSpace intersectSpace) { PrefixSpace newSpace = new PrefixSpace(); Set<PrefixRange> intersectRanges = intersectSpace.getPrefixRanges(); for (PrefixRange intersectRange : intersectRanges) { if (containsPrefixRange(intersectRange)) { newSpace.addPrefixRange(intersectRange); } } return newSpace; }
/** * Generates and returns a {@link RoutingPolicy} that matches routes that should be aggregated for * aggregate network indicated by the given {@link Prefix}. * * <p>Does the bookkeeping in the provided {@link Configuration} to ensure the generated policy is * available and tracked. */ static RoutingPolicy generateAggregateRoutePolicy( Configuration c, String vrfName, Prefix prefix) { BooleanExpr matchLongerNetworks = new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet(new PrefixSpace(PrefixRange.moreSpecificThan(prefix)))); If currentGeneratedRouteConditional = new If(matchLongerNetworks, singletonList(Statements.ReturnTrue.toStaticStatement())); RoutingPolicy policy = new RoutingPolicy("~AGGREGATE_ROUTE_GEN:" + vrfName + ":" + prefix + "~", c); policy.setStatements(ImmutableList.of(currentGeneratedRouteConditional)); c.getRoutingPolicies().put(policy.getName(), policy); return policy; }
/** * For each static route in the given {@link RoutingInstance} that has at least one community set, * creates an {@link If} that matches that route (specifically, matches static routes with that * route's destination network), and sets communities for matching exported routes. */ @Nonnull private static List<If> getStaticRouteCommunitySetters(@Nonnull RoutingInstance ri) { MatchProtocol matchStatic = new MatchProtocol(RoutingProtocol.STATIC); return ri.getRibs().get(RoutingInformationBase.RIB_IPV4_UNICAST).getStaticRoutes().values() .stream() .filter(route -> !route.getCommunities().isEmpty()) .map( route -> { // Create matcher that matches routes that share this route's destination network PrefixExpr destNetworkMatcher = DestinationNetwork.instance(); PrefixSetExpr destNetwork = new ExplicitPrefixSet(new PrefixSpace(PrefixRange.fromPrefix(route.getPrefix()))); MatchPrefixSet networkMatcher = new MatchPrefixSet(destNetworkMatcher, destNetwork); // When a matching static route is exported, set its communities return new If( new Conjunction(ImmutableList.of(matchStatic, networkMatcher)), ImmutableList.of( new SetCommunity(new LiteralCommunitySet(route.getCommunities())))); }) .collect(ImmutableList.toImmutableList()); }
PrefixSpace space1 = new PrefixSpace(); boolean empty1 = true; for (Vrf v1 : c1.getVrfs().values()) { PrefixSpace space2 = new PrefixSpace(); boolean empty2 = true; for (Vrf v2 : c2.getVrfs().values()) {
@Test public void addSpaceTest() { PrefixRange range = PrefixRange.fromString("100.0.0.0/32"); _ps.addSpace(new PrefixSpace(range)); assertThat(_ps.isEmpty(), equalTo(false)); assertThat(_ps.containsPrefixRange(range), equalTo(true)); }
@Test public void constructPrefixSpaceTest() { _ps = new PrefixSpace(PrefixRange.fromString("100.0.0.0/32")); assertThat(_ps.isEmpty(), equalTo(false)); assertThat(_ps.containsPrefixRange(PrefixRange.fromString("100.0.0.0/32")), equalTo(true)); }
PrefixRange all = PrefixRange.fromString("0.0.0.0/0:0-32"); PrefixSpace space = new PrefixSpace(); space.addPrefixRange(ten8to15); assertThat(space.getPrefixRanges(), equalTo(ImmutableSet.of(ten8to15)));
new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet(new PrefixSpace(PrefixRange.fromPrefix(prefix))))); generateAggregateConditions.add(new MatchProtocol(RoutingProtocol.AGGREGATE)); .forEach( (prefix, routeMapOrEmpty) -> { PrefixSpace exportSpace = new PrefixSpace(PrefixRange.fromPrefix(prefix)); List<BooleanExpr> exportNetworkConditions = ImmutableList.of(
@Test public void addPrefixRangeTest() { PrefixRange range = PrefixRange.fromString("10.10.10.0/32:16-24"); _ps = new PrefixSpace(range); assertThat(_ps.containsPrefixRange(range), equalTo(true)); assertThat( "Shorter prefixes not included", _ps.containsPrefix(Prefix.parse("10.10.10.0/15")), equalTo(false)); assertThat( "Shortest prefix is included", _ps.containsPrefix(Prefix.parse("10.10.10.0/16")), equalTo(true)); assertThat( "Longest prefix is included", _ps.containsPrefix(Prefix.parse("10.10.10.0/24")), equalTo(true)); assertThat( "Longer prefixes not included", _ps.containsPrefix(Prefix.parse("10.10.10.0/25")), equalTo(false)); assertThat( "Prefixes with mismatch in masked bits not included", _ps.containsPrefix(Prefix.parse("10.10.11.0/24")), equalTo(false)); assertThat( "Prefixes with mismatch in unmasked bits included", _ps.containsPrefix(Prefix.parse("10.10.10.255/24")), equalTo(true)); }
@Test public void intersectionAndOverlapsTest() { PrefixSpace other = new PrefixSpace(); assertThat("empty spaces don't intersect", _ps.intersection(other).isEmpty(), equalTo(true)); assertThat("no overlap for empty spaces", _ps.overlaps(other), equalTo(false)); _ps.addPrefixRange(PrefixRange.fromString("10.10.10.0/20:18-21")); assertThat("no intersection for one empty", _ps.intersection(other).isEmpty(), equalTo(true)); assertThat("no overlap for one empty", _ps.overlaps(other), equalTo(false)); other.addPrefixRange(PrefixRange.fromString("10.10.10.0/16:14-16")); assertThat("no intersection", _ps.intersection(other).isEmpty(), equalTo(true)); assertThat("no overlap", _ps.overlaps(other), equalTo(false)); other.addPrefixRange(PrefixRange.fromString("10.10.10.0/20:18-20")); PrefixSpace intersection = _ps.intersection(other); assertThat( "now intersect for length 18", intersection.containsPrefix(Prefix.parse("10.10.0.0/18")), equalTo(true)); assertThat( "now intersect for length 19", intersection.containsPrefix(Prefix.parse("10.10.0.0/19")), equalTo(true)); assertThat( "now intersect for range 18-20", intersection.containsPrefixRange(PrefixRange.fromString("10.10.0.0/20:18-20")), equalTo(true)); assertThat("don't intersect anywhere else", intersection.getPrefixRanges().size(), equalTo(3)); assertThat("has overlap", _ps.overlaps(other), equalTo(true)); }
new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet(new PrefixSpace(PrefixRange.fromPrefix(prefix))))); generateAggregateConditions.add(new MatchProtocol(RoutingProtocol.AGGREGATE)); PrefixSpace space = new PrefixSpace(); space.addPrefix(prefix); newBgpProcess.addToOriginationSpace(space);
} else { PrefixSpace prefixSpace = new PrefixSpace(); Prefix6Space prefix6Space = new Prefix6Space(); boolean ipv6 = false;