java_gen: Fixed bug in Ipv[46]AddressWithMask when mask == /0

Both IP address parsers treated the case mask == /0 incorrectly.
Fixed the behavior and added to the unit tests.
Clean up the IPv6 unit test.
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 3fcfe23..672a0e1 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
@@ -2,16 +2,20 @@
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
+import org.hamcrest.CoreMatchers;
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.junit.Test;
 import org.projectfloodlight.openflow.exceptions.OFParseError;
 
+import com.google.common.io.BaseEncoding;
+
 public class IPv6AddressTest {
 
     String[] testStrings = {
@@ -21,61 +25,83 @@
             "1:2:3:4:5:6:7:8"
     };
 
-    String[] ipsWithMask = {
-                            "1::1/80",
-                            "1:2:3:4::/ffff:ffff:ffff:ff00::",
-                            "ffff:ffee:1::/ff00:ff00:ff00:ff00::",
-                            "8:8:8:8:8:8:8:8",
-    };
 
-    byte[][] masks = {
-                    new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
-                                 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
-                                 (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
-                    new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
-                                 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
-                    new byte[] { (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
-                                 (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
-                    new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
-                                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }
-    };
+    private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
 
-    boolean[] hasMask = {
-                         true,
-                         true,
-                         true,
-                         false
+    private class WithMaskTaskCase {
+        final String input;
+        boolean hasMask;
+        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) {
+            super();
+            this.input = input;
+        }
+
+        public WithMaskTaskCase hasMask() {
+            this.hasMask = true;
+            return this;
+        }
+
+        public WithMaskTaskCase maskLength(int l) {
+            this.hasMask = l < 128;
+            for(int j=0; j < 8; j++) {
+                int pos = j * 8;
+                int index = Math.max(0, Math.min(7, pos - l));
+
+                if(index == 0) {
+                    expectedMask[j] = 0;
+                } else {
+                    expectedMask[j] = (byte) (-1 << index);
+                }
+            }
+            return this;
+        }
+
+        public WithMaskTaskCase maskHex(String string) {
+            string = string.replaceAll(" ", "");
+            this.hasMask = true;
+            expectedMask = hex.decode(string);
+            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"),
+
+            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"),
+            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")
     };
 
     @Test
     public void testMasked() throws UnknownHostException {
-        for(int i=0; i < ipsWithMask.length; i++ ) {
-            IPv6AddressWithMask value = IPv6AddressWithMask.of(ipsWithMask[i]);
-            if (!hasMask[i]) {
+        for(WithMaskTaskCase w: withMasks) {
+            IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
+            if (!w.hasMask) {
                 IPv6Address ip = value.getValue();
-                InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i]);
+                InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
 
                 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
-                assertEquals(ipsWithMask[i], ip.toString());
-            } else if (value instanceof IPv6AddressWithMask && hasMask[i]) {
-                InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/')));
+                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] &= masks[i][j];
+                    address[j] &= w.expectedMask[j];
                 }
 
-                assertArrayEquals(value.getValue().getBytes(), address);
-                assertArrayEquals(masks[i], value.getMask().getBytes());
+                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));
             }
         }
     }