* Added methods IpAddress.getIp4Address() and IpAddress.getIp6Address()
to get the Ip4Address and Ip6Address view of the IpAddress.
* Added methods IpPrefix.getIp4Prefix() and IpPrefix.getIp6Prefix()
to get the Ip4Prefix and Ip6Prefix view of the IpPrefix.
Added the corresponding unit tests as well.
diff --git a/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java b/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
index fd0eac4..ad3ca4b 100644
--- a/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
@@ -23,6 +23,7 @@
import java.net.InetAddress;
import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
@@ -77,6 +78,44 @@
}
/**
+ * Tests getting the Ip4Address and Ip6Address view of the IP address.
+ */
+ @Test
+ public void testGetIp4AndIp6AddressView() {
+ IpAddress ipAddress;
+ Ip4Address ip4Address;
+ Ip6Address ip6Address;
+
+ // Pure IPv4 IpAddress
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertThat(ip4Address.toString(), is("1.2.3.4"));
+ assertNull(ip6Address);
+
+ // IPv4 IpAddress that is Ip4Address
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertThat(ip4Address.toString(), is("1.2.3.4"));
+ assertNull(ip6Address);
+
+ // Pure IPv6 IpAddress
+ ipAddress = IpAddress.valueOf("1111:2222::");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertNull(ip4Address);
+ assertThat(ip6Address.toString(), is("1111:2222::"));
+
+ // IPv6 IpAddress that is Ip6Address
+ ipAddress = Ip6Address.valueOf("1111:2222::");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertNull(ip4Address);
+ assertThat(ip6Address.toString(), is("1111:2222::"));
+ }
+
+ /**
* Tests returning an IPv4 address as a byte array.
*/
@Test
diff --git a/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java b/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
index c2171ce..91a4d49 100644
--- a/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
@@ -21,8 +21,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
@@ -145,6 +146,44 @@
}
/**
+ * Tests getting the Ip4Prefix and Ip6Prefix view of the IP prefix.
+ */
+ @Test
+ public void testGetIp4AndIp6PrefixView() {
+ IpPrefix ipPrefix;
+ Ip4Prefix ip4Prefix;
+ Ip6Prefix ip6Prefix;
+
+ // Pure IPv4 IpPrefix
+ ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
+ assertNull(ip6Prefix);
+
+ // IPv4 IpPrefix that is Ip4Prefix
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
+ assertNull(ip6Prefix);
+
+ // Pure IPv6 IpPrefix
+ ipPrefix = IpPrefix.valueOf("1111:2222::/64");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertNull(ip4Prefix);
+ assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
+
+ // IPv6 IpPrefix that is Ip6Prefix
+ ipPrefix = Ip6Prefix.valueOf("1111:2222::/64");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertNull(ip4Prefix);
+ assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
+ }
+
+ /**
* Tests valueOf() converter for IPv4 integer value.
*/
@Test