Bug 3577 - Message decoder fails to decode "all IP addresses match this update" 67/22067/2
authorLadislav Borak <lborak@cisco.com>
Fri, 5 Jun 2015 12:35:45 +0000 (14:35 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 9 Jun 2015 09:36:33 +0000 (09:36 +0000)
Added support to parse and serialize "default route" prefix in Ipv4Util and Ipv6Util.

Change-Id: Ibd84cad5864beb71112f72ac727ff7a65079efcf
Signed-off-by: Ladislav Borak <lborak@cisco.com>
(cherry picked from commit a277e11606404e307ca037f32e612813ce7a3a51)

util/src/main/java/org/opendaylight/protocol/util/Ipv4Util.java
util/src/main/java/org/opendaylight/protocol/util/Ipv6Util.java
util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java

index 79c4e8aa297de3c486fdb5b2195dd979edd2633f..3174ec7affbfb7067a900019ea2c88d579c0c99e 100644 (file)
@@ -118,11 +118,14 @@ public final class Ipv4Util {
      */
     public static byte[] bytesForPrefixBegin(final Ipv4Prefix prefix) {
         final String p = prefix.getValue();
+        final int length = getPrefixLength(p);
+        if (length == 0) {
+            return new byte[] { 0 };
+        }
         final int sep = p.indexOf('/');
         final InetAddress a = InetAddresses.forString(p.substring(0, sep));
         Preconditions.checkArgument(a instanceof Inet4Address);
         final byte[] bytes = a.getAddress();
-        final int length = getPrefixLength(p);
         return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , getPrefixLengthBytes(p)));
     }
 
@@ -169,9 +172,15 @@ public final class Ipv4Util {
         while (byteOffset < bytes.length) {
             final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
             byteOffset += 1;
+            // if length == 0, default route will be added
+            if (bitLength == 0) {
+                list.add(new Ipv4Prefix("0.0.0.0/0"));
+                continue;
+            }
             final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
             list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
             byteOffset += byteCount;
+
         }
         return list;
     }
index b1c1964738caf501976e15dfbd4809b29a4fc4fb..c43883bb8d78762c2abc004a49272183afebaff0 100644 (file)
@@ -104,11 +104,14 @@ public final class Ipv6Util {
      */
     public static byte[] bytesForPrefixBegin(final Ipv6Prefix prefix) {
         final String p = prefix.getValue();
+        final int length = Ipv4Util.getPrefixLength(p);
+        if (length == 0) {
+            return new byte[] { 0 };
+        }
         final int sep = p.indexOf('/');
         final InetAddress a = InetAddresses.forString(p.substring(0, sep));
         Preconditions.checkArgument(a instanceof Inet6Address);
         final byte[] bytes = a.getAddress();
-        final int length = Ipv4Util.getPrefixLength(p);
         return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , Ipv4Util.getPrefixLengthBytes(p)));
     }
 
@@ -156,9 +159,15 @@ public final class Ipv6Util {
         while (byteOffset < bytes.length) {
             final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
             byteOffset += 1;
+            // if length == 0, default route will be added
+            if (bitLength == 0) {
+                list.add(new Ipv6Prefix("::/0"));
+                continue;
+            }
             final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
             list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
             byteOffset += byteCount;
+
         }
         return list;
     }
index f2b32bf2b8a8a744c28260651b158b70e7402f3a..7a6cad8a73c8702a40dc1bc53e29c406b8c8127e 100644 (file)
@@ -77,6 +77,12 @@ public class IPAddressesAndPrefixesTest {
         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
 
         assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
+
+        bytes = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
+        assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0));
+
+        bytes = new byte[] { (byte) 0 };
+        assertArrayEquals(bytes, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("0.0.0.0/0")));
     }
 
     @Test
@@ -125,9 +131,13 @@ public class IPAddressesAndPrefixesTest {
 
     @Test
     public void testBytesForPrefix6Begin() {
-        final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
+        byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
         assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
         assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
+
+        bytes = new byte[] { (byte) 0 };
+        assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
+        assertArrayEquals(bytes, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("::/0")));
     }
 
     @Test