BUG-612 : switched PCEP tlv serializers to ByteBuf 42/8042/4
authorDana Kutenicsova <dkutenic@cisco.com>
Mon, 16 Jun 2014 14:27:43 +0000 (16:27 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 22 Jun 2014 11:59:18 +0000 (11:59 +0000)
Change-Id: I836c1a0ae71987d5ae9fe0dcbc8d84c540e43af0
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
52 files changed:
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/crabbe/initiated00/LSPCleanupTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/crabbe/initiated00/PCEPOpenObjectParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/crabbe/initiated00/PCEStatefulCapabilityTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02LspDbVersionTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02LspObjectParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02LspSymbolicNameTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02LspaObjectParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02NodeIdentifierTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02OpenObjectParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02RSVPErrorSpecTlvParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02StatefulCapabilityTlvParser.java
pcep/ietf-stateful02/src/test/java/org/opendaylight/protocol/pcep/ietf/PCEPTlvParserTest.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/initiated00/CInitiated00LspObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/initiated00/CInitiated00SrpObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/initiated00/CInitiated00StatefulCapabilityTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspSymbolicNameTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspaObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07OpenObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07StatefulCapabilityTlvParser.java
pcep/ietf-stateful07/src/test/java/org/opendaylight/protocol/pcep/ietf/PCEPTlvParserTest.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPErrorObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPNoPathObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPNotificationObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPOpenObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPRequestParameterObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/AbstractVendorSpecificTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/NoPathVectorTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/OFListTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/OrderTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/OverloadedDurationTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/ReqMissingTlvParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/TlvUtil.java [deleted file]
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPTlvParserTest.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/CInitiated00SrpObjectWithPstTlvParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PathSetupTypeTlvParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepRpObjectWithPstTlvParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/PcepOpenObjectWithSpcTlvParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrPceCapabilityTlvParser.java
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepTlvParserTest.java
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepTlvParserTest.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/TlvRegistry.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/TlvSerializer.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/TlvUtil.java [new file with mode: 0644]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleTlvRegistry.java

index a28f5ed87762251a0c2626748706464937765918..502d359bfd9256edcd1b230a1f277aa3127698e5 100644 (file)
@@ -7,13 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.crabbe.initiated00;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanup;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanupBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -23,8 +25,9 @@ public final class LSPCleanupTlvParser implements TlvParser, TlvSerializer {
     public static final int TYPE = 26;
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        return TlvUtil.formatTlv(TYPE, ByteArray.intToBytes(((LspCleanup) tlv).getTimeout().intValue()));
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "LSPCleanupTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((LspCleanup) tlv).getTimeout().intValue()), buffer);
     }
 
     @Override
index 0e739458ef4aa3ab91af302a98300a34db20fb15..1a35711fccc325e0eb27c2318818e393ce0bdcd3 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.crabbe.initiated00;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02OpenObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanup;
@@ -56,27 +57,16 @@ public final class PCEPOpenObjectParser extends Stateful02OpenObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] cleanupBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs1.class) != null) {
             final Tlvs1 cleanupTlv = tlvs.getAugmentation(Tlvs1.class);
             if (cleanupTlv.getLspCleanup() != null) {
-                cleanupBytes = serializeTlv(cleanupTlv.getLspCleanup());
-                finalLength += cleanupBytes.length;
+                serializeTlv(cleanupTlv.getLspCleanup(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (cleanupBytes != null) {
-            ByteArray.copyWhole(cleanupBytes, result, offset);
-            offset += cleanupBytes.length;
-        }
-        return result;
     }
 }
index 98cfed7fa3690eda8e0ab14588f672bd55a0a293..93014717225ec005d475840b4f73ace24d50b4ae 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.protocol.pcep.crabbe.initiated00;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02StatefulCapabilityTlvParser;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1Builder;
@@ -51,24 +54,22 @@ public final class PCEStatefulCapabilityTlvParser extends Stateful02StatefulCapa
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
         final Stateful sct = (Stateful) tlv;
-
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
         final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
         if (sfi != null) {
             flags.set(I_FLAG_OFFSET, sfi.isInitiation());
         }
-        if (sct.isLspUpdateCapability() != null && sct.isLspUpdateCapability()) {
+        if (sct.isLspUpdateCapability() != null) {
             flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
         }
-        if (sct.isIncludeDbVersion() != null && sct.isIncludeDbVersion()) {
+        if (sct.isIncludeDbVersion() != null) {
             flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
         }
-        return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
-}
\ No newline at end of file
+}
index b977627b542c2b27442d9576b7cedd2e6fc7c128..1505877142da78753194e75a358017b24f3c0fab 100644 (file)
@@ -10,14 +10,14 @@ package org.opendaylight.protocol.pcep.ietf.stateful02;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.math.BigInteger;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -29,8 +29,6 @@ public final class Stateful02LspDbVersionTlvParser implements TlvParser, TlvSeri
 
     public static final int TYPE = 23;
 
-    private static final int DBV_F_LENGTH = 8;
-
     @Override
     public LspDbVersion parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -40,9 +38,8 @@ public final class Stateful02LspDbVersionTlvParser implements TlvParser, TlvSeri
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkNotNull(tlv, "LspDbVersionTlv is mandatory.");
-        final LspDbVersion lsp = (LspDbVersion) tlv;
-        return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(lsp.getVersion().longValue(), DBV_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, Unpooled.copyLong(((LspDbVersion) tlv).getVersion().longValue()), buffer);
     }
 }
index c4319f76d76d0e36b68cb9045c0459cc7a26d7ec..f8d9f13be446e5a97a7b069e174dd4bded2f8e86 100644 (file)
@@ -105,46 +105,22 @@ public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
             flags.set(OPERATIONAL_FLAG_OFFSET);
         }
         body.writeByte(ByteArray.bitSetToBytes(flags, 2)[1]);
-        //FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        body.writeBytes(tlvs);
+        serializeTlvs(specObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        int finalLength = 0;
-        byte[] rsvpErrBytes = null;
-        byte[] symbBytes = null;
-        byte[] dbvBytes = null;
         if (tlvs.getRsvpErrorSpec() != null) {
-            rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
-            finalLength += rsvpErrBytes.length;
+            serializeTlv(tlvs.getRsvpErrorSpec(), body);
         }
         if (tlvs.getSymbolicPathName() != null) {
-            symbBytes = serializeTlv(tlvs.getSymbolicPathName());
-            finalLength += symbBytes.length;
+            serializeTlv(tlvs.getSymbolicPathName(), body);
         }
         if (tlvs.getLspDbVersion() != null) {
-            dbvBytes = serializeTlv(tlvs.getLspDbVersion());
-            finalLength += dbvBytes.length;
+            serializeTlv(tlvs.getLspDbVersion(), body);
         }
-        int offset = 0;
-        final byte[] result = new byte[finalLength];
-        if (rsvpErrBytes != null) {
-            ByteArray.copyWhole(rsvpErrBytes, result, offset);
-            offset += rsvpErrBytes.length;
-        }
-        if (symbBytes != null) {
-            ByteArray.copyWhole(symbBytes, result, offset);
-            offset += symbBytes.length;
-        }
-        if (dbvBytes != null) {
-            ByteArray.copyWhole(dbvBytes, result, offset);
-            offset += dbvBytes.length;
-        }
-        return result;
     }
 }
index 0515783b90b397b66099a94851506f16c9048b29..ad79c06b9c37dba9c68482fc2f8b9393bcc7d08a 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
@@ -35,11 +38,8 @@ public final class Stateful02LspSymbolicNameTlvParser implements TlvParser, TlvS
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("SymbolicPathNameTlv is mandatory.");
-        }
-        final SymbolicPathName spn = (SymbolicPathName) tlv;
-        return TlvUtil.formatTlv(TYPE, spn.getPathName().getValue());
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "SymbolicPathNameTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((SymbolicPathName) tlv).getPathName().getValue()), buffer);
     }
 }
index 3244f90f1e4338265347c22d05a36b940595f0f2..723c4753c24293ffc249734a5c22decd5fa9307e 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
@@ -43,27 +44,16 @@ public class Stateful02LspaObjectParser extends PCEPLspaObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] nameBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs2.class) != null) {
             final Tlvs2 nameTlvs = tlvs.getAugmentation(Tlvs2.class);
             if (nameTlvs.getSymbolicPathName() != null) {
-                nameBytes = serializeTlv(nameTlvs.getSymbolicPathName());
-                finalLength += nameBytes.length;
+                serializeTlv(nameTlvs.getSymbolicPathName(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (nameBytes != null) {
-            ByteArray.copyWhole(nameBytes, result, offset);
-            offset += nameBytes.length;
-        }
-        return result;
     }
 }
index 92e748c3680de491ca4a27baa786a80e5a8f6d76..7aad3d21d42e2049350aa277dc96ecda2889f845 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.NodeIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifierBuilder;
@@ -23,10 +26,9 @@ public final class Stateful02NodeIdentifierTlvParser implements TlvParser, TlvSe
     public static final int TYPE = 24;
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        return TlvUtil.formatTlv(
-                TYPE,
-                ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier) tlv).getNodeId().getValue());
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "NodeIdentifierTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier) tlv).getNodeId().getValue()), buffer);
     }
 
     @Override
index 31b955be1516136f961da180ff6daf21ca9c7a2e..7ccf95c89a582fb8d09025ad71667a4aa5f9370f 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier;
@@ -49,37 +50,19 @@ public class Stateful02OpenObjectParser extends PCEPOpenObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] statefulBytes = null;
-        byte[] nodeIdBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs2.class) != null) {
             final Tlvs2 statefulTlvs = tlvs.getAugmentation(Tlvs2.class);
             if (statefulTlvs.getStateful() != null) {
-                statefulBytes = serializeTlv(statefulTlvs.getStateful());
-                finalLength += statefulBytes.length;
+                serializeTlv(statefulTlvs.getStateful(), body);
             }
             if (statefulTlvs.getNodeIdentifier() != null) {
-                nodeIdBytes = serializeTlv(statefulTlvs.getNodeIdentifier());
-                finalLength += nodeIdBytes.length;
+                serializeTlv(statefulTlvs.getNodeIdentifier(), body);
             }
         }
-
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (statefulBytes != null) {
-            ByteArray.copyWhole(statefulBytes, result, offset);
-            offset += statefulBytes.length;
-        }
-        if (nodeIdBytes != null) {
-            ByteArray.copyWhole(nodeIdBytes, result, offset);
-            offset += nodeIdBytes.length;
-        }
-        return result;
     }
 }
index 49d4f03736ef43455b175d32979d168f4db03a10..b5ad0e30d2a8bf5e79c55aadc2bf6a4caddc1578 100644 (file)
@@ -7,18 +7,20 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
@@ -35,11 +37,7 @@ public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSer
 
     public static final int TYPE = 21;
 
-    private static final int IP4_F_LENGTH = 4;
-    private static final int IP6_F_LENGTH = 16;
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int ERROR_CODE_F_LENGTH = 1;
-    private static final int ERROR_VALUE_F_LENGTH = 2;
 
     private static final int IN_PLACE_FLAG_OFFSET = 7;
     private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
@@ -54,9 +52,9 @@ public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSer
         }
         final RsvpErrorBuilder builder = new RsvpErrorBuilder();
         if (buffer.readableBytes() == V4_RSVP_LENGTH) {
-            builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH))));
+            builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
         } else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
-            builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH))));
+            builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
         }
         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
         builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
@@ -68,33 +66,28 @@ public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSer
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
         final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
         final RsvpError rsvp = rsvpTlv.getRsvpError();
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
-        flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
-        int offset = 0;
+        Flags f = rsvp.getFlags();
+        if (f.isInPlace() != null) {
+            flags.set(IN_PLACE_FLAG_OFFSET, f.isInPlace());
+        }
+        if (f.isNotGuilty() != null) {
+            flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
+        }
         final IpAddress node = rsvp.getNode();
-        byte[] bytes;
         if (node.getIpv4Address() != null) {
-            bytes = new byte[V4_RSVP_LENGTH];
-            ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
-            offset += IP4_F_LENGTH;
+            body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
         } else {
-            bytes = new byte[V6_RSVP_LENGTH];
-            ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
-            offset += IP6_F_LENGTH;
+            body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
         }
-        bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
-        offset += FLAGS_F_LENGTH;
-        bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
-        offset += ERROR_CODE_F_LENGTH;
-        final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
-        ByteArray.copyWhole(value, bytes, offset);
-        return TlvUtil.formatTlv(TYPE, bytes);
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        body.writeByte(rsvp.getCode());
+        body.writeShort(rsvp.getValue().shortValue());
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index 6707200607b72b5ef670a67e630f0431a5a10655..d354a473058553022b0d2539f8475b1facf20b90 100644 (file)
@@ -7,14 +7,17 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.StatefulBuilder;
@@ -46,14 +49,18 @@ public class Stateful02StatefulCapabilityTlvParser implements TlvParser, TlvSeri
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
         final Stateful sct = (Stateful) tlv;
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
-        flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
-        return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        if (sct.isLspUpdateCapability() != null) {
+            flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+        }
+        if (sct.isIncludeDbVersion() != null) {
+            flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index 602d186386cc0c6d860dda61ee4773ed6d974569..1289b5966bd6ca0d06ac73d3396aed245c0c6430 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.ietf;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
 import java.math.BigInteger;
@@ -54,7 +55,9 @@ public class PCEPTlvParserTest {
         final Stateful02StatefulCapabilityTlvParser parser = new Stateful02StatefulCapabilityTlvParser();
         final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).setIncludeDbVersion(false).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(statefulBytes, 4))));
-        assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(statefulBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -63,7 +66,9 @@ public class PCEPTlvParserTest {
         final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName("Med test of symbolic name".getBytes())).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(symbolicNameBytes, 4, 25))));
-        assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(symbolicNameBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -73,7 +78,9 @@ public class PCEPTlvParserTest {
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.NodeIdentifier(ByteArray.subByte(
                         nodeIdentifierBytes, 4, 25))).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(nodeIdentifierBytes, 4, 25))));
-        assertArrayEquals(nodeIdentifierBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(nodeIdentifierBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -86,7 +93,9 @@ public class PCEPTlvParserTest {
         builder.setValue(5634);
         final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpErrorBytes, 4, 8))));
-        assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(rsvpErrorBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -101,7 +110,9 @@ public class PCEPTlvParserTest {
         builder.setValue(50649);
         final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpError6Bytes, 4, 20))));
-        assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(rsvpError6Bytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -109,7 +120,9 @@ public class PCEPTlvParserTest {
         final Stateful02LspDbVersionTlvParser parser = new Stateful02LspDbVersionTlvParser();
         final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(180L)).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspDbBytes, 4))));
-        assertArrayEquals(lspDbBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(lspDbBytes, ByteArray.getAllBytes(buff));
 
     }
 }
index 8abce5982e0a45ac9367137351c8b586141f9bc0..0de742e8d2d66d1392a00e92912eb61fa1129e4f 100644 (file)
@@ -98,9 +98,7 @@ public class CInitiated00LspObjectParser extends Stateful07LspObjectParser {
             retBytes[3] |= (op & 7) << 4;
         }
         body.writeBytes(retBytes);
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        body.writeBytes(tlvs);
+        serializeTlvs(specObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 0954f3c55eeb837e5097083b12ecfafb345bcd9e..37f6ca65bfebc00ce97aafa052fe8123a08ff081 100644 (file)
@@ -62,16 +62,14 @@ public class CInitiated00SrpObjectParser extends Stateful07SrpObjectParser {
     public void serializeObject(final Object object, final ByteBuf buffer) {
         Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s. Needed SrpObject.", object.getClass());
         final Srp srp = (Srp) object;
-        ByteBuf body = Unpooled.buffer();
-        //FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(srp.getTlvs());
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_SIZE * Byte.SIZE);
         if (srp.getAugmentation(Srp1.class) != null && srp.getAugmentation(Srp1.class).isRemove()) {
             flags.set(REMOVE_FLAG, srp.getAugmentation(Srp1.class).isRemove());
         }
         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_SIZE));
         body.writeInt(srp.getOperationId().getValue().intValue());
-        body.writeBytes(tlvs);
+        serializeTlvs(srp.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 3ae66c0573a470bdf9332428bf41f599658aaa18..a3626d368f16e47c0acd1406eca3823bc0db652c 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.protocol.pcep.ietf.initiated00;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
@@ -49,19 +52,19 @@ public final class CInitiated00StatefulCapabilityTlvParser extends Stateful07Sta
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
         final Stateful sct = (Stateful) tlv;
-
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
         final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
         if (sfi != null) {
             flags.set(I_FLAG_OFFSET, sfi.isInitiation());
         }
-        flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
-        return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        if (sct.isLspUpdateCapability() != null) {
+            flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index 3a5f24622cedd67460597bbaf2e9223250f8f665..1501c1d732924f5a90bc72e920139f844e3c9fa2 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
@@ -35,12 +38,8 @@ public final class Stateful07LSPIdentifierIpv4TlvParser implements TlvParser, Tl
 
     public static final int TYPE = 18;
 
-    private static final int IP4_F_LENGTH = 4;
     private static final int EX_TUNNEL_ID4_F_LENGTH = 4;
 
-    private static final int LSP_ID_F_LENGTH = 2;
-    private static final int TUNNEL_ID_F_LENGTH = 2;
-
     private static final int V4_LENGTH = 16;
 
     @Override
@@ -52,40 +51,31 @@ public final class Stateful07LSPIdentifierIpv4TlvParser implements TlvParser, Tl
             throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv4 tlv length.");
         }
         final Ipv4Builder builder = new Ipv4Builder();
-        builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH)));
+        builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH)));
         final LspId lspId = new LspId((long) buffer.readUnsignedShort());
         final TunnelId tunnelId = new TunnelId(buffer.readUnsignedShort());
         builder.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer,
                 EX_TUNNEL_ID4_F_LENGTH))));
-        builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH)));
+        builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH)));
         final AddressFamily afi = new Ipv4CaseBuilder().setIpv4(builder.build()).build();
         return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("LspIdentifiersTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "LspIdentifiersTlv is mandatory.");
         final LspIdentifiers lsp = (LspIdentifiers) tlv;
         final AddressFamily afi = lsp.getAddressFamily();
-
+        final ByteBuf body = Unpooled.buffer();
         if (afi.getImplementedInterface().equals(Ipv6Case.class)) {
-            return new Stateful07LSPIdentifierIpv6TlvParser().serializeTlv(tlv);
+            new Stateful07LSPIdentifierIpv6TlvParser().serializeTlv(tlv, buffer);
         }
-
-        final byte[] bytes = new byte[V4_LENGTH];
-        int offset = 0;
         final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
-        ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelSenderAddress()), bytes, offset);
-        offset += IP4_F_LENGTH;
-        ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
-        offset += LSP_ID_F_LENGTH;
-        ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
-        offset += TUNNEL_ID_F_LENGTH;
-        ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4ExtendedTunnelId()), bytes, offset);
-        offset += EX_TUNNEL_ID4_F_LENGTH;
-        ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelEndpointAddress()), bytes, offset);
-        return TlvUtil.formatTlv(TYPE, bytes);
+        body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelSenderAddress()));
+        body.writeShort(lsp.getLspId().getValue().shortValue());
+        body.writeShort(lsp.getTunnelId().getValue().shortValue());
+        body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4ExtendedTunnelId()));
+        body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelEndpointAddress()));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index 3264d4c9716af7d5b37b1e25b561b85eaa1f101b..040f96b06f8b1d8d01692a3d76d05bef2fa93171 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
@@ -34,12 +37,8 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl
 
     public static final int TYPE = 19;
 
-    private static final int IP6_F_LENGTH = 16;
     private static final int EX_TUNNEL_ID6_F_LENGTH = 16;
 
-    private static final int LSP_ID_F_LENGTH = 2;
-    private static final int TUNNEL_ID_F_LENGTH = 2;
-
     private static final int V6_LENGTH = 52;
 
     @Override
@@ -51,34 +50,27 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl
             throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv6 tlv length.");
         }
         final Ipv6Builder builder = new Ipv6Builder();
-        builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH)));
+        builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
         final LspId lspId = new LspId((long) buffer.readUnsignedShort());
         final TunnelId tunnelId = new TunnelId(buffer.readUnsignedShort());
         builder.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer,
                 EX_TUNNEL_ID6_F_LENGTH))));
-        builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH)));
+        builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
         final AddressFamily afi = new Ipv6CaseBuilder().setIpv6(builder.build()).build();
         return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("LspIdentifiersTlv is mandatory.");
-        }
-        final byte[] bytes = new byte[V6_LENGTH];
-        int offset = 0;
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "LspIdentifiersTlv is mandatory.");
         final LspIdentifiers lsp = (LspIdentifiers) tlv;
+        final ByteBuf body = Unpooled.buffer();
         final Ipv6 ipv6 = ((Ipv6Case) lsp.getAddressFamily()).getIpv6();
-        ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()), bytes, offset);
-        offset += IP6_F_LENGTH;
-        ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
-        offset += LSP_ID_F_LENGTH;
-        ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
-        offset += TUNNEL_ID_F_LENGTH;
-        ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()), bytes, offset);
-        offset += EX_TUNNEL_ID6_F_LENGTH;
-        ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()), bytes, offset);
-        return TlvUtil.formatTlv(TYPE, bytes);
+        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()));
+        body.writeShort(lsp.getLspId().getValue().shortValue());
+        body.writeShort(lsp.getTunnelId().getValue().shortValue());
+        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()));
+        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index e1ebd35f76b07eeed1e5b8330c75b86e4b27022d..eaee975b4ce630a0837b6ef9126b63c0ef107a03 100644 (file)
@@ -131,64 +131,28 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
             retBytes[3] |= (op & 7) << 4;
         }
         body.writeBytes(retBytes);
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        body.writeBytes(tlvs);
+        serializeTlvs(specObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        int finalLength = 0;
-        byte[] lspErrBytes = null;
-        byte[] lspIdBytes = null;
-        byte[] rsvpErrBytes = null;
-        byte[] symbBytes = null;
-        byte[] vsTlvBytes = null;
         if (tlvs.getLspErrorCode() != null) {
-            lspErrBytes = serializeTlv(tlvs.getLspErrorCode());
-            finalLength += lspErrBytes.length;
+            serializeTlv(tlvs.getLspErrorCode(), body);
         }
         if (tlvs.getLspIdentifiers() != null) {
-            lspIdBytes = serializeTlv(tlvs.getLspIdentifiers());
-            finalLength += lspIdBytes.length;
+            serializeTlv(tlvs.getLspIdentifiers(), body);
         }
         if (tlvs.getRsvpErrorSpec() != null) {
-            rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
-            finalLength += rsvpErrBytes.length;
+            serializeTlv(tlvs.getRsvpErrorSpec(), body);
         }
         if (tlvs.getSymbolicPathName() != null) {
-            symbBytes = serializeTlv(tlvs.getSymbolicPathName());
-            finalLength += symbBytes.length;
+            serializeTlv(tlvs.getSymbolicPathName(), body);
         }
         if (tlvs.getVsTlv() != null) {
-            vsTlvBytes = serializeTlv(tlvs.getVsTlv());
-            finalLength += vsTlvBytes.length;
+            serializeTlv(tlvs.getVsTlv(), body);
         }
-        int offset = 0;
-        final byte[] result = new byte[finalLength];
-        if (lspErrBytes != null) {
-            ByteArray.copyWhole(lspErrBytes, result, offset);
-            offset += lspErrBytes.length;
-        }
-        if (lspIdBytes != null) {
-            ByteArray.copyWhole(lspIdBytes, result, offset);
-            offset += lspIdBytes.length;
-        }
-        if (rsvpErrBytes != null) {
-            ByteArray.copyWhole(rsvpErrBytes, result, offset);
-            offset += rsvpErrBytes.length;
-        }
-        if (symbBytes != null) {
-            ByteArray.copyWhole(symbBytes, result, offset);
-            offset += symbBytes.length;
-        }
-        if (vsTlvBytes != null) {
-            ByteArray.copyWhole(vsTlvBytes, result, offset);
-            offset += vsTlvBytes.length;
-        }
-        return result;
     }
 }
index 1fd18d046a2c17eb0bd3534cbcf4f08b1fd0d71b..1c4093d8667d6c26acd83a81b7a738ae8e39899a 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
@@ -35,11 +38,8 @@ public final class Stateful07LspSymbolicNameTlvParser implements TlvParser, TlvS
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("SymbolicPathNameTlv is mandatory.");
-        }
-        final SymbolicPathName spn = (SymbolicPathName) tlv;
-        return TlvUtil.formatTlv(TYPE, spn.getPathName().getValue());
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "SymbolicPathNameTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((SymbolicPathName) tlv).getPathName().getValue()), buffer);
     }
 }
index a84de90db7947f08f1f197cff15e81bc9d021d2d..afdc23dd332f380f991c76a5c694bfc3300bb5fb 100644 (file)
@@ -7,13 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -25,8 +27,6 @@ public final class Stateful07LspUpdateErrorTlvParser implements TlvParser, TlvSe
 
     public static final int TYPE = 20;
 
-    private static final int UPDATE_ERR_CODE_LENGTH = 4;
-
     @Override
     public LspErrorCode parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -36,11 +36,8 @@ public final class Stateful07LspUpdateErrorTlvParser implements TlvParser, TlvSe
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("LspErrorCodeTlv is mandatory.");
-        }
-        final LspErrorCode lsp = (LspErrorCode) tlv;
-        return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(lsp.getErrorCode(), UPDATE_ERR_CODE_LENGTH));
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "LspErrorCodeTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((LspErrorCode) tlv).getErrorCode().intValue()), buffer);
     }
 }
index 09bf179d8dd0803dda70393181579d8a25d309e2..108605a937258f2881af6cb63d9732d9e0c0adc2 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
@@ -43,27 +44,16 @@ public class Stateful07LspaObjectParser extends PCEPLspaObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] nameBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs2.class) != null) {
             final Tlvs2 nameTlvs = tlvs.getAugmentation(Tlvs2.class);
             if (nameTlvs.getSymbolicPathName() != null) {
-                nameBytes = serializeTlv(nameTlvs.getSymbolicPathName());
-                finalLength += nameBytes.length;
+                serializeTlv(nameTlvs.getSymbolicPathName(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (nameBytes != null) {
-            ByteArray.copyWhole(nameBytes, result, offset);
-            offset += nameBytes.length;
-        }
-        return result;
     }
 }
index e79d42f61c2ebdd12f349b99ef13257f38a01165..bc593aef53fee2b3153d3832fffb8776f294bd6f 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
@@ -43,36 +44,19 @@ public class Stateful07OpenObjectParser extends PCEPOpenObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] ofListBytes = null;
-        byte[] statefulBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getOfList() != null) {
-            ofListBytes = serializeTlv(tlvs.getOfList());
-            finalLength += ofListBytes.length;
+            serializeTlv(tlvs.getOfList(), body);
         }
         if (tlvs.getAugmentation(Tlvs1.class) != null) {
             final Tlvs1 statefulTlvs = tlvs.getAugmentation(Tlvs1.class);
             if (statefulTlvs.getStateful() != null) {
-                statefulBytes = serializeTlv(statefulTlvs.getStateful());
-                finalLength += statefulBytes.length;
+                serializeTlv(statefulTlvs.getStateful(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (ofListBytes != null) {
-            ByteArray.copyWhole(ofListBytes, result, offset);
-            offset += ofListBytes.length;
-        }
-        if (statefulBytes != null) {
-            ByteArray.copyWhole(statefulBytes, result, offset);
-            offset += statefulBytes.length;
-        }
-        return result;
     }
 }
index 0df3ad11d66ad45fbf84eeeb93f5e7774e100146..69811002b43cd8c9e639404cd28d5f0fb90ee666 100644 (file)
@@ -7,18 +7,20 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
@@ -43,16 +45,7 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer
 
     public static final int TYPE = 21;
 
-    private static final int IP4_F_LENGTH = 4;
-    private static final int IP6_F_LENGTH = 16;
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int ERROR_CODE_F_LENGTH = 1;
-    private static final int ERROR_VALUE_F_LENGTH = 2;
-
-    private static final int ENTERPRISE_F_LENGTH = 4;
-    private static final int SUB_ORG_F_LENGTH = 1;
-    private static final int ERR_DESCR_LENGTH_F_LENGTH = 1;
-    private static final int USER_VALUE_F_LENGTH = 2;
 
     private static final int RSVP_ERROR_CLASS_NUM = 6;
     private static final int RSVP_IPV4_ERROR_CLASS_TYPE = 1;
@@ -64,9 +57,6 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer
     private static final int IN_PLACE_FLAG_OFFSET = 7;
     private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
 
-    private static final int V4_RSVP_LENGTH = 10;
-    private static final int V6_RSVP_LENGTH = 22;
-
     @Override
     public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -84,18 +74,18 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
         final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
-
+        final ByteBuf body = Unpooled.buffer();
         if (rsvp.getErrorType().getImplementedInterface().equals(RsvpCase.class)) {
             final RsvpCase r = (RsvpCase) rsvp.getErrorType();
-            return TlvUtil.formatTlv(TYPE, serializeRsvp(r.getRsvpError()));
+            serializeRsvp(r.getRsvpError(), body);
+            TlvUtil.formatTlv(TYPE, body, buffer);
         } else {
             final UserCase u = (UserCase) rsvp.getErrorType();
-            return TlvUtil.formatTlv(TYPE, serializerUserError(u.getUserError()));
+            serializerUserError(u.getUserError(), body);
+            TlvUtil.formatTlv(TYPE, body, buffer);
         }
     }
 
@@ -110,36 +100,23 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer
         return new UserCaseBuilder().setUserError(error.build()).build();
     }
 
-    private byte[] serializerUserError(final UserError ue) {
-        final byte[] enterprise = ByteArray.longToBytes(ue.getEnterprise().getValue(), ENTERPRISE_F_LENGTH);
-        final byte suborg = UnsignedBytes.checkedCast(ue.getSubOrg());
-        final byte[] value = ByteArray.intToBytes(ue.getValue(), USER_VALUE_F_LENGTH);
-        final byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
-        final byte descLen = UnsignedBytes.checkedCast(desc.length);
-        // if we have any subobjects, place the implementation here
-        final byte[] bytes = new byte[2 + ENTERPRISE_F_LENGTH + SUB_ORG_F_LENGTH + USER_VALUE_F_LENGTH + ERR_DESCR_LENGTH_F_LENGTH
-                + desc.length];
-        bytes[0] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_NUM);
-        bytes[1] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_TYPE);
-        int offset = 2;
-        ByteArray.copyWhole(enterprise, bytes, offset);
-        offset += ENTERPRISE_F_LENGTH;
-        bytes[offset] = suborg;
-        offset += SUB_ORG_F_LENGTH;
-        bytes[offset] = descLen;
-        offset += ERR_DESCR_LENGTH_F_LENGTH;
-        ByteArray.copyWhole(value, bytes, offset);
-        offset += USER_VALUE_F_LENGTH;
-        ByteArray.copyWhole(desc, bytes, offset);
-        return bytes;
+    private void serializerUserError(final UserError ue, final ByteBuf body) {
+        byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
+        body.writeByte(USER_ERROR_CLASS_NUM);
+        body.writeByte(USER_ERROR_CLASS_TYPE);
+        body.writeInt(ue.getEnterprise().getValue().intValue());
+        body.writeByte(ue.getSubOrg());
+        body.writeByte(desc.length);
+        body.writeShort(ue.getValue().shortValue());
+        body.writeBytes(desc);
     }
 
     private RsvpCase parseRsvp(final int classType, final ByteBuf buffer) {
         final RsvpErrorBuilder builder = new RsvpErrorBuilder();
         if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
-            builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH))));
+            builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
         } else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
-            builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH))));
+            builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
         }
         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
         builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
@@ -150,34 +127,22 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer
         return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
     }
 
-    private byte[] serializeRsvp(final RsvpError rsvp) {
+    private void serializeRsvp(final RsvpError rsvp, final ByteBuf body) {
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
         flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
         flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
-        int offset = 0;
         final IpAddress node = rsvp.getNode();
-        byte[] bytes;
         if (node.getIpv4Address() != null) {
-            bytes = new byte[V4_RSVP_LENGTH];
-            bytes[0] = RSVP_ERROR_CLASS_NUM;
-            bytes[1] = RSVP_IPV4_ERROR_CLASS_TYPE;
-            offset += 2;
-            ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
-            offset += IP4_F_LENGTH;
+            body.writeByte(RSVP_ERROR_CLASS_NUM);
+            body.writeByte(RSVP_IPV4_ERROR_CLASS_TYPE);
+            body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
         } else {
-            bytes = new byte[V6_RSVP_LENGTH];
-            bytes[0] = RSVP_ERROR_CLASS_NUM;
-            bytes[1] = RSVP_IPV6_ERROR_CLASS_TYPE;
-            offset += 2;
-            ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
-            offset += IP6_F_LENGTH;
+            body.writeByte(RSVP_ERROR_CLASS_NUM);
+            body.writeByte(RSVP_IPV6_ERROR_CLASS_TYPE);
+            body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
         }
-        bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
-        offset += FLAGS_F_LENGTH;
-        bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
-        offset += ERROR_CODE_F_LENGTH;
-        final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
-        ByteArray.copyWhole(value, bytes, offset);
-        return bytes;
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        body.writeByte(rsvp.getCode());
+        body.writeShort(rsvp.getValue().shortValue());
     }
 }
index c5e73910c8268f3cd4e75f46483b748696a26733..f6c4316291e84e1abf6929bab39ab93e58aec89a 100644 (file)
@@ -76,22 +76,19 @@ public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<SrpB
     public void serializeObject(final Object object, final ByteBuf buffer) {
         Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.", object.getClass());
         final Srp srp = (Srp) object;
-        ByteBuf body = Unpooled.buffer();
-        //FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(srp.getTlvs());
+        final ByteBuf body = Unpooled.buffer();
         body.writerIndex(body.writerIndex() + FLAGS_SIZE);
         final Long id = srp.getOperationId().getValue();
         body.writeInt(id.intValue());
-        body.writeBytes(tlvs);
+        serializeTlvs(srp.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         } else if (tlvs.getSymbolicPathName() != null) {
-            return serializeTlv(tlvs.getSymbolicPathName());
+            serializeTlv(tlvs.getSymbolicPathName(), body);
         }
-        return new byte[0];
     }
 }
index 09d0345cea7de780ae0f1333e9835a391735a53d..26abb3cba63a24eeedb75c13c1d25696b34f714b 100644 (file)
@@ -7,14 +7,17 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
@@ -48,15 +51,15 @@ public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSeri
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
         final Stateful sct = (Stateful) tlv;
-
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
-        flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
-        return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        if (sct.isLspUpdateCapability() != null) {
+            flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index 7d636791e0ef0147ff5c7bae935a82d48a79fc72..918a2c56492c1bdc518d16b3ccdcb204d300e0cd 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.ietf;
 
 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;
@@ -73,7 +74,9 @@ public class PCEPTlvParserTest {
         final Stateful07StatefulCapabilityTlvParser parser = new Stateful07StatefulCapabilityTlvParser();
         final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(statefulBytes, 4))));
-        assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(statefulBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -82,7 +85,9 @@ public class PCEPTlvParserTest {
         final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med test of symbolic nameeee".getBytes())).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(symbolicNameBytes, 4))));
-        assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(symbolicNameBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -90,7 +95,9 @@ public class PCEPTlvParserTest {
         final Stateful07LspUpdateErrorTlvParser parser = new Stateful07LspUpdateErrorTlvParser();
         final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspUpdateErrorBytes, 4))));
-        assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(lspUpdateErrorBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -104,7 +111,9 @@ public class PCEPTlvParserTest {
         final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(afi.build()).build()).setLspId(
                 new LspId(65535L)).setTunnelId(new TunnelId(4660)).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspIdentifiers4Bytes, 4))));
-        assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(lspIdentifiers4Bytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -123,7 +132,9 @@ public class PCEPTlvParserTest {
         final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv6CaseBuilder().setIpv6(afi.build()).build()).setLspId(
                 new LspId(4660L)).setTunnelId(new TunnelId(65535)).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspIdentifiers6Bytes, 4))));
-        assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(lspIdentifiers6Bytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -136,7 +147,9 @@ public class PCEPTlvParserTest {
         builder.setValue(5634);
         final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(rsvpErrorBytes, 4))));
-        assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(rsvpErrorBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -151,7 +164,9 @@ public class PCEPTlvParserTest {
         builder.setValue(50649);
         final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(rsvpError6Bytes, 4))));
-        assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(rsvpError6Bytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -164,6 +179,8 @@ public class PCEPTlvParserTest {
         builder.setDescription("user desc");
         final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserCaseBuilder().setUserError(builder.build()).build()).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(userErrorBytes, 4))));
-        assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(userErrorBytes, ByteArray.getAllBytes(buff));
     }
 }
index bb5f371c7d13e9d3415b5d4b5fa78d2e8fee9a9b..dee4e8fb3fbc16584966e4e6990f5650791f87fd 100644 (file)
@@ -71,20 +71,15 @@ public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObj
         body.writeZero(FLAGS_F_LENGTH + RESERVED);
         body.writeByte(errObj.getType());
         body.writeByte(errObj.getValue());
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(errObj.getTlvs());
-        if (tlvs.length != 0) {
-            body.writeBytes(tlvs);
-        }
+        serializeTlvs(errObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         } else if (tlvs.getReqMissing() != null) {
-            return serializeTlv(tlvs.getReqMissing());
+            serializeTlv(tlvs.getReqMissing(), body);
         }
-        return new byte[0];
     }
 }
index c1d9b5b498d89a5d4c429c2bcadc82b7969c1617..ee0057d6bb405d0f0c1ca037fd1841a4d7a4dd44 100644 (file)
@@ -91,15 +91,11 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
         }
         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
         body.writeZero(RESERVED);
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
-        if (tlvs.length != 0) {
-            body.writeBytes(tlvs);
-        }
+        serializeTlvs(lspaObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
-        return new byte[0];
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
+        return;
     }
 }
index fdbc0dc6a0b847c3d3c3e937daa6e11b8d3eaad5..72c879b90702e613300adc2cbddeadeff4e81c24 100644 (file)
@@ -88,20 +88,16 @@ public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathB
         }
         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
         body.writeZero(RESERVED_F_LENGTH);
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
-        if (tlvs != null) {
-            body.writeBytes(tlvs);
-        }
+        serializeTlvs(nPObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
-        } else if (tlvs.getNoPathVector() != null) {
-            return serializeTlv(tlvs.getNoPathVector());
+            return;
+        }
+        if (tlvs.getNoPathVector() != null) {
+            serializeTlv(tlvs.getNoPathVector(), body);
         }
-        return new byte[0];
     }
 }
index a922f06e893141c7658ea9190574b41f7fa84c86..51e03d26588e94d2deacf70660dc0572ba63ef2e 100644 (file)
@@ -72,20 +72,16 @@ public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<C
         body.writeZero(NT_F_OFFSET);
         body.writeByte(notObj.getType());
         body.writeByte(notObj.getValue());
-        // FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(notObj.getTlvs());
-        if (tlvs.length != 0) {
-            body.writeBytes(tlvs);
-        }
+        serializeTlvs(notObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
-        } else if (tlvs.getOverloadDuration() != null) {
-            return serializeTlv(tlvs.getOverloadDuration());
+            return;
+        }
+        if (tlvs.getOverloadDuration() != null) {
+            serializeTlv(tlvs.getOverloadDuration(), body);
         }
-        return new byte[0];
     }
 }
index bf7df42f6e16c3d2f9b40306a52892dc334a74a5..5b562b301a10c36eccc0c645675f164d380e16e0 100644 (file)
@@ -102,24 +102,16 @@ public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
         body.writeByte(open.getKeepalive());
         body.writeByte(open.getDeadTimer());
         body.writeByte(open.getSessionId());
-        //FIXME: switch to ByteBuf
-        body.writeBytes(serializeTlvs(open.getTlvs()));
+        serializeTlvs(open.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        byte[] ofListBytes = null;
         if (tlvs.getOfList() != null) {
-            ofListBytes = serializeTlv(tlvs.getOfList());
+            serializeTlv(tlvs.getOfList(), body);
         }
-        byte[] result = new byte[0];
-        if (ofListBytes != null) {
-            result = new byte[ofListBytes.length];
-            ByteArray.copyWhole(ofListBytes, result, 0);
-        }
-        return result;
     }
 }
index 0a62f68b476378cd98a60ee09d0b26a22105b39c..8a31586adc44058c2cceb0a11e9aceb09bf5ceec 100644 (file)
@@ -171,20 +171,15 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
         }
         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH));
         body.writeInt(rpObj.getRequestId().getValue().intValue());
-        //FIXME: switch to ByteBuf
-        final byte[] tlvs = serializeTlvs(rpObj.getTlvs());
-        if (tlvs.length != 0) {
-            body.writeBytes(tlvs);
-        }
+        serializeTlvs(rpObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
-    public byte[] serializeTlvs(final Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         } else if (tlvs.getOrder() != null) {
-            return serializeTlv(tlvs.getOrder());
+            serializeTlv(tlvs.getOrder(), body);
         }
-        return new byte[0];
     }
 }
index 1e829b268fd1f1900c47261a0f16280f3487f972..2b85ef625e5bd3e8d57c1a5412968d4e1fe2554a 100644 (file)
@@ -7,11 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -26,21 +30,15 @@ public abstract class AbstractVendorSpecificTlvParser implements TlvParser, TlvS
     protected static final int ENTERPRISE_NUM_LENGTH = 4;
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "Vendor Specific Tlv is mandatory.");
         final VsTlv vsTlv = (VsTlv) tlv;
+        final ByteBuf body = Unpooled.buffer();
         if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
-            final byte[] payloadBytes = serializeVendorPayload(vsTlv.getVendorPayload());
-            final byte[] ianaNumBytes = ByteArray.longToBytes(vsTlv.getEnterpriseNumber().getValue(), ENTERPRISE_NUM_LENGTH);
-
-            final byte[] bytes = new byte[ianaNumBytes.length + payloadBytes.length];
-            System.arraycopy(ianaNumBytes, 0, bytes, 0, ENTERPRISE_NUM_LENGTH);
-            System.arraycopy(payloadBytes, 0, bytes, ENTERPRISE_NUM_LENGTH, payloadBytes.length);
-            return TlvUtil.formatTlv(TYPE, bytes);
+            body.writeInt(vsTlv.getEnterpriseNumber().getValue().intValue());
+            body.writeBytes(serializeVendorPayload(vsTlv.getVendorPayload()));
+            TlvUtil.formatTlv(TYPE, body, buffer);
         }
-        return new byte[0];
     }
 
     @Override
index c4cc3b7526a405a22c7afa12b877a56107f54024..ec87c54a53efb8356c5cf159e4cb59823e0103ca 100644 (file)
@@ -7,13 +7,17 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.BitSet;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -53,21 +57,37 @@ public class NoPathVectorTlvParser implements TlvParser, TlvSerializer {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlvs) {
-        if (tlvs == null) {
-            throw new IllegalArgumentException("NoPathVectorTlv is mandatory.");
-        }
-        final NoPathVector tlv = (NoPathVector) tlvs;
-
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "NoPathVectorTlv is mandatory.");
+        final NoPathVector noPath = (NoPathVector) tlv;
+        final ByteBuf body = Unpooled.buffer();
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(REACHABLITY_PROBLEM, tlv.getFlags().isP2mpUnreachable());
-        flags.set(NO_GCO_SOLUTION, tlv.getFlags().isNoGcoSolution());
-        flags.set(NO_GCO_MIGRATION_PATH, tlv.getFlags().isNoGcoMigration());
-        flags.set(PATH_KEY, tlv.getFlags().isPathKey());
-        flags.set(CHAIN_UNAVAILABLE, tlv.getFlags().isChainUnavailable());
-        flags.set(UNKNOWN_SRC, tlv.getFlags().isUnknownSource());
-        flags.set(UNKNOWN_DEST, tlv.getFlags().isUnknownDestination());
-        flags.set(PCE_UNAVAILABLE, tlv.getFlags().isPceUnavailable());
-        return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        Flags f = noPath.getFlags();
+        if (f.isP2mpUnreachable() != null) {
+            flags.set(REACHABLITY_PROBLEM, f.isP2mpUnreachable());
+        }
+        if (f.isNoGcoSolution() != null) {
+            flags.set(NO_GCO_SOLUTION, f.isNoGcoSolution());
+        }
+        if (f.isNoGcoMigration() != null) {
+            flags.set(NO_GCO_MIGRATION_PATH, f.isNoGcoMigration());
+        }
+        if (f.isPathKey() != null) {
+            flags.set(PATH_KEY, f.isPathKey());
+        }
+        if (f.isChainUnavailable() != null) {
+            flags.set(CHAIN_UNAVAILABLE, f.isChainUnavailable());
+        }
+        if (f.isUnknownSource() != null) {
+            flags.set(UNKNOWN_SRC, f.isUnknownSource());
+        }
+        if (f.isUnknownDestination() != null) {
+            flags.set(UNKNOWN_DEST, f.isUnknownDestination());
+        }
+        if (f.isPceUnavailable() != null) {
+            flags.set(PCE_UNAVAILABLE, f.isPceUnavailable());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index c66dbde455d28ec5dc9169230fb931fe3ab25d0d..258e78adb3bdea2db1f8ced12f84428ba06ef927 100644 (file)
@@ -7,16 +7,18 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.List;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
@@ -47,19 +49,14 @@ public class OFListTlvParser implements TlvParser, TlvSerializer {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("OFListTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "OFListTlv is mandatory.");
         final OfList oft = (OfList) tlv;
-
+        final ByteBuf body = Unpooled.buffer();
         final List<OfId> ofCodes = oft.getCodes();
-        final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
-
-        final int size = ofCodes.size();
-        for (int i = 0; i < size; i++) {
-            ByteArray.copyWhole(ByteArray.shortToBytes(ofCodes.get(i).getValue().shortValue()), retBytes, i * OF_CODE_ELEMENT_LENGTH);
+        for (OfId id : ofCodes) {
+            body.writeShort(id.getValue().shortValue());
         }
-        return TlvUtil.formatTlv(TYPE, retBytes);
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index df84505864ed1940ae20f978fec7700bac783941..b94fc55b8d4942574465f8f632c5bda41681a2fe 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
@@ -24,10 +27,6 @@ public class OrderTlvParser implements TlvParser, TlvSerializer {
 
     public static final int TYPE = 5;
 
-    private static final int ORDR_DEL_LENGTH = 4;
-
-    private static final int ORDR_SETUP_LENGTH = 4;
-
     @Override
     public Order parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -37,16 +36,12 @@ public class OrderTlvParser implements TlvParser, TlvSerializer {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("OrderTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "OrderTlv is mandatory.");
         final Order otlv = (Order) tlv;
-        final byte[] bytes = new byte[ORDR_DEL_LENGTH + ORDR_SETUP_LENGTH];
-        int offset = 0;
-        ByteArray.copyWhole(ByteArray.longToBytes(otlv.getDelete(), ORDR_DEL_LENGTH), bytes, offset);
-        offset += ORDR_DEL_LENGTH;
-        ByteArray.copyWhole(ByteArray.longToBytes(otlv.getSetup(), ORDR_SETUP_LENGTH), bytes, offset);
-        return TlvUtil.formatTlv(TYPE, bytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeInt(otlv.getDelete().intValue());
+        body.writeInt(otlv.getSetup().intValue());
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
index b5546147014900dec17a651f8d94469d2f7318cc..9701d8218085ea2777eca98836d3f9cf008c7349 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
@@ -24,8 +27,6 @@ public class OverloadedDurationTlvParser implements TlvParser, TlvSerializer {
 
     public static final int TYPE = 2;
 
-    private static final int OVERLOADED_DURATION_LENGTH = 4;
-
     @Override
     public OverloadDuration parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -35,11 +36,8 @@ public class OverloadedDurationTlvParser implements TlvParser, TlvSerializer {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("OverloadedTlv is mandatory.");
-        }
-        final OverloadDuration odt = (OverloadDuration) tlv;
-        return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(odt.getDuration(), OVERLOADED_DURATION_LENGTH));
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "OverloadedTlv is mandatory.");
+        TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((OverloadDuration) tlv).getDuration().intValue()), buffer);
     }
 }
index 9ec9213740eed32f9d0debd49127e80b6a651ff9..2033c29eea5d44ede0771ee5062ae8e07868ecb1 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
@@ -25,8 +28,6 @@ public class ReqMissingTlvParser implements TlvParser, TlvSerializer {
 
     public static final int TYPE = 3;
 
-    private static final int REQ_ID_LENGTH = 4;
-
     @Override
     public ReqMissing parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -36,11 +37,11 @@ public class ReqMissingTlvParser implements TlvParser, TlvSerializer {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
-        if (tlv == null) {
-            throw new IllegalArgumentException("ReqMissingTlv is mandatory.");
-        }
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        Preconditions.checkArgument(tlv != null, "ReqMissingTlv is mandatory.");
         final ReqMissing req = (ReqMissing) tlv;
-        return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(req.getRequestId().getValue(), REQ_ID_LENGTH));
+        final ByteBuf body = Unpooled.buffer();
+        body.writeInt(req.getRequestId().getValue().intValue());
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }
diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/TlvUtil.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tlv/TlvUtil.java
deleted file mode 100644 (file)
index 2facb59..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.pcep.impl.tlv;
-
-import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
-import org.opendaylight.protocol.util.ByteArray;
-
-public final class TlvUtil {
-
-    private static final int TLV_TYPE_F_LENGTH = 2;
-    private static final int TLV_LENGTH_F_LENGTH = 2;
-    private static final int HEADER_SIZE = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
-
-    protected static final int PADDED_TO = 4;
-
-    private TlvUtil() {
-        throw new UnsupportedOperationException("Utility class should not be instantiated");
-    }
-
-    public static byte[] formatTlv(final int type, final byte[] valueBytes) {
-        final byte[] typeBytes = ByteArray.intToBytes(type, TLV_TYPE_F_LENGTH);
-
-        final byte[] lengthBytes = ByteArray.intToBytes(valueBytes.length, TLV_LENGTH_F_LENGTH);
-
-        final byte[] bytes = new byte[HEADER_SIZE + valueBytes.length
-                + AbstractObjectWithTlvsParser.getPadding(HEADER_SIZE + valueBytes.length, PADDED_TO)];
-
-        int byteOffset = 0;
-        System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
-        byteOffset += TLV_TYPE_F_LENGTH;
-        System.arraycopy(lengthBytes, 0, bytes, byteOffset, TLV_LENGTH_F_LENGTH);
-        byteOffset += TLV_LENGTH_F_LENGTH;
-        System.arraycopy(valueBytes, 0, bytes, byteOffset, valueBytes.length);
-        return bytes;
-    }
-}
index 8b474dadd6b2373cd2568fea4e91b60b2336e9b3..f9e1adc5769a0633044c5975071feae6fe703451 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals;
 
 import com.google.common.collect.Lists;
 
+import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
 import java.util.List;
@@ -86,7 +87,9 @@ public class PCEPTlvParserTest {
         final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true)).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
-        assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -94,14 +97,19 @@ public class PCEPTlvParserTest {
         final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
         final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
-        assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
     }
 
+    @Test
     public void testReqMissingTlv() throws PCEPDeserializerException {
         final ReqMissingTlvParser parser = new ReqMissingTlvParser();
         final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
-        assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -109,7 +117,9 @@ public class PCEPTlvParserTest {
         final OrderTlvParser parser = new OrderTlvParser();
         final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
-        assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -120,13 +130,17 @@ public class PCEPTlvParserTest {
         ids.add(new OfId(0x5678));
         final OfList tlv = new OfListBuilder().setCodes(ids).build();
         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
-        assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(tlv, buff);
+        assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
     public void testVendorSpecificTlv() throws PCEPDeserializerException {
         VsTlv tlv = new VsTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(9L)).setVendorPayload(this.vp).build();
         assertEquals(tlv, this.vsParser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(vsTlvBytes, 4))));
-        assertArrayEquals(vsTlvBytes, this.vsParser.serializeTlv(tlv));
+        final ByteBuf buff = Unpooled.buffer();
+        this.vsParser.serializeTlv(tlv, buff);
+        assertArrayEquals(vsTlvBytes, ByteArray.getAllBytes(buff));
     }
 }
index 4beef11da5af4041fd6e97df2068038f7f732ca7..5ae8dc0cdad1b495bb0c42f3c706f6dcdae1df00 100644 (file)
@@ -5,12 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.lsp.setup.type01;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00SrpObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
@@ -44,28 +44,16 @@ public class CInitiated00SrpObjectWithPstTlvParser extends CInitiated00SrpObject
     }
 
     @Override
-    public byte[] serializeTlvs(Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] nameBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs8.class) != null) {
             final Tlvs8 nameTlvs = tlvs.getAugmentation(Tlvs8.class);
             if (nameTlvs.getPathSetupType() != null) {
-                nameBytes = serializeTlv(nameTlvs.getPathSetupType());
-                finalLength += nameBytes.length;
+                serializeTlv(nameTlvs.getPathSetupType(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (nameBytes != null) {
-            ByteArray.copyWhole(nameBytes, result, offset);
-            offset += nameBytes.length;
-        }
-        return result;
     }
-
 }
index 38f3ed07d0c0731f9780b2a019bfffc223b8adef..219ad1adafc3eef72332520efb3a893b42ed47c4 100644 (file)
@@ -5,15 +5,17 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.lsp.setup.type01;
 
 import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import io.netty.buffer.Unpooled;
+
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupTypeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -26,14 +28,17 @@ public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
     private static final int OFFSET = 4 - PST_LENGTH;
 
     @Override
-    public byte[] serializeTlv(Tlv tlv) {
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkNotNull(tlv, "PathSetupType is mandatory.");
         final PathSetupType pstTlv = (PathSetupType) tlv;
-        return TlvUtil.formatTlv(TYPE, new byte[] { 0, 0, 0, (byte) (pstTlv.isPst() ? 1 : 0) });
+        ByteBuf body = Unpooled.buffer();
+        body.writeZero(OFFSET);
+        body.writeBoolean((pstTlv.isPst() != null) ? pstTlv.isPst() : false);
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 
     @Override
-    public Tlv parseTlv(ByteBuf buffer) throws PCEPDeserializerException {
+    public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
             return null;
         }
index 012cbc587b7ff4257188e242fe2ef9f1811a081a..a448540ba33395f83bbbd4dd5ccbd85a2655c9f9 100644 (file)
@@ -5,12 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.lsp.setup.type01;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPRequestParameterObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
@@ -44,28 +44,16 @@ public class PcepRpObjectWithPstTlvParser extends PCEPRequestParameterObjectPars
     }
 
     @Override
-    public byte[] serializeTlvs(Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] nameBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs1.class) != null) {
             final Tlvs1 nameTlvs = tlvs.getAugmentation(Tlvs1.class);
             if (nameTlvs.getPathSetupType() != null) {
-                nameBytes = serializeTlv(nameTlvs.getPathSetupType());
-                finalLength += nameBytes.length;
+                serializeTlv(nameTlvs.getPathSetupType(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (nameBytes != null) {
-            ByteArray.copyWhole(nameBytes, result, offset);
-            offset += nameBytes.length;
-        }
-        return result;
     }
-
 }
index 8361a3a4b2b3219bf1a1019a54b637f79affc34d..ac29cdb33c16a5967131f3618d04f095b4427c0c 100644 (file)
@@ -5,12 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.segment.routing02;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
@@ -41,28 +41,16 @@ public class PcepOpenObjectWithSpcTlvParser extends PCEPOpenObjectParser {
     }
 
     @Override
-    public byte[] serializeTlvs(Tlvs tlvs) {
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
-            return new byte[0];
+            return;
         }
-        final byte[] prev = super.serializeTlvs(tlvs);
-        int finalLength = prev.length;
-        byte[] nameBytes = null;
+        super.serializeTlvs(tlvs, body);
         if (tlvs.getAugmentation(Tlvs1.class) != null) {
             final Tlvs1 spcTlvs = tlvs.getAugmentation(Tlvs1.class);
             if (spcTlvs.getSrPceCapability() != null) {
-                nameBytes = serializeTlv(spcTlvs.getSrPceCapability());
-                finalLength += nameBytes.length;
+                serializeTlv(spcTlvs.getSrPceCapability(), body);
             }
         }
-        final byte[] result = new byte[finalLength];
-        ByteArray.copyWhole(prev, result, 0);
-        int offset = prev.length;
-        if (nameBytes != null) {
-            ByteArray.copyWhole(nameBytes, result, offset);
-            offset += nameBytes.length;
-        }
-        return result;
     }
-
 }
index e1cec140b15664fa7c155c92a05e3d9e71facb78..fffeab667605094868235172bdacbef76dcde7e5 100644 (file)
@@ -5,16 +5,17 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.segment.routing02;
 
 import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import io.netty.buffer.Unpooled;
+
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -27,10 +28,9 @@ public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
     private static final int OFFSET = 4 - MSD_LENGTH;
 
     @Override
-    public byte[] serializeTlv(Tlv tlv) {
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkNotNull(tlv, "SrPceCapability is mandatory.");
-        final SrPceCapability spcTlv = (SrPceCapability) tlv;
-        return TlvUtil.formatTlv(TYPE, ByteArray.intToBytes(spcTlv.getMsd()));
+        TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((SrPceCapability) tlv).getMsd()), buffer);
     }
 
     @Override
@@ -41,5 +41,4 @@ public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
         final short msd = buffer.readerIndex(OFFSET).readUnsignedByte();
         return new SrPceCapabilityBuilder().setMsd(msd).build();
     }
-
 }
index 76311efc1a65b63632f9a5e93e827973d4494bcd..becc7e25cd4ad410d98d69858b18102678207368 100644 (file)
@@ -10,8 +10,9 @@ package org.opendaylight.protocol.pcep.lsp.setup.type01;
 
 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;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -26,9 +27,10 @@ public class PcepTlvParserTest {
     public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
         final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
         final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst(true).build();
-
         assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(pstTlvBytes, 4))));
-        assertArrayEquals(pstTlvBytes, parser.serializeTlv(pstTlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(pstTlv, buff);
+        assertArrayEquals(pstTlvBytes, ByteArray.getAllBytes(buff));
     }
 
 }
index 790eabf52950bb6461d2068c2aa7db87052eddbf..5b91e07a270e20898166baaf3b7075a85a7dafba 100644 (file)
@@ -10,8 +10,9 @@ package org.opendaylight.protocol.pcep.segment.routing02;
 
 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;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -27,7 +28,9 @@ public class PcepTlvParserTest {
         final SrPceCapabilityTlvParser parser = new SrPceCapabilityTlvParser();
         final SrPceCapability spcTlv = new SrPceCapabilityBuilder().setMsd((short) 1).build();
         assertEquals(spcTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(spcTlvBytes, 4))));
-        assertArrayEquals(spcTlvBytes, parser.serializeTlv(spcTlv));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeTlv(spcTlv, buff);
+        assertArrayEquals(spcTlvBytes, ByteArray.getAllBytes(buff));
     }
 
 }
index c7538be8b4425acd6713a33d4d93cc0bc33ad124..ec57536c6b4e2bff98c993359613da20c929b8d9 100644 (file)
@@ -12,8 +12,6 @@ import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 
-import java.util.Arrays;
-
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,15 +53,11 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
         }
     }
 
-    protected final byte[] serializeTlv(final Tlv tlv) {
+    protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
         LOG.trace("Serializing PCEP TLV {}", tlv);
-        final byte[] ret = this.tlvReg.serializeTlv(tlv);
-        if (ret == null) {
-            LOG.warn("TLV serializer for type {} could not be found.", tlv);
-        }
-        LOG.trace("Serialized PCEP TLV {}.", Arrays.toString(ret));
-        return ret;
+        this.tlvReg.serializeTlv(tlv, buffer);
+        LOG.trace("Serialized PCEP TLV {}.", ByteBufUtil.hexDump(buffer));
     }
 
     protected void addTlv(final T builder, final Tlv tlv) {
index 6703d1add3dfd57e446c08817d880cd4f36af65d..31bdc4fa2e4d8f1285029d87c9784dd9e336a0e9 100644 (file)
@@ -14,5 +14,5 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 public interface TlvRegistry {
     Tlv parseTlv(final int type, final ByteBuf buffer) throws PCEPDeserializerException;
 
-    byte[] serializeTlv(final Tlv tlv);
+    void serializeTlv(final Tlv tlv, final ByteBuf buffer);
 }
index 83bceadee890722e5fcea5d02d0d1574ecc46be0..9b0e144497d995233061ef84b4f7f7a55afe23a9 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.pcep.types.rev131005.Tlv;
 
 public interface TlvSerializer {
 
-    byte[] serializeTlv(Tlv tlv);
+    void serializeTlv(final Tlv tlv, final ByteBuf buffer);
 }
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/TlvUtil.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/TlvUtil.java
new file mode 100644 (file)
index 0000000..7f62280
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.pcep.spi;
+
+import io.netty.buffer.ByteBuf;
+
+public final class TlvUtil {
+
+    private static final int HEADER_SIZE = 4;
+
+    protected static final int PADDED_TO = 4;
+
+    private TlvUtil() {
+        throw new UnsupportedOperationException("Utility class should not be instantiated");
+    }
+
+    public static void formatTlv(final int type,final ByteBuf body, final ByteBuf out) {
+        out.writeShort(type);
+        out.writeShort(body.writerIndex());
+        out.writeBytes(body);
+        out.writeZero(AbstractObjectWithTlvsParser.getPadding(HEADER_SIZE + body.writerIndex(), PADDED_TO));
+    }
+}
index d4b57c3be60a2fac302f968dce31573b4922dc55..b00811beb6ded2899f8009466ac93b830f703709 100644 (file)
@@ -47,11 +47,11 @@ public final class SimpleTlvRegistry implements TlvRegistry {
     }
 
     @Override
-    public byte[] serializeTlv(final Tlv tlv) {
+    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         final TlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
         if (serializer == null) {
-            return null;
+            return;
         }
-        return serializer.serializeTlv(tlv);
+        serializer.serializeTlv(tlv, buffer);
     }
 }