/** * Adds the given {@link Prefix} to this {@link PrefixSpace}. * * @param prefix Prefix to add */ public void addPrefix(Prefix prefix) { addPrefixRange(PrefixRange.fromPrefix(prefix)); }
public boolean containsPrefix(Prefix prefix) { @Nullable Boolean result = _cache.get(prefix); if (result != null) { return result; } else { boolean contained = containsPrefixRange(PrefixRange.fromPrefix(prefix)); _cache.put(prefix, contained); return contained; } }
@JsonCreator public static PrefixRange fromString(String prefixRangeStr) { String[] parts = prefixRangeStr.split(":"); if (parts.length == 1) { return fromPrefix(Prefix.parse(parts[0])); } else if (parts.length == 2) { return new PrefixRange(Prefix.parse(parts[0]), new SubRange(parts[1])); } else { throw new BatfishException("Invalid PrefixRange string: '" + prefixRangeStr + "'"); } }
/** * 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()); }
/** * Tests that getting a range more specific than a /32 does not crash and is empty -- or at least * does not contain the initial /32. */ @Test public void testEmptyRange() { Prefix slash32 = Prefix.parse("1.2.3.4/32"); PrefixRange empty = PrefixRange.moreSpecificThan(slash32); assertFalse(empty.includesPrefixRange(PrefixRange.fromPrefix(slash32))); } }
public void addPrefixRange(PrefixRange prefixRange) { Prefix prefix = prefixRange.getPrefix(); BitSet bits = getAddressBits(prefix.getStartIp()); // The minimum length of the range may be shorter than the actual prefix length. // If so, we need to specially handle all shorter prefixes with a custom address and bitset. int minLength = prefixRange.getLengthRange().getStart(); int maxLength = Math.min(prefixRange.getLengthRange().getEnd(), prefix.getPrefixLength() - 1); for (int currentLength = minLength; currentLength <= maxLength; currentLength++) { Prefix currentPrefix = Prefix.create(prefix.getStartIp(), currentLength); PrefixRange currentPrefixRange = PrefixRange.fromPrefix(currentPrefix); BitSet currentBits = getAddressBits(currentPrefix.getStartIp()); _root.addPrefixRange(currentPrefixRange, currentBits, currentLength, 0); } // Otherwise, add the prefix range as-is. _root.addPrefixRange(prefixRange, bits, prefix.getPrefixLength(), 0); }
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(
new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet(new PrefixSpace(PrefixRange.fromPrefix(prefix))))); generateAggregateConditions.add(new MatchProtocol(RoutingProtocol.AGGREGATE));