Pavlin Radoslavov

Completed the IPv6 implementation for IpPrefix, and added

a new set of IpPrefix unit tests.

Also, fix few nits in IpAddress and IpAddressTest
......@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState;
/**
* A class representing an IP address.
* TODO: Add support for IPv6 as well.
*/
public final class IpAddress implements Comparable<IpAddress> {
// IP Versions
......
......@@ -17,8 +17,6 @@ package org.onlab.packet;
import java.util.Objects;
// TODO: Add support for IPv6 as well.
/**
* A class representing an IP prefix. A prefix consists of an IP address and
* a subnet mask.
......@@ -40,26 +38,39 @@ public final class IpPrefix {
*
* @param address the IP address
* @param prefixLength the prefix length
* @throws IllegalArgumentException if the prefix length value is invalid
*/
private IpPrefix(IpAddress address, int prefixLength) {
checkPrefixLength(prefixLength);
checkPrefixLength(address.version(), prefixLength);
this.address = IpAddress.makeMaskedAddress(address, prefixLength);
this.prefixLength = (short) prefixLength;
}
/**
* Checks whether the prefix length is valid.
* Returns the IP version of the prefix.
*
* @param prefixLength the prefix length value to check
* @throws IllegalArgumentException if the prefix length value is invalid
* @return the IP version of the prefix
*/
private static void checkPrefixLength(int prefixLength) {
if ((prefixLength < 0) || (prefixLength > MAX_INET_MASK_LENGTH)) {
String msg = "Invalid prefix length " + prefixLength + ". " +
"The value must be in the interval [0, " +
MAX_INET_MASK_LENGTH + "]";
throw new IllegalArgumentException(msg);
}
public IpAddress.Version version() {
return address.version();
}
/**
* Returns the IP address value of the prefix.
*
* @return the IP address value of the prefix
*/
public IpAddress address() {
return address;
}
/**
* Returns the IP address prefix length.
*
* @return the IP address prefix length
*/
public int prefixLength() {
return prefixLength;
}
/**
......@@ -68,6 +79,7 @@ public final class IpPrefix {
* @param address an integer representing the IPv4 address
* @param prefixLength the prefix length
* @return an IP prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public static IpPrefix valueOf(int address, int prefixLength) {
return new IpPrefix(IpAddress.valueOf(address), prefixLength);
......@@ -80,11 +92,11 @@ public final class IpPrefix {
* @param address the IP address value stored in network byte order
* @param prefixLength the prefix length
* @return an IP prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
int prefixLength) {
return new IpPrefix(IpAddress.valueOf(version, address),
prefixLength);
return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
}
/**
......@@ -93,6 +105,7 @@ public final class IpPrefix {
* @param address the IP address
* @param prefixLength the prefix length
* @return an IP prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public static IpPrefix valueOf(IpAddress address, int prefixLength) {
return new IpPrefix(address, prefixLength);
......@@ -104,6 +117,7 @@ public final class IpPrefix {
*
* @param address an IP prefix in string form, e.g. "10.1.0.0/16"
* @return an IP prefix
* @throws IllegalArgumentException if the arguments are invalid
*/
public static IpPrefix valueOf(String address) {
final String[] parts = address.split("/");
......@@ -119,33 +133,6 @@ public final class IpPrefix {
}
/**
* Returns the IP version of the prefix.
*
* @return the IP version of the prefix
*/
public IpAddress.Version version() {
return address.version();
}
/**
* Returns the IP address value of the prefix.
*
* @return the IP address value of the prefix
*/
public IpAddress address() {
return address;
}
/**
* Returns the IP address prefix length.
*
* @return the IP address prefix length
*/
public int prefixLength() {
return prefixLength;
}
/**
* Determines whether a given IP prefix is contained within this prefix.
*
* @param other the IP prefix to test
......@@ -217,4 +204,35 @@ public final class IpPrefix {
builder.append(String.format("%d", prefixLength));
return builder.toString();
}
/**
* Checks whether the prefix length is valid.
*
* @param version the IP address version
* @param prefixLength the prefix length value to check
* @throws IllegalArgumentException if the prefix length value is invalid
*/
private static void checkPrefixLength(IpAddress.Version version,
int prefixLength) {
int maxPrefixLen = 0;
switch (version) {
case INET:
maxPrefixLen = MAX_INET_MASK_LENGTH;
break;
case INET6:
maxPrefixLen = MAX_INET6_MASK_LENGTH;
break;
default:
String msg = "Invalid IP version " + version;
throw new IllegalArgumentException(msg);
}
if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
String msg = "Invalid prefix length " + prefixLength + ". " +
"The value must be in the interval [0, " +
maxPrefixLen + "]";
throw new IllegalArgumentException(msg);
}
}
}
......
......@@ -135,7 +135,7 @@ public class IpAddressTest {
* Tests returning an IPv4 address asn an integer.
*/
@Test
public void testToint() {
public void testToInt() {
IpAddress ipAddress;
ipAddress = IpAddress.valueOf("1.2.3.4");
......@@ -149,10 +149,10 @@ public class IpAddressTest {
}
/**
* Tests valueOf() converter for an integer value.
* Tests valueOf() converter for IPv4 integer value.
*/
@Test
public void testValueOfForInteger() {
public void testValueOfForIntegerIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.valueOf(0x01020304);
......