Pavlin Radoslavov

Added new method IpAddress.valueOf(InetAddress inetAddress)

and use it as appropriate inside DistributedClusterStore.memberAddress()
......@@ -147,9 +147,7 @@ public class DistributedClusterStore
}
private IpAddress memberAddress(Member member) {
byte[] address = member.getSocketAddress().getAddress().getAddress();
// TODO: Add support for IPv6
return IpAddress.valueOf(IpAddress.Version.INET, address);
return IpAddress.valueOf(member.getSocketAddress().getAddress());
}
// Interceptor for membership events.
......
......@@ -164,6 +164,33 @@ public final class IpAddress implements Comparable<IpAddress> {
}
/**
* Converts an InetAddress into an IP address.
*
* @param inetAddress the InetAddress value to use
* @return an IP address
* @throws IllegalArgumentException if the argument is invalid
*/
public static IpAddress valueOf(InetAddress inetAddress) {
byte[] bytes = inetAddress.getAddress();
if (inetAddress instanceof Inet4Address) {
return new IpAddress(Version.INET, bytes);
}
if (inetAddress instanceof Inet6Address) {
return new IpAddress(Version.INET6, bytes);
}
// Use the number of bytes as a hint
if (bytes.length == INET_BYTE_LENGTH) {
return new IpAddress(Version.INET, bytes);
}
if (bytes.length == INET6_BYTE_LENGTH) {
return new IpAddress(Version.INET6, bytes);
}
final String msg = "Unrecognized IP version address string: " +
inetAddress.toString();
throw new IllegalArgumentException(msg);
}
/**
* Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or
* "1111:2222::8888") into an IP address.
*
......@@ -172,22 +199,14 @@ public final class IpAddress implements Comparable<IpAddress> {
* @throws IllegalArgumentException if the argument is invalid
*/
public static IpAddress valueOf(String value) {
InetAddress addr = null;
InetAddress inetAddress = null;
try {
addr = InetAddresses.forString(value);
inetAddress = InetAddresses.forString(value);
} catch (IllegalArgumentException e) {
final String msg = "Invalid IP address string: " + value;
throw new IllegalArgumentException(msg);
}
byte[] bytes = addr.getAddress();
if (addr instanceof Inet4Address) {
return new IpAddress(Version.INET, bytes);
}
if (addr instanceof Inet6Address) {
return new IpAddress(Version.INET6, bytes);
}
final String msg = "Unrecognized IP version address string: " + value;
throw new IllegalArgumentException(msg);
return valueOf(inetAddress);
}
/**
......
......@@ -15,6 +15,9 @@
*/
package org.onlab.packet;
import java.net.InetAddress;
import com.google.common.net.InetAddresses;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
......@@ -367,6 +370,52 @@ public class IpAddressTest {
}
/**
* Tests valueOf() converter for IPv4 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv4() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress = InetAddresses.forString("1.2.3.4");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("1.2.3.4"));
inetAddress = InetAddresses.forString("0.0.0.0");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("0.0.0.0"));
inetAddress = InetAddresses.forString("255.255.255.255");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv6 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv6() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress =
InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
inetAddress = InetAddresses.forString("::");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("::"));
inetAddress =
InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests valueOf() converter for IPv4 string.
*/
@Test
......