Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/src/com/cloud/network/NetworkModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,8 @@ public boolean isIP6AddressAvailableInVlan(long vlanId) {
}
long existedCount = _ipv6Dao.countExistedIpsInVlan(vlanId);
BigInteger existedInt = BigInteger.valueOf(existedCount);
BigInteger rangeInt = NetUtils.countIp6InRange(vlan.getIp6Range());
return (existedInt.compareTo(rangeInt) < 0);
BigInteger rangeInt = NetUtils.countIp6InRange(vlan.getIp6Range()).get();// old behaviour npe on the next line
return (existedInt.compareTo(rangeInt) < 0);// new behaviour IllegalState Exception on the previous line
}

@Override
Expand Down
23 changes: 14 additions & 9 deletions utils/src/main/java/com/cloud/utils/net/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.cloud.utils.IteratorUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.script.Script;
import com.google.common.base.Optional;
import com.googlecode.ipv6.IPv6Address;
import com.googlecode.ipv6.IPv6AddressRange;
import com.googlecode.ipv6.IPv6Network;
Expand Down Expand Up @@ -1263,7 +1264,11 @@ 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);
final Optional<BigInteger> gapOption = countIp6InRange(ip6Range);
if (! gapOption.isPresent()) {
return null;
}
final BigInteger gap = gapOption.get();
BigInteger next = new BigInteger(gap.bitLength(), s_rand);
while (next.compareTo(gap) >= 0) {
next = new BigInteger(gap.bitLength(), s_rand);
Expand Down Expand Up @@ -1302,26 +1307,26 @@ private static BigInteger convertIPv6AddressToBigInteger(final IPv6Address addr)
}

// Can cover 127 bits
public static BigInteger countIp6InRange(final String ip6Range) {
public static Optional<BigInteger> countIp6InRange(final String ip6Range) {
if (ip6Range == null) {
return null;
return Optional.fromNullable(null);
}
final String[] ips = ip6Range.split("-");
final String startIp = ips[0];
String endIp = ips[0];
if (ips.length > 1) {
endIp = ips[1];
if(ips[0].equals("")) {
return Optional.fromNullable(null);
}
final String startIp = ips[0];
final String endIp = (ips.length > 1) ? ips[1] : ips[0];
try {
final BigInteger startInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(startIp));
final BigInteger endInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(endIp));
if (endInt != null && startInt != null && startInt.compareTo(endInt) <= 0) {
return endInt.subtract(startInt).add(BigInteger.ONE);
return Optional.of(endInt.subtract(startInt).add(BigInteger.ONE));
}
} catch (final IllegalArgumentException ex) {
s_logger.error("Failed to convert a string to an IPv6 address", ex);
}
return null;
return Optional.fromNullable(null);
}

public static boolean isIp6InRange(final String ip6, final String ip6Range) {
Expand Down
9 changes: 5 additions & 4 deletions utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.log4j.Logger;
import org.junit.Test;

import com.google.common.base.Optional;
import com.googlecode.ipv6.IPv6Address;

public class NetUtilsTest {
Expand Down Expand Up @@ -139,22 +140,22 @@ public void testGetIp6FromRange() {

@Test
public void testCountIp6InRange() {
assertEquals(new BigInteger("2"), NetUtils.countIp6InRange("1234:5678::1-1234:5678::2"));
assertEquals(Optional.of(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"));
assertEquals(Optional.absent(), NetUtils.countIp6InRange("1234:5678::2-1234:5678::0"));
}

@Test
public void testCountIp6InRangeWithNullStart() {
assertEquals(null, NetUtils.countIp6InRange("-1234:5678::0"));
assertEquals(Optional.absent(), NetUtils.countIp6InRange("-1234:5678::0"));
}

@Test
public void testCountIp6InRangeWithNoEnd() {
assertEquals(new BigInteger("1"), NetUtils.countIp6InRange("1234:5678::2"));
assertEquals(Optional.of(new BigInteger("1")), NetUtils.countIp6InRange("1234:5678::2"));
}

@Test
Expand Down