Pavlin Radoslavov

Added new method IpAddress.valueOf(InetAddress inetAddress)

and use it as appropriate inside DistributedClusterStore.memberAddress()
...@@ -147,9 +147,7 @@ public class DistributedClusterStore ...@@ -147,9 +147,7 @@ public class DistributedClusterStore
147 } 147 }
148 148
149 private IpAddress memberAddress(Member member) { 149 private IpAddress memberAddress(Member member) {
150 - byte[] address = member.getSocketAddress().getAddress().getAddress(); 150 + return IpAddress.valueOf(member.getSocketAddress().getAddress());
151 - // TODO: Add support for IPv6
152 - return IpAddress.valueOf(IpAddress.Version.INET, address);
153 } 151 }
154 152
155 // Interceptor for membership events. 153 // Interceptor for membership events.
......
...@@ -164,6 +164,33 @@ public final class IpAddress implements Comparable<IpAddress> { ...@@ -164,6 +164,33 @@ public final class IpAddress implements Comparable<IpAddress> {
164 } 164 }
165 165
166 /** 166 /**
167 + * Converts an InetAddress into an IP address.
168 + *
169 + * @param inetAddress the InetAddress value to use
170 + * @return an IP address
171 + * @throws IllegalArgumentException if the argument is invalid
172 + */
173 + public static IpAddress valueOf(InetAddress inetAddress) {
174 + byte[] bytes = inetAddress.getAddress();
175 + if (inetAddress instanceof Inet4Address) {
176 + return new IpAddress(Version.INET, bytes);
177 + }
178 + if (inetAddress instanceof Inet6Address) {
179 + return new IpAddress(Version.INET6, bytes);
180 + }
181 + // Use the number of bytes as a hint
182 + if (bytes.length == INET_BYTE_LENGTH) {
183 + return new IpAddress(Version.INET, bytes);
184 + }
185 + if (bytes.length == INET6_BYTE_LENGTH) {
186 + return new IpAddress(Version.INET6, bytes);
187 + }
188 + final String msg = "Unrecognized IP version address string: " +
189 + inetAddress.toString();
190 + throw new IllegalArgumentException(msg);
191 + }
192 +
193 + /**
167 * Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or 194 * Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or
168 * "1111:2222::8888") into an IP address. 195 * "1111:2222::8888") into an IP address.
169 * 196 *
...@@ -172,22 +199,14 @@ public final class IpAddress implements Comparable<IpAddress> { ...@@ -172,22 +199,14 @@ public final class IpAddress implements Comparable<IpAddress> {
172 * @throws IllegalArgumentException if the argument is invalid 199 * @throws IllegalArgumentException if the argument is invalid
173 */ 200 */
174 public static IpAddress valueOf(String value) { 201 public static IpAddress valueOf(String value) {
175 - InetAddress addr = null; 202 + InetAddress inetAddress = null;
176 try { 203 try {
177 - addr = InetAddresses.forString(value); 204 + inetAddress = InetAddresses.forString(value);
178 } catch (IllegalArgumentException e) { 205 } catch (IllegalArgumentException e) {
179 final String msg = "Invalid IP address string: " + value; 206 final String msg = "Invalid IP address string: " + value;
180 throw new IllegalArgumentException(msg); 207 throw new IllegalArgumentException(msg);
181 } 208 }
182 - byte[] bytes = addr.getAddress(); 209 + return valueOf(inetAddress);
183 - if (addr instanceof Inet4Address) {
184 - return new IpAddress(Version.INET, bytes);
185 - }
186 - if (addr instanceof Inet6Address) {
187 - return new IpAddress(Version.INET6, bytes);
188 - }
189 - final String msg = "Unrecognized IP version address string: " + value;
190 - throw new IllegalArgumentException(msg);
191 } 210 }
192 211
193 /** 212 /**
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
15 */ 15 */
16 package org.onlab.packet; 16 package org.onlab.packet;
17 17
18 +import java.net.InetAddress;
19 +
20 +import com.google.common.net.InetAddresses;
18 import org.junit.Test; 21 import org.junit.Test;
19 22
20 import static org.hamcrest.Matchers.is; 23 import static org.hamcrest.Matchers.is;
...@@ -367,6 +370,52 @@ public class IpAddressTest { ...@@ -367,6 +370,52 @@ public class IpAddressTest {
367 } 370 }
368 371
369 /** 372 /**
373 + * Tests valueOf() converter for IPv4 InetAddress.
374 + */
375 + @Test
376 + public void testValueOfInetAddressIPv4() {
377 + IpAddress ipAddress;
378 + InetAddress inetAddress;
379 +
380 + inetAddress = InetAddresses.forString("1.2.3.4");
381 + ipAddress = IpAddress.valueOf(inetAddress);
382 + assertThat(ipAddress.toString(), is("1.2.3.4"));
383 +
384 + inetAddress = InetAddresses.forString("0.0.0.0");
385 + ipAddress = IpAddress.valueOf(inetAddress);
386 + assertThat(ipAddress.toString(), is("0.0.0.0"));
387 +
388 + inetAddress = InetAddresses.forString("255.255.255.255");
389 + ipAddress = IpAddress.valueOf(inetAddress);
390 + assertThat(ipAddress.toString(), is("255.255.255.255"));
391 + }
392 +
393 + /**
394 + * Tests valueOf() converter for IPv6 InetAddress.
395 + */
396 + @Test
397 + public void testValueOfInetAddressIPv6() {
398 + IpAddress ipAddress;
399 + InetAddress inetAddress;
400 +
401 + inetAddress =
402 + InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
403 + ipAddress = IpAddress.valueOf(inetAddress);
404 + assertThat(ipAddress.toString(),
405 + is("1111:2222:3333:4444:5555:6666:7777:8888"));
406 +
407 + inetAddress = InetAddresses.forString("::");
408 + ipAddress = IpAddress.valueOf(inetAddress);
409 + assertThat(ipAddress.toString(), is("::"));
410 +
411 + inetAddress =
412 + InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
413 + ipAddress = IpAddress.valueOf(inetAddress);
414 + assertThat(ipAddress.toString(),
415 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
416 + }
417 +
418 + /**
370 * Tests valueOf() converter for IPv4 string. 419 * Tests valueOf() converter for IPv4 string.
371 */ 420 */
372 @Test 421 @Test
......