Added methods to test whether an IP address/prefix is IPv4 or IPv6:
IpAddress.isIp4()
IpAddress.isIp6()
IpPrefix.isIp4()
IpPrefix.isIp6()
Also, added the corresponding unit tests.
Change-Id: I2b1f08501c94d61f75b15f2c6977c0349e313ebd
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 f8d9f2f..699ad47 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,24 @@
}
/**
+ * Tests whether the IP version of this address is IPv4.
+ *
+ * @return true if the IP version of this address is IPv4, otherwise false.
+ */
+ public boolean isIp4() {
+ return (version() == Ip4Address.VERSION);
+ }
+
+ /**
+ * Tests whether the IP version of this address is IPv6.
+ *
+ * @return true if the IP version of this address is IPv6, otherwise false.
+ */
+ public boolean isIp6() {
+ return (version() == Ip6Address.VERSION);
+ }
+
+ /**
* Gets the {@link Ip4Address} view of the IP address.
*
* @return the {@link Ip4Address} view of the IP address if it is IPv4,
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 4084221..46a920e 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IpPrefix.java
@@ -57,6 +57,24 @@
}
/**
+ * Tests whether the IP version of this prefix is IPv4.
+ *
+ * @return true if the IP version of this prefix is IPv4, otherwise false.
+ */
+ public boolean isIp4() {
+ return address.isIp4();
+ }
+
+ /**
+ * Tests whether the IP version of this prefix is IPv6.
+ *
+ * @return true if the IP version of this prefix is IPv6, otherwise false.
+ */
+ public boolean isIp6() {
+ return address.isIp6();
+ }
+
+ /**
* Returns the IP address value of the prefix.
*
* @return the IP address value of the prefix
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 f0e6ae7..2257991 100644
--- a/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
@@ -78,6 +78,38 @@
}
/**
+ * Tests whether the IP version of an address is IPv4.
+ */
+ @Test
+ public void testIsIp4() {
+ IpAddress ipAddress;
+
+ // IPv4
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertTrue(ipAddress.isIp4());
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertFalse(ipAddress.isIp4());
+ }
+
+ /**
+ * Tests whether the IP version of an address is IPv6.
+ */
+ @Test
+ public void testIsIp6() {
+ IpAddress ipAddress;
+
+ // IPv4
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertFalse(ipAddress.isIp6());
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertTrue(ipAddress.isIp6());
+ }
+
+ /**
* Tests getting the Ip4Address and Ip6Address view of the IP address.
*/
@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 9fc4eaa..31fbb03 100644
--- a/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
@@ -65,6 +65,38 @@
}
/**
+ * Tests whether the IP version of a prefix is IPv4.
+ */
+ @Test
+ public void testIsIp4() {
+ IpPrefix ipPrefix;
+
+ // IPv4
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.isIp4());
+
+ // IPv6
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertFalse(ipPrefix.isIp4());
+ }
+
+ /**
+ * Tests whether the IP version of a prefix is IPv6.
+ */
+ @Test
+ public void testIsIp6() {
+ IpPrefix ipPrefix;
+
+ // IPv4
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertFalse(ipPrefix.isIp6());
+
+ // IPv6
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertTrue(ipPrefix.isIp6());
+ }
+
+ /**
* Tests returning the IP address value and IP address prefix length of
* an IPv4 prefix.
*/