Merge "Bug 611 - NlriReg supports serialization"
authorDana Kutenicsova <dkutenic@cisco.com>
Sun, 22 Jun 2014 20:51:31 +0000 (20:51 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Sun, 22 Jun 2014 20:51:31 +0000 (20:51 +0000)
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/LabelRegistry.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/LabelSerializer.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/LabelUtil.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleLabelRegistry.java
pcep/spi/src/test/java/org/opendaylight/protocol/pcep/spi/UtilsTest.java

index 2ea5cd67e7532d633c84a0cc4f77bfe0ba2cc794..60cd6ad1b0166a8a2f7dcf33ee6cb1c0da1db064 100644 (file)
@@ -75,12 +75,8 @@ public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobject
     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase, "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
-        // FIXME: switch to ByteBuf
-        final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
-        if (labelbytes == null) {
-            throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
-                    + label.getLabelType().getImplementedInterface());
-        }
-        EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), Unpooled.copiedBuffer(labelbytes), buffer);
+        final ByteBuf body = Unpooled.buffer();
+        this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
+        EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
     }
 }
index ae22313e8a838f3bde8934c48fdef6969cc718e1..79680e8f5bb4dbc9be15c01cd7abd9dfa5bf9bd9 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
@@ -36,12 +37,9 @@ public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
     }
 
     @Override
-    public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
-        if (!(subobject instanceof GeneralizedLabelCase)) {
-            throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
-                    + ". Needed GeneralizedLabelCase.");
-        }
-        return LabelUtil.formatLabel(CTYPE, unidirectional, global,
-                ((GeneralizedLabelCase) subobject).getGeneralizedLabel().getGeneralizedLabel());
+    public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject instanceof GeneralizedLabelCase, "Unknown Label Subobject instance. Passed {}. Needed GeneralizedLabelCase.", subobject.getClass());
+        final ByteBuf body = Unpooled.wrappedBuffer(((GeneralizedLabelCase) subobject).getGeneralizedLabel().getGeneralizedLabel());
+        LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
     }
 }
index 108abcf631100f00bf1da6dea8c4f2fc3539dc38..83ad46f3593e3b60e6f7e037ead21d1affaa7323 100644 (file)
@@ -78,12 +78,8 @@ public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobject
     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
         Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
-        final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
-        if (labelbytes == null) {
-            throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
-                    + label.getLabelType().getImplementedInterface());
-        }
-        //FIXME: switch to ByteBuf
-        RROSubobjectUtil.formatSubobject(TYPE, Unpooled.copiedBuffer(labelbytes), buffer);
+        final ByteBuf body = Unpooled.buffer();
+        this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
+        RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
     }
 }
index ed5f7245f0509c1bf5eeb1d7f565816d892ae7af..7ced7b3406a9cd649373989bd04b11ffc9760fae 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 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.Type1LabelCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCaseBuilder;
@@ -41,12 +41,9 @@ public class Type1LabelParser implements LabelParser, LabelSerializer {
     }
 
     @Override
-    public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
-        if (!(subobject instanceof Type1LabelCase)) {
-            throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
-                    + ". Needed Type1LabelCase.");
-        }
-        return LabelUtil.formatLabel(CTYPE, unidirectional, global, ByteArray.longToBytes(
-                ((Type1LabelCase) subobject).getType1Label().getType1Label().longValue(), LABEL_LENGTH));
+    public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject instanceof Type1LabelCase, "Unknown Label Subobject instance. Passed {}. Needed Type1LabelCase.", subobject.getClass());
+        LabelUtil.formatLabel(CTYPE, unidirectional, global, Unpooled.copyInt(
+                ((Type1LabelCase) subobject).getType1Label().getType1Label().intValue()), buffer);
     }
 }
index 5948f8cc5cc2d1791b1099ba207036d035783a44..c12815efd394a9a35a33433a68b2af6646945e16 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 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.WavebandSwitchingLabelCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
@@ -50,17 +50,13 @@ public class WavebandSwitchingLabelParser implements LabelParser, LabelSerialize
     }
 
     @Override
-    public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
-        if (!(subobject instanceof WavebandSwitchingLabelCase)) {
-            throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
-                    + ". Needed WavebandSwitchingLabelCase.");
-        }
-        final byte[] retBytes = new byte[CONTENT_LENGTH];
+    public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject instanceof WavebandSwitchingLabelCase, "Unknown Label Subobject instance. Passed {}. Needed WavebandSwitchingLabelCase.", subobject.getClass());
         final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
-        System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue(), WAVEB_F_LENGTH), 0, retBytes, 0, WAVEB_F_LENGTH);
-        System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue(), START_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
-        System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue(), END_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH,
-                END_F_LENGTH);
-        return LabelUtil.formatLabel(CTYPE, unidirectional, global, retBytes);
+        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        body.writeInt(obj.getWavebandId().intValue());
+        body.writeInt(obj.getStartLabel().intValue());
+        body.writeInt(obj.getEndLabel().intValue());
+        LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
     }
 }
index 2784999878e1567506dcb8b8ebb23b2fec204293..16227d75c25cfd6e6d3d61070faf5a4995755ad2 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.ByteBuf;
 import io.netty.buffer.Unpooled;
 
 import org.junit.Test;
@@ -40,7 +41,9 @@ public class LabelSubobjectParserTest {
         iBuilder.setGeneralizedLabel(ByteArray.cutBytes(generalizedLabelBytes, 2));
         final GeneralizedLabelCaseBuilder builder = new GeneralizedLabelCaseBuilder().setGeneralizedLabel(iBuilder.build());
         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(generalizedLabelBytes, 2))));
