IPAddressWithMask should have a contains method taht can take any IPAddress
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
index ba7eb93..aaaac3b 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
@@ -1,5 +1,7 @@
package org.projectfloodlight.openflow.types;
+import com.google.common.base.Preconditions;
+
public abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> {
@@ -22,9 +24,8 @@
}
public static IPAddressWithMask<?> of(String ip) {
- if (ip == null) {
- throw new NullPointerException("String ip must not be null");
- }
+ Preconditions.checkNotNull(ip, "string ip must not be null");
+
if (ip.indexOf('.') != -1)
return IPv4AddressWithMask.of(ip);
else if (ip.indexOf(':') != -1)
@@ -50,4 +51,22 @@
return res.toString();
}
+ public boolean contains(IPAddress<?> ip) {
+ Preconditions.checkNotNull(ip, "ip must not be null");
+
+ // Ensure mask and IP of the same version
+ if(getIpVersion() == ip.getIpVersion()) {
+ if(getIpVersion() == IPVersion.IPv4) {
+ IPv4AddressWithMask ipv4mask = (IPv4AddressWithMask) this;
+ IPv4Address ipv4 = (IPv4Address) ip;
+ return ipv4mask.matches(ipv4);
+ } else {
+ IPv6AddressWithMask ipv6mask = (IPv6AddressWithMask) this;
+ IPv6Address ipv6 = (IPv6Address) ip;
+ return ipv6mask.matches(ipv6);
+ }
+ }
+ return false;
+ }
+
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
index 865df75..bdb26c0 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
@@ -1,6 +1,8 @@
package org.projectfloodlight.openflow.types;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.net.UnknownHostException;
@@ -64,4 +66,32 @@
}
}
+ @Test
+ public void testContains() {
+
+ // Test IPv4 Mask
+ IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24");
+
+ IPAddress<?> validIp = IPAddress.of("1.2.3.5");
+ assertTrue(mask.contains(validIp));
+
+ IPAddress<?> invalidIp = IPAddress.of("1.2.5.5");
+ assertFalse(mask.contains(invalidIp));
+
+ IPAddress<?> invalidIpv6 = IPAddress.of("10:10::ffff");
+ assertFalse(mask.contains(invalidIpv6));
+
+ // Test IPv6 Mask
+ mask = IPAddressWithMask.of("10:10::1/112");
+
+ validIp = IPAddress.of("10:10::f");
+ assertTrue(mask.contains(validIp));
+
+ invalidIp = IPAddress.of("11:10::f");
+ assertFalse(mask.contains(invalidIp));
+
+ IPAddress<?> invalidIpv4 = IPAddress.of("10.0.0.1");
+ assertFalse(mask.contains(invalidIpv4));
+ }
+
}