public static String getIp6FromRange(final String ip6Range) { final String[] ips = ip6Range.split("-"); final String startIp = ips[0]; final IPv6Address start = IPv6Address.fromString(startIp); final BigInteger gap = countIp6InRange(ip6Range); BigInteger next = new BigInteger(gap.bitLength(), s_rand); while (next.compareTo(gap) >= 0) { next = new BigInteger(gap.bitLength(), s_rand); } InetAddress resultAddr = null; final BigInteger startInt = convertIPv6AddressToBigInteger(start); if (startInt != null) { final BigInteger resultInt = startInt.add(next); try { resultAddr = InetAddress.getByAddress(resultInt.toByteArray()); } catch (final UnknownHostException e) { return null; } } if( resultAddr != null) { final IPv6Address ip = IPv6Address.fromInetAddress(resultAddr); return ip.toString(); } return null; }
@Override public boolean isIP6AddressAvailableInVlan(long vlanId) { VlanVO vlan = _vlanDao.findById(vlanId); if (vlan.getIp6Range() == null) { return false; } long existedCount = _ipv6Dao.countExistedIpsInVlan(vlanId); BigInteger existedInt = BigInteger.valueOf(existedCount); BigInteger rangeInt = NetUtils.countIp6InRange(vlan.getIp6Range()); return (existedInt.compareTo(rangeInt) < 0); }
@Test public void testCountIp6InRangeWithNoEnd() { assertEquals(new BigInteger("1"), NetUtils.countIp6InRange("1234:5678::2")); }
@Test public void testCountIp6InRange() { assertEquals(new BigInteger("2"), NetUtils.countIp6InRange("1234:5678::1-1234:5678::2")); }
@Test public void testCountIp6InRangeWithInvalidRange() { assertEquals(null, NetUtils.countIp6InRange("1234:5678::2-1234:5678::0")); }
@Test public void testCountIp6InRangeWithNullStart() { assertEquals(null, NetUtils.countIp6InRange("-1234:5678::0")); }
public static String getIp6FromRange(final String ip6Range) { final String[] ips = ip6Range.split("-"); final String startIp = ips[0]; final IPv6Address start = IPv6Address.fromString(startIp); final BigInteger gap = countIp6InRange(ip6Range); BigInteger next = new BigInteger(gap.bitLength(), s_rand); while (next.compareTo(gap) >= 0) { next = new BigInteger(gap.bitLength(), s_rand); } InetAddress resultAddr = null; final BigInteger startInt = convertIPv6AddressToBigInteger(start); if (startInt != null) { final BigInteger resultInt = startInt.add(next); try { resultAddr = InetAddress.getByAddress(resultInt.toByteArray()); } catch (final UnknownHostException e) { return null; } } if (resultAddr != null) { final IPv6Address ip = IPv6Address.fromInetAddress(resultAddr); return ip.toString(); } return null; }
@Test public void testCountIp6InRangeWithNoEnd() { assertEquals(new BigInteger("1"), NetUtils.countIp6InRange("1234:5678::2")); }
@Test public void testCountIp6InRange() { assertEquals(new BigInteger("2"), NetUtils.countIp6InRange("1234:5678::1-1234:5678::2")); }
@Test public void testCountIp6InRangeWithNullStart() { assertEquals(null, NetUtils.countIp6InRange("-1234:5678::0")); }
@Test public void testCountIp6InRangeWithInvalidRange() { assertEquals(null, NetUtils.countIp6InRange("1234:5678::2-1234:5678::0")); }