-        assertArrayEquals(generalizedLabelBytes, parser.serializeLabel(true, false, builder.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeLabel(true, false, builder.build(), buff);
+        assertArrayEquals(generalizedLabelBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -52,7 +55,9 @@ public class LabelSubobjectParserTest {
         iBuilder.setEndLabel(0x1111L);
         final WavebandSwitchingLabelCaseBuilder builder = new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(iBuilder.build());
         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(wavebandLabelBytes, 2))));
-        assertArrayEquals(wavebandLabelBytes, parser.serializeLabel(false, true, builder.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeLabel(false, true, builder.build(), buff);
+        assertArrayEquals(wavebandLabelBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -62,6 +67,8 @@ public class LabelSubobjectParserTest {
         iBuilder.setType1Label(0x120025ffL);
         final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder().setType1Label(iBuilder.build());
         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(typeOneLabelBytes, 2))));
-        assertArrayEquals(typeOneLabelBytes, parser.serializeLabel(true, true, builder.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeLabel(true, true,  builder.build(), buff);
+        assertArrayEquals(typeOneLabelBytes, ByteArray.getAllBytes(buff));
     }
 }
index 8804b4bc635687c62f9568d5b99ca3c71848d267..8e57ae7cc5457174aeb200d6fec0f6ec2cca4817 100644 (file)
@@ -25,10 +25,10 @@ public interface LabelRegistry {
     /**
      * Find serializer for given label. Delegates parsing to found serializer.
      *
-     * @param label to be parsed
      * @param unidirectional label common header flag
      * @param global label commom header flag
-     * @return null if the serializer for this label could not be found
+     * @param label to be parsed
+     * @param buffer buffer where the serialized label will be parsed
      */
-    byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType label);
+    void serializeLabel(final boolean unidirectional, final boolean global, final LabelType label, final ByteBuf buffer);
 }
index eaa8cf5dd028af2e0a10ec8072ce67c1efdd60e4..2e9f72391f3b6fa0c2f96e9bb864e309955457f4 100644 (file)
@@ -7,9 +7,11 @@
  */
 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 LabelSerializer {
 
-    byte[] serializeLabel(boolean unidirectional, boolean global, LabelType subobject);
+    void serializeLabel(boolean unidirectional, boolean global, final LabelType subobject, final ByteBuf buffer);
 }
index 0c3a7378053586711697157232d1215d72c6ad55..96adc9fbe5edcc87566d264cd69d6d293e30b9e3 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
-import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
 
 import java.util.BitSet;
 
@@ -17,14 +17,6 @@ public final class LabelUtil {
 
     private static final int RES_F_LENGTH = 1;
 
-    private static final int C_TYPE_F_LENGTH = 1;
-
-    private static final int RES_F_OFFSET = 0;
-
-    private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
-
-    private static final int HEADER_OFFSET = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
-
     private static final int U_FLAG_OFFSET = 0;
 
     private static final int G_FLAG_OFFSET = 7;
@@ -33,17 +25,12 @@ public final class LabelUtil {
         throw new UnsupportedOperationException("Utility class should not be instantiated");
     }
 
-    public static byte[] formatLabel(final int type, final boolean unidirectional, final boolean global, final byte[] labelbytes) {
-
-        final byte[] retBytes = new byte[labelbytes.length + HEADER_OFFSET];
-
-        System.arraycopy(labelbytes, 0, retBytes, HEADER_OFFSET, labelbytes.length);
-
-        final BitSet reserved = new BitSet();
+    public static void formatLabel(final int type, final boolean unidirectional, final boolean global, final ByteBuf body, final ByteBuf buffer) {
+        final BitSet reserved = new BitSet(RES_F_LENGTH * Byte.SIZE);
         reserved.set(U_FLAG_OFFSET, unidirectional);
         reserved.set(G_FLAG_OFFSET, global);
-        System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
-        retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(type);
-        return retBytes;
+        buffer.writeBytes(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH));
+        buffer.writeByte(type);
+        buffer.writeBytes(body);
     }
 }
index d2ddbe517bf71cb563ffd30cbebd3f25a3c0c1d0..f45259d405299fdfafb358ba35fb385c5c7a8ff2 100644 (file)
@@ -43,11 +43,10 @@ public class SimpleLabelRegistry implements LabelRegistry {
     }
 
     @Override
-    public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType label) {
+    public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType label, final ByteBuf buffer) {
         final LabelSerializer serializer = this.handlers.getSerializer(label.getImplementedInterface());
-        if (serializer == null) {
-            return null;
+        if (serializer != null) {
+            serializer.serializeLabel(unidirectional, global, label, buffer);
         }
-        return serializer.serializeLabel(unidirectional, global, label);
     }
 }
index 00cf6a1a9bc06eb650541c57f7e4f5daab1df0a2..2224469ce80f243c6fb50ef971ad9cb507729648 100644 (file)
@@ -19,7 +19,10 @@ public class UtilsTest {
     @Test
     public void testLabelUtil() {
         byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
-        assertArrayEquals(expected, LabelUtil.formatLabel(4, true, true, new byte[] { 1, 2, 3, 4 }));
+        ByteBuf out = Unpooled.buffer();
+        ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
+        LabelUtil.formatLabel(4, true, true, body, out);
+        assertArrayEquals(expected, ByteArray.getAllBytes(out));
     }
 
     @Test