Add prefixForByteBuf method to util. 38/14638/2
authorDana Kutenicsova <dkutenic@cisco.com>
Fri, 30 Jan 2015 17:07:48 +0000 (18:07 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Sat, 31 Jan 2015 14:58:12 +0000 (15:58 +0100)
Change-Id: Iad68714b97074d71aa7051860e5d1f5af32da48a
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
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 3c64f474e6bede86df58e97567a51157d85b96a7..e63aa88ea2b4e77bd9e296091d6adb6befdc556a 100644 (file)
@@ -140,6 +140,20 @@ public final class Ipv4Util {
         return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
     }
 
+    /**
+     * Creates an Ipv4Prefix object from given ByteBuf. Prefix length is assumed to
+     * be in the left most byte of the buffer.
+     *
+     * @param bytes IPv4 address
+     * @return Ipv4Prefix object
+     */
+    public static Ipv4Prefix prefixForByteBuf(final ByteBuf bytes) {
+        final int prefixLength = bytes.readByte();
+        final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
+        Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: " + (bytes.readableBytes()));
+        return Ipv4Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+    }
+
     /**
      * Creates a list of Ipv4 Prefixes from given byte array.
      *
index 8a0ee29a4dc9458185165201c826306cc6e94f07..391402f687d9394988fc3db2e1951a3549589ca4 100644 (file)
@@ -126,6 +126,21 @@ public final class Ipv6Util {
         return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
     }
 
+    /**
+     * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
+     * be in the left most byte of the buffer.
+     *
+     * @param bytes IPv6 address
+     * @return Ipv6Prefix object
+     */
+    public static Ipv6Prefix prefixForByteBuf(final ByteBuf bytes) {
+        final int prefixLength = bytes.readByte();
+        final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
+        Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: " + (bytes.readableBytes()));
+        return Ipv6Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+    }
+
+
     /**
      * Creates a list of Ipv6 Prefixes from given byte array.
      *
index 22412240a42451697a0fb75be60805166a665073..f2b32bf2b8a8a744c28260651b158b70e7402f3a 100644 (file)
@@ -79,6 +79,13 @@ public class IPAddressesAndPrefixesTest {
         assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
     }
 
+    @Test
+    public void testPrefixForByteBuf() {
+        final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 0x0e, 123, 122, 0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
+        assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
+        assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
+    }
+
     @Test
     public void testAddressForByteBuf() {
         final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );