@Nonnull public Ip getPrefixWildcard() { int numWildcardBits = MAX_PREFIX_LENGTH - _prefixLength; long wildcardLong = numWildcardBitsToWildcardLong(numWildcardBits); return Ip.create(wildcardLong); }
public synchronized Pair<InterfaceAddress, InterfaceAddress> getNextGeneratedLinkSubnet() { assert _currentGeneratedIpAsLong % 2 == 0; InterfaceAddress val = new InterfaceAddress(Ip.create(_currentGeneratedIpAsLong), Prefix.MAX_PREFIX_LENGTH - 1); InterfaceAddress val2 = new InterfaceAddress( Ip.create(_currentGeneratedIpAsLong + 1), Prefix.MAX_PREFIX_LENGTH - 1); _currentGeneratedIpAsLong += 2L; return new Pair<>(val, val2); }
Ip computeInstancesIfaceIp() { Long generatedIp = _cidrBlock.getStartIp().asLong() + 1L; _allocatedIps.add(generatedIp); _lastGeneratedIp = generatedIp; return Ip.create(generatedIp); }
@Nonnull public Ip getEndIp() { return Ip.create(getNetworkEnd(_ip.asLong(), _prefixLength)); }
/** * Returns the first ip in the prefix that is not a subnet address. When the prefix is /32 or /31, * returns the getStartIp(), otherwise, returns the ip after getStartIp(). */ public Ip getFirstHostIp() { if (_prefixLength >= Prefix.MAX_PREFIX_LENGTH - 1) { return getStartIp(); } else { Ip subnetIp = getStartIp(); return Ip.create(subnetIp.asLong() + 1); } }
/** * Returns the last ip in the prefix that is not a broadcast address. When the prefix is /32 or * /31, returns the getEndIp(), otherwise, returns the ip before getEndIp(). */ public Ip getLastHostIp() { if (_prefixLength >= Prefix.MAX_PREFIX_LENGTH - 1) { return getEndIp(); } else { Ip broadcastIp = getEndIp(); return Ip.create(broadcastIp.asLong() - 1); } }
private Prefix extendPrefixWith(Prefix p, boolean val) { int length = p.getPrefixLength(); assert (length < Prefix.MAX_PREFIX_LENGTH); Ip ip = p.getStartIp(); long l = ip.asLong(); long lnew = l; if (val) { long delta = (long) Math.pow(2, 32 - length - 1); lnew = l + delta; } return Prefix.create(Ip.create(lnew), length + 1); }
/** Returns cluster ID of this peer */ private static Object getClusterId(BgpPeerConfig peer) { return !peer.getRouteReflectorClient() || peer.getClusterId() == null ? null : Ip.create(peer.getClusterId()); }
@Test public void testIntersects() { /* * The second Ip of an IpWildcard indicates which bits of the first Ip * are significant (i.e. not wild). In this example, since the significant bits of * wc1 and wc2 don't overlap, they should intersect (i.e. their bitwise OR is included in each). */ IpWildcard wc1 = new IpWildcard(Ip.create(0x00b0000aL), Ip.create(0x00FF00FFL).inverted()); IpWildcard wc2 = new IpWildcard(Ip.create(0x000cd000L), Ip.create(0x0000FF00L).inverted()); assertThat("wildcards should overlap", wc1.intersects(wc2)); }
@Test public void testNotIntersects() { /* * Since the significant regions of wc1 and wc2 overlap and are not equal, there is no * intersection between them. */ IpWildcard wc1 = new IpWildcard(Ip.create(0x00000F00L), Ip.create(0x0000FF00L).inverted()); IpWildcard wc2 = new IpWildcard(Ip.create(0x0000F000L), Ip.create(0x0000FF00L).inverted()); assertThat("wildcards should not overlap", !wc1.intersects(wc2)); }
@Override public void exitRbnx_cluster_id(Rbnx_cluster_idContext ctx) { if (ctx.ip != null) { _currentBgpNxVrfConfiguration.setClusterId(toIp(ctx.ip)); } else { _currentBgpNxVrfConfiguration.setClusterId(Ip.create(toLong(ctx.ip_as_int))); } }
public IpWildcard(Ip address, Ip wildcardMask) { // Canonicalize the address before passing it to parent, so that #equals works. super(Ip.create(address.asLong() & ~wildcardMask.asLong()), wildcardMask); if (!wildcardMask.valid()) { throw new BatfishException("Invalid wildcard: " + wildcardMask); } }
@Test public void testSupersetOf() { IpWildcard wc1 = new IpWildcard("1.2.0.0/16"); IpWildcard wc2 = new IpWildcard("1.2.3.0/24"); assertThat("IpWildcard.supersetOf should not be strict", wc1.supersetOf(wc1)); assertThat("wc1 should be a superset of wc2", wc1.supersetOf(wc2)); assertThat("wc2 should not be a superset of wc1", !wc2.supersetOf(wc1)); wc1 = new IpWildcard(Ip.create(0x12005600L), Ip.create(0xFF00FF00L).inverted()); wc2 = new IpWildcard(Ip.create(0x12345600L), Ip.create(0xFFFFFF00L).inverted()); assertThat("wc1 should be a superset of wc2", wc1.supersetOf(wc2)); assertThat("wc2 should not be a superset of wc1", !wc2.supersetOf(wc1)); }
@Test public void testConstructors() { assertThat( new IpWildcard(Ip.parse("1.2.3.4")), equalTo(new IpWildcard(Ip.create(0x01020304L), Ip.ZERO))); assertThat(new IpWildcard("1.2.3.4"), equalTo(new IpWildcard(Ip.create(0x01020304L), Ip.ZERO))); assertThat(new IpWildcard("1.2.3.4/8"), equalTo(new IpWildcard("1.0.0.0/8"))); }
@Test public void testContains() { IpSpace ipWildcard = new IpWildcard(Ip.create(0x01010001L), Ip.create(0x0000FF00L)).toIpSpace(); assertThat(ipWildcard, containsIp(Ip.parse("1.1.1.1"))); assertThat(ipWildcard, containsIp(Ip.parse("1.1.255.1"))); assertThat(ipWildcard, not(containsIp(Ip.parse("1.1.0.0")))); }
@Override public void exitCluster_id_bgp_tail(Cluster_id_bgp_tailContext ctx) { Ip clusterId = null; if (ctx.DEC() != null) { long ipAsLong = toLong(ctx.DEC()); clusterId = Ip.create(ipAsLong); } else if (ctx.IP_ADDRESS() != null) { clusterId = toIp(ctx.IP_ADDRESS()); } _currentPeerGroup.setClusterId(clusterId); }
@Test public void testComplement() { IpSpace ipSpace = new IpWildcard(Ip.create(0x01010001L), Ip.create(0x0000FF00L)).toIpSpace().complement(); assertThat(ipSpace, not(containsIp(Ip.parse("1.1.1.1")))); assertThat(ipSpace, not(containsIp(Ip.parse("1.1.255.1")))); assertThat(ipSpace, containsIp(Ip.parse("1.1.0.0"))); }
@Test public void testVisitIpWildcard() { assertThat(SIMPLIFIER.simplify(IpWildcard.ANY.toIpSpace()), equalTo(UniverseIpSpace.INSTANCE)); IpWildcard ipWildcard = new IpWildcard(Ip.parse("1.2.0.5"), Ip.create(0xFFFF00FFL)); assertThat(SIMPLIFIER.simplify(ipWildcard.toIpSpace()), equalTo(ipWildcard.toIpSpace())); }
@Override public Boolean visitShiftIpAddressIntoSubnet(ShiftIpAddressIntoSubnet step) { IpField field = step.getIpField(); Ip oldValue = get(field); Prefix targetSubnet = step.getSubnet(); Prefix currentSubnetPrefix = Prefix.create(oldValue, targetSubnet.getPrefixLength()); long offset = oldValue.asLong() - currentSubnetPrefix.getStartIp().asLong(); Ip newValue = Ip.create(targetSubnet.getStartIp().asLong() + offset); return set(step.getType(), field, oldValue, newValue); } }