IPAddress and IPAddressWithMask updates and fixes

* Provide asCidrPrefixMask to determine if an IPAddress represents a valid CIDR netmask
* cidr prefix length computation was broken:
  + 0 not detected as valid CIDR
  + BigInteger is signed. Grrr
* Add more unit test cases
* Test asCidrPrefixMask
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
index 7c50aed..ed8a11b 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
@@ -4,6 +4,29 @@
 
     public abstract IPVersion getIpVersion();
 
+    /**
+     * Checks if this IPAddress represents a valid CIDR style netmask, i.e.,
+     * it has a set of leading "1" bits followed by only "0" bits
+     * @return true if this represents a valid CIDR style netmask, false
+     * otherwise
+     */
+    public boolean isCidrMask() {
+        return asCidrMaskLength() != -1;
+    }
+
+    /**
+     * If this IPAddress represents a valid CIDR style netmask (see
+     * isCidrMask()) returns the length of the prefix (the number of "1" bits).
+     * @return length of CIDR mask or -1 if this is not a CIDR netmask
+     */
+    public abstract int asCidrMaskLength();
+
+    @Override
+    public abstract boolean equals(Object other);
+
+    @Override
+    public abstract int hashCode();
+
     public static IPAddress<?> of(String ip) {
         if (ip.indexOf('.') != -1)
             return IPv4Address.of(ip);
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 11ef103..d0632c5 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
@@ -18,4 +18,20 @@
             throw new IllegalArgumentException("IP Address not well formed: " + ip);
     }
 
+    public String toString() {
+        StringBuilder res = new StringBuilder();
+        res.append(value.toString());
+
+        res.append('/');
+        if (mask.asCidrMaskLength() != -1) {
+            // CIDR notation
+            res.append(mask.asCidrMaskLength());
+        } else {
+            // Full address mask
+            res.append(mask.toString());
+        }
+
+        return res.toString();
+    }
+
 }
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
index 51d10f3..970a65c 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
@@ -33,6 +33,21 @@
         return IPVersion.IPv4;
     }
 
+
+    @Override
+    public int asCidrMaskLength() {
+        int maskint = getInt();
+        if (maskint == 0)
+            return 0;
+        else if (Integer.bitCount((~maskint) + 1) == 1) {
+            // IP represents a true CIDR prefix length
+            return Integer.bitCount(maskint);
+        } else {
+            // IP is not a true prefix.
+            return -1;
+        }
+    }
+
     public static IPv4Address of(final byte[] address) {
         if (address.length != LENGTH) {
             throw new IllegalArgumentException(
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
index f30fcbb..1f23f2d 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
@@ -25,24 +25,6 @@
         return new IPv4AddressWithMask(value, mask);
     }
 
-    @Override
-    public String toString() {
-        StringBuilder res = new StringBuilder();
-        res.append(value.toString());
-
-        int maskint = mask.getInt();
-        res.append('/');
-        if (Integer.bitCount((~maskint) + 1) == 1) {
-            // CIDR notation
-            res.append(Integer.bitCount(maskint));
-        } else {
-            // Full address mask
-            res.append(mask.toString());
-        }
-
-        return res.toString();
-    }
-
     public static IPv4AddressWithMask of(final String string) {
         int slashPos;
         String ip = string;
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
index 1aad85b..531dadf 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
@@ -1,5 +1,6 @@
 package org.projectfloodlight.openflow.types;
 
+import java.math.BigInteger;
 import java.util.regex.Pattern;
 
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -36,6 +37,21 @@
         return IPVersion.IPv6;
     }
 
+
+    @Override
+    public int asCidrMaskLength() {
+        BigInteger maskBigint = new BigInteger(getBytes());
+        if (maskBigint.equals(BigInteger.ZERO))
+            return 0; // Thanks, signed BigInteger
+        else if (maskBigint.not().add(BigInteger.ONE).bitCount() == 1) {
+            // Need to get a positive BigInteger before we can count
+            return new BigInteger(1, getBytes()).bitCount();
+        } else {
+            // IP is not a true prefix.
+            return -1;
+        }
+    }
+
     public static IPv6Address of(final byte[] address) {
         if (address.length != LENGTH) {
             throw new IllegalArgumentException(
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
index 6faf0b8..8376e5e 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
@@ -19,23 +19,6 @@
         return new IPv6AddressWithMask(value, mask);
     }
 
-    @Override
-    public String toString() {
-        StringBuilder res = new StringBuilder();
-        res.append(value.toString());
-        res.append('/');
-
-        BigInteger maskint = new BigInteger(mask.getBytes());
-        if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
-            // CIDR notation
-            res.append(maskint.bitCount());
-        } else {
-            // Full address mask
-            res.append(mask.toString());
-        }
-
-        return res.toString();
-    }
 
     public static IPv6AddressWithMask of(final String string) {
         int slashPos;
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
index 38d60b3..d9c6681 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
@@ -38,6 +38,7 @@
             "1.2..3.4",
             "1.2.3.4.",
             "257.11.225.1",
+            "256.11.225.1",
             "-1.2.3.4",
             "1.2.3.4.5",
             "1.x.3.4",
@@ -49,7 +50,10 @@
                             "192.168.130.140/255.255.192.0",
                             "127.0.0.1/8",
                             "8.8.8.8",
-                            "0.0.0.0/0"
+                            "8.8.8.8/32",
+                            "0.0.0.0/0",
+                            "192.168.130.140/255.0.255.0",
+                            "1.2.3.4/0.127.0.255"
     };
 
     boolean[] hasMask = {
@@ -57,6 +61,9 @@
                          true,
                          true,
                          false,
+                         false,
+                         true,
+                         true,
                          true
     };
 
@@ -64,8 +71,32 @@
                              new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
                              new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
                              new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
-                             new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null },
-                             new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }
+                             new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
+                             new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
+                             new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
+                             new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
+                             new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
+    };
+
+    int[] ipsWithMaskLengths = {
+                                24,
+                                18,
+                                8,
+                                32,
+                                32,
+                                0,
+                                -1,
+                                -1
+    };
+
+    String[] invalidIpsWithMask = {
+                                   "asdf",
+                                   "1.2.3.4/33",
+                                   "1.2.3.4/34",
+                                   "1.2.3.4/-1",
+                                   "1.2.3.4/256.0.0.0",
+                                   "1.256.3.4/255.255.0.0",
+                                   "1.2.3.4/255.255.0.0.0",
     };
 
 
@@ -119,18 +150,47 @@
             if (!hasMask[i]) {
                 IPv4Address ip = value.getValue();
                 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
-            } else if (hasMask[i]) {
-                byte[] ipBytes = new byte[4];
-                System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
-                assertEquals(ipBytes.length, value.getValue().getBytes().length);
-                for (int j = 0; j < ipBytes.length; j++) {
-                    ipBytes[j] &= ipsWithMaskValues[i][1][j];
-                }
-
-                assertArrayEquals(ipBytes, value.getValue().getBytes());
-                assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
-                        value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
             }
+            IPv4Address mask = value.getMask();
+            assertEquals(ipsWithMaskLengths[i], value.getMask().asCidrMaskLength());
+            assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
+            byte[] ipBytes = new byte[4];
+            System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
+            assertEquals(ipBytes.length, value.getValue().getBytes().length);
+            for (int j = 0; j < ipBytes.length; j++) {
+                ipBytes[j] &= ipsWithMaskValues[i][1][j];
+            }
+
+            assertArrayEquals(ipBytes, value.getValue().getBytes());
+            assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
+                    value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
+        }
+    }
+
+    @Test
+    public void testOfMaskedInvalid() throws Exception {
+        for(String invalid : invalidIpsWithMask) {
+            try {
+                IPv4Address.of(invalid);
+                fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
+            } catch(IllegalArgumentException e) {
+                // ok
+            }
+        }
+    }
+
+    @Test
+    public void testSuperclass() throws Exception {
+        for(String ipString: testStrings) {
+            IPAddress<?> superIp = IPAddress.of(ipString);
+            assertEquals(IPVersion.IPv4, superIp.getIpVersion());
+            assertEquals(IPv4Address.of(ipString), superIp);
+        }
+
+        for(String ipMaskedString: ipsWithMask) {
+            IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
+            assertEquals(IPVersion.IPv4, superIp.getIpVersion());
+            assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
         }
     }
 }
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
index 6eb5743..1243c61 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
@@ -31,6 +31,7 @@
     private class WithMaskTaskCase {
         final String input;
         boolean hasMask;
+        int expectedMaskLength = 128;
         byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
 
         public WithMaskTaskCase(String input) {
@@ -45,19 +46,27 @@
             return this;
         }
 
+        public WithMaskTaskCase expectedMaskLength(int expectedLength) {
+            this.expectedMaskLength = expectedLength;
+            return this;
+        }
+
     }
 
     WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
             new WithMaskTaskCase("1::1/80")
-                .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00"),
+                .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
+                .expectedMaskLength(80),
 
             new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
-                .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00"),
+                .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
+                .expectedMaskLength(-1),
             new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
             new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
             new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
             new WithMaskTaskCase("::/0")
                 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
+                .expectedMaskLength(0),
     };
 
     @Test
@@ -70,19 +79,25 @@
 
                 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
                 assertEquals(w.input.split("/")[0], ip.toString());
-            } else {
-                InetAddress inetAddress = InetAddress.getByName(w.input.substring(0, w.input.indexOf('/')));
-
-                byte[] address = inetAddress.getAddress();
-                assertEquals(address.length, value.getValue().getBytes().length);
-
-                for (int j = 0; j < address.length; j++) {
-                    address[j] &= w.expectedMask[j];
-                }
-
-                assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
-                assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
             }
+            InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
+
+            assertEquals("Input " + w.input, w.expectedMaskLength, value.getMask().asCidrMaskLength());
+
+            byte[] address = inetAddress.getAddress();
+            assertEquals(address.length, value.getValue().getBytes().length);
+
+            for (int j = 0; j < address.length; j++) {
+                address[j] &= w.expectedMask[j];
+            }
+
+            assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
+            assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
+        }
+        for (int i = 0; i <= 128; i++) {
+            String ipString = String.format("8001:2::1/%d", i);
+            IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
+            assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
         }
     }
 
@@ -150,4 +165,20 @@
         assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
         assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false));
     }
+
+    @Test
+    public void testSuperclass() throws Exception {
+        for(String ipString: testStrings) {
+            IPAddress<?> superIp = IPAddress.of(ipString);
+            assertEquals(IPVersion.IPv6, superIp.getIpVersion());
+            assertEquals(IPv6Address.of(ipString), superIp);
+        }
+
+        for(WithMaskTaskCase w: withMasks) {
+            String ipMaskedString = w.input;
+            IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
+            assertEquals(IPVersion.IPv6, superIp.getIpVersion());
+            assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
+        }
+    }
 }