BUG-612 : switched Labels to ByteBuf. 75/7575/1
authorDana Kutenicsova <dkutenic@cisco.com>
Sun, 1 Jun 2014 22:01:43 +0000 (00:01 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Sun, 1 Jun 2014 22:01:43 +0000 (00:01 +0200)
Change-Id: Ib3ff3cc49022af0193c55d7d0fef6f872ac4f250
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROLabelSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/GeneralizedLabelParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROLabelSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/Type1LabelParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/WavebandSwitchingLabelParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/LabelSubobjectParserTest.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/LabelParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/LabelRegistry.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleLabelRegistry.java

index a817874bf220fa7c0dbb0dba48b2af6a5e2ad37a..274492fda833d7ec57a25b9eb11de92be7ad55cf 100644 (file)
@@ -60,8 +60,7 @@ public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobject
                final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
                final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
 
-               //FIXME: switch to ByteBuf
-               final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer));
+               final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
                if (labelType == null) {
                        throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
                }
index bb9ccde79db9050ec3fb1ede4e091b01b53b998f..7855e7c1ca5f4de9c302625c4f7f8f8962b32b49 100644 (file)
@@ -7,15 +7,20 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
 import org.opendaylight.protocol.pcep.spi.LabelUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
 
+import com.google.common.base.Preconditions;
+
 /**
  * Parser for {@link GeneralizedLabelCase}
  */
@@ -24,12 +29,9 @@ public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
        public static final int CTYPE = 2;
 
        @Override
-       public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
-               if (buffer == null || buffer.length == 0) {
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               }
-
-               return new GeneralizedLabelCaseBuilder().setGeneralizedLabel(new GeneralizedLabelBuilder().setGeneralizedLabel(buffer).build()).build();
+       public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
+               Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+               return new GeneralizedLabelCaseBuilder().setGeneralizedLabel(new GeneralizedLabelBuilder().setGeneralizedLabel(ByteArray.readAllBytes(buffer)).build()).build();
        }
 
        @Override
index 82fd6159beb158e8dcc46a35f548d64e9127f1e0..074f3d5c5395053b5efde7b0e91df65d06382588 100644 (file)
@@ -62,8 +62,7 @@ public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobject
 
                final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
 
-               //FIXME: switch to ByteBuf
-               final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer));
+               final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
                if (labelType == null) {
                        throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
                }
index 223f9f03d39f99ffffe120b99f066fac35d14d9d..a0d8f6d3874471caac497ed54d82976fbfb9d095 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
 import org.opendaylight.protocol.pcep.spi.LabelUtil;
@@ -17,7 +19,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
 
-import com.google.common.primitives.UnsignedInts;
+import com.google.common.base.Preconditions;
 
 /**
  * Parser for {@link Type1LabelCase}
@@ -29,16 +31,14 @@ public class Type1LabelParser implements LabelParser, LabelSerializer {
        public static final int LABEL_LENGTH = 4;
 
        @Override
-       public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
-               if (buffer == null || buffer.length == 0) {
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               }
-               if (buffer.length != LABEL_LENGTH) {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: " + LABEL_LENGTH
+       public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
+               Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+               if (buffer.readableBytes() != LABEL_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + LABEL_LENGTH
                                        + ".");
                }
                return new Type1LabelCaseBuilder().setType1Label(
-                               new Type1LabelBuilder().setType1Label(UnsignedInts.toLong(ByteArray.bytesToInt(buffer))).build()).build();
+                               new Type1LabelBuilder().setType1Label(buffer.readUnsignedInt()).build()).build();
        }
 
        @Override
index 28cb97e51685bc6cada5ef2e8026f147c1ebac10..4fd6438c1ce834ef1f8090bfdac6ca913e853faa 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
 import org.opendaylight.protocol.pcep.spi.LabelUtil;
@@ -18,6 +20,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
 
+import com.google.common.base.Preconditions;
+
 /**
  * Parser for {@link WavebandSwitchingLabelCase}
  */
@@ -32,21 +36,16 @@ public class WavebandSwitchingLabelParser implements LabelParser, LabelSerialize
        private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
 
        @Override
-       public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
-               if (buffer == null || buffer.length == 0) {
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               }
-               if (buffer.length != CONTENT_LENGTH) {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
+       public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
+               Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+               if (buffer.readableBytes() != CONTENT_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
                                        + CONTENT_LENGTH + ".");
                }
                final WavebandSwitchingLabelBuilder builder = new WavebandSwitchingLabelBuilder();
-               int byteOffset = 0;
-               builder.setWavebandId(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, WAVEB_F_LENGTH)));
-               byteOffset += WAVEB_F_LENGTH;
-               builder.setStartLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, START_F_LENGTH)));
-               byteOffset += START_F_LENGTH;
-               builder.setEndLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, END_F_LENGTH)));
+               builder.setWavebandId(buffer.readUnsignedInt());
+               builder.setStartLabel(buffer.readUnsignedInt());
+               builder.setEndLabel(buffer.readUnsignedInt());
                return new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(builder.build()).build();
        }
 
index e0d161b0a6aed257d3496acb357b3105240ad72a..3823e6079817b82298a7b61eb25ae34e64c5e2fd 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.impl;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import io.netty.buffer.Unpooled;
 
 import org.junit.Test;
 import org.opendaylight.protocol.pcep.impl.subobject.GeneralizedLabelParser;
@@ -37,7 +38,7 @@ public class LabelSubobjectParserTest {
                final GeneralizedLabelBuilder iBuilder = new GeneralizedLabelBuilder();
                iBuilder.setGeneralizedLabel(ByteArray.cutBytes(generalizedLabelBytes, 2));
                final GeneralizedLabelCaseBuilder builder = new GeneralizedLabelCaseBuilder().setGeneralizedLabel(iBuilder.build());
-               assertEquals(builder.build(), parser.parseLabel(ByteArray.cutBytes(generalizedLabelBytes, 2)));
+               assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(generalizedLabelBytes, 2))));
                assertArrayEquals(generalizedLabelBytes, parser.serializeLabel(true, false, builder.build()));
        }
 
@@ -49,7 +50,7 @@ public class LabelSubobjectParserTest {
                iBuilder.setStartLabel(0x9999L);
                iBuilder.setEndLabel(0x1111L);
                final WavebandSwitchingLabelCaseBuilder builder = new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(iBuilder.build());
-               assertEquals(builder.build(), parser.parseLabel(ByteArray.cutBytes(wavebandLabelBytes, 2)));
+               assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(wavebandLabelBytes, 2))));
                assertArrayEquals(wavebandLabelBytes, parser.serializeLabel(false, true, builder.build()));
        }
 
@@ -59,7 +60,7 @@ public class LabelSubobjectParserTest {
                final Type1LabelBuilder iBuilder = new Type1LabelBuilder();
                iBuilder.setType1Label(0x120025ffL);
                final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder().setType1Label(iBuilder.build());
-               assertEquals(builder.build(), parser.parseLabel(ByteArray.cutBytes(typeOneLabelBytes, 2)));
+               assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(typeOneLabelBytes, 2))));
                assertArrayEquals(typeOneLabelBytes, parser.serializeLabel(true, true, builder.build()));
        }
 }
index 0cae78c1ff8ab5de3060898d534c94c95ddb78e5..79b22c1eb9fe8fefc5ccb60f8e1e03f83792a29f 100644 (file)
@@ -7,8 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
 
 public interface LabelParser {
-       LabelType parseLabel(byte[] buffer) throws PCEPDeserializerException;
+       LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException;
 }
index 04b912dfc74aaaf0b83a1fccdf9073ae863c7f5f..e94cf5caf4761b8404444dde79f9df3124156b7a 100644 (file)
@@ -7,17 +7,19 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
 
 public interface LabelRegistry {
        /**
         * Finds parser for given label C-type in the registry. Delegates parsing to found parser.
         * @param cType label type, key in parser registry
-        * @param buffer label raw binary value to be parsed
+        * @param buffer label wrapped in ByteBuf
         * @return null if the parser for this label could not be found
         * @throws PCEPDeserializerException if the parsing did not succeed
         */
-       LabelType parseLabel(final int cType, final byte[] buffer) throws PCEPDeserializerException;
+       LabelType parseLabel(final int cType, final ByteBuf buffer) throws PCEPDeserializerException;
 
        /**
         * Find serializer for given label. Delegates parsing to found serializer.
index ba8880cc8479fc106031c43cfff1122353ab5d21..a4cef53c988f59f32bbe42531eb6b5c04a71c0a3 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.concepts.HandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
@@ -31,7 +33,7 @@ public class SimpleLabelRegistry implements LabelRegistry {
        }
 
        @Override
-       public LabelType parseLabel(int cType, byte[] buffer) throws PCEPDeserializerException {
+       public LabelType parseLabel(final int cType, final ByteBuf buffer) throws PCEPDeserializerException {
                Preconditions.checkArgument(cType >= 0 && cType <= Values.UNSIGNED_BYTE_MAX_VALUE);
                final LabelParser parser = this.handlers.getParser(cType);
                if (parser == null) {