* 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/main/java/org/onlab/packet/IpAddress.java b/utils/misc/src/main/java/org/onlab/packet/IpAddress.java
index 45c0207..2906798 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IpAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IpAddress.java
@@ -80,6 +80,42 @@
}
/**
+ * Gets the {@link Ip4Address} view of the IP address.
+ *
+ * @return the {@link Ip4Address} view of the IP address if it is IPv4,
+ * otherwise null
+ */
+ public Ip4Address getIp4Address() {
+ if (version() != Ip4Address.VERSION) {
+ return null;
+ }
+
+ // Return this object itself if it is already instance of Ip4Address
+ if (this instanceof Ip4Address) {
+ return (Ip4Address) this;
+ }
+ return Ip4Address.valueOf(octets);
+ }
+
+ /**
+ * Gets the {@link Ip6Address} view of the IP address.
+ *
+ * @return the {@link Ip6Address} view of the IP address if it is IPv6,
+ * otherwise null
+ */
+ public Ip6Address getIp6Address() {
+ if (version() != Ip6Address.VERSION) {
+ return null;
+ }
+
+ // Return this object itself if it is already instance of Ip6Address
+ if (this instanceof Ip6Address) {
+ return (Ip6Address) this;
+ }
+ return Ip6Address.valueOf(octets);
+ }
+
+ /**
* Returns the IP address as a byte array.
*
* @return a byte array
diff --git a/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java b/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java
index 83592aa..c2fc175 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java
@@ -75,6 +75,42 @@
}
/**
+ * Gets the {@link Ip4Prefix} view of the IP prefix.
+ *
+ * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4,
+ * otherwise null
+ */
+ public Ip4Prefix getIp4Prefix() {
+ if (version() != Ip4Prefix.VERSION) {
+ return null;
+ }
+
+ // Return this object itself if it is already instance of Ip4Prefix
+ if (this instanceof Ip4Prefix) {
+ return (Ip4Prefix) this;
+ }
+ return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength);
+ }
+
+ /**
+ * Gets the {@link Ip6Prefix} view of the IP prefix.
+ *
+ * @return the {@link Ip6Prefix} view of the IP prefix if it is IPv6,
+ * otherwise null
+ */
+ public Ip6Prefix getIp6Prefix() {
+ if (version() != Ip6Prefix.VERSION) {
+ return null;
+ }
+
+ // Return this object itself if it is already instance of Ip6Prefix
+ if (this instanceof Ip6Prefix) {
+ return (Ip6Prefix) this;
+ }
+ return Ip6Prefix.valueOf(address.getIp6Address(), prefixLength);
+ }
+
+ /**
* Converts an integer and a prefix length into an IPv4 prefix.
*
* @param address an integer representing the IPv4 address
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