BUG-612 : switched PCEP message serializers to ByteBuf 40/8040/4
authorDana Kutenicsova <dkutenic@cisco.com>
Fri, 13 Jun 2014 20:46:51 +0000 (22:46 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 22 Jun 2014 11:59:04 +0000 (11:59 +0000)
Change-Id: I380427951f0d3d505bd53c7fa0ad286fb226422e
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
57 files changed:
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/crabbe/initiated00/PcinitiateMessageParser.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/Stateful02PCReplyMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCReportMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCRequestMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCUpdateRequestMessageParser.java
pcep/ietf-stateful02/src/test/java/org/opendaylight/protocol/pcep/ietf/PCEPObjectParserTest.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/CInitiated00PCInitiateMessageParser.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/stateful07/Stateful07ErrorMessageParser.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/Stateful07PCReportMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07PCUpdateRequestMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java
pcep/ietf-stateful07/src/test/java/org/opendaylight/protocol/pcep/ietf/PCEPObjectParserTest.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPCloseMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPErrorMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPKeepAliveMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPNotificationMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPOpenMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPReplyMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPRequestMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractBandwidthParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPBandwidthObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPClassTypeObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPCloseObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv4ObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv6ObjectParser.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/PCEPExcludeRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExistingBandwidthObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPGlobalConstraintsObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLoadBalancingObjectParser.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/PCEPMetricObjectParser.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/PCEPObjectiveFunctionObjectParser.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/PCEPPathKeyObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPReportedRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPRequestParameterObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPSvecObjectParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepObjectParserTest.java
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepObjectParserTest.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractMessageParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/MessageSerializer.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/MessageUtil.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectRegistry.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectSerializer.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectUtil.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleObjectRegistry.java
pcep/spi/src/test/java/org/opendaylight/protocol/pcep/spi/UtilsTest.java

index f5804acf6553d01be14fa55fd9b459abd0bacda5..4ea0f233c3eddb7f943e247fd56683ac0eedc8e9 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.crabbe.initiated00;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -45,24 +46,21 @@ public class PcinitiateMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcinitiate)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Needed PcinitiateMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcinitiate, "Wrong instance of Message. Passed instance of %s. Need Pcinitiate.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.PcinitiateMessage init = ((Pcinitiate) message).getPcinitiateMessage();
         ByteBuf buffer = Unpooled.buffer();
         for (final Requests req : init.getRequests()) {
-            buffer.writeBytes(serializeObject(req.getEndpointsObj()));
-            buffer.writeBytes(serializeObject(req.getLspa()));
+            serializeObject(req.getEndpointsObj(), buffer);
+            serializeObject(req.getLspa(), buffer);
             if (req.getEro() != null) {
-                buffer.writeBytes(serializeObject(req.getEro()));
+                serializeObject(req.getEro(), buffer);
             }
             if (req.getBandwidth() != null) {
-                buffer.writeBytes(serializeObject(req.getBandwidth()));
+                serializeObject(req.getBandwidth(), buffer);
             }
             if (req.getMetrics() != null && !req.getMetrics().isEmpty()) {
                 for (final Metrics m : req.getMetrics()) {
-                    buffer.writeBytes(serializeObject(m.getMetric()));
+                    serializeObject(m.getMetric(), buffer);
                 }
             }
         }
index de33d4f42fa5dbd562a7dcd8814504fd47cc0e21..c4319f76d76d0e36b68cb9045c0459cc7a26d7ec 100644 (file)
@@ -10,6 +10,7 @@ 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;
 
@@ -40,18 +41,12 @@ public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
     public static final int TYPE = 1;
 
     /*
-     * offset of TLVs offset of other fields are not defined as constants
-     * because of non-standard mapping of bits
-     */
-    private static final int TLVS_OFFSET = 4;
-
-    /*
-     * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
+     * first 4b are restricted
      */
     private static final int DELEGATE_FLAG_OFFSET = 15;
     private static final int SYNC_FLAG_OFFSET = 14;
-    private static final int REMOVE_FLAG_OFFSET = 12;
     private static final int OPERATIONAL_FLAG_OFFSET = 13;
+    private static final int REMOVE_FLAG_OFFSET = 12;
 
     public Stateful02LspObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -63,7 +58,7 @@ public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
         final LspBuilder builder = new LspBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
-        int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
+        int[] plspIdRaw = { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2) };
         builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << 12) | (plspIdRaw[1] << 4) | (plspIdRaw[2] >> 4))));
         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
         builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
@@ -88,33 +83,32 @@ public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Lsp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
         final Lsp specObj = (Lsp) object;
+        final ByteBuf body = Unpooled.buffer();
+        final PlspId plsp = specObj.getPlspId();
+        Preconditions.checkArgument(plsp != null, "PLSP-ID not present");
+        body.writeMedium(plsp.getValue().intValue() << 4);
 
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
-        final int lspID = specObj.getPlspId().getValue().intValue();
-        retBytes[0] = (byte) (lspID >> 12);
-        retBytes[1] = (byte) (lspID >> 4);
-        retBytes[2] = (byte) (lspID << 4);
+        BitSet flags = new BitSet(2 * Byte.SIZE);
         if (specObj.isDelegate() != null && specObj.isDelegate()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
+            flags.set(DELEGATE_FLAG_OFFSET);
         }
         if (specObj.isRemove() != null && specObj.isRemove()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
+            flags.set(REMOVE_FLAG_OFFSET);
         }
         if (specObj.isSync() != null && specObj.isSync()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
+            flags.set(SYNC_FLAG_OFFSET);
         }
         if (specObj.isOperational() != null && specObj.isOperational()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
+            flags.set(OPERATIONAL_FLAG_OFFSET);
         }
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        body.writeByte(ByteArray.bitSetToBytes(flags, 2)[1]);
+        //FIXME: switch to ByteBuf
+        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+        body.writeBytes(tlvs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 893e3a4f1426b8fdc064c1f41054f4dae7fb7980..a0249b8246d85a50c1fe49ab19e9aa38f4d13d04 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -50,10 +51,7 @@ public final class Stateful02PCReplyMessageParser extends PCEPReplyMessageParser
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcrep)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Nedded PcrepMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
         if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
             throw new IllegalArgumentException("Replies cannot be null or empty.");
@@ -63,48 +61,48 @@ public final class Stateful02PCReplyMessageParser extends PCEPReplyMessageParser
             if (reply.getRp() == null) {
                 throw new IllegalArgumentException("Reply must contain RP object.");
             }
-            buffer.writeBytes(serializeObject(reply.getRp()));
+            serializeObject(reply.getRp(), buffer);
             if (reply.getAugmentation(Replies1.class) != null && reply.getAugmentation(Replies1.class).getLsp() != null) {
-                buffer.writeBytes(serializeObject(reply.getAugmentation(Replies1.class).getLsp()));
+                serializeObject(reply.getAugmentation(Replies1.class).getLsp(), buffer);
             }
             if (reply.getResult() != null) {
                 if (reply.getResult() instanceof FailureCase) {
                     final FailureCase f = ((FailureCase) reply.getResult());
-                    buffer.writeBytes(serializeObject(f.getNoPath()));
+                    serializeObject(f.getNoPath(), buffer);
                     if (f.getLspa() != null) {
-                        buffer.writeBytes(serializeObject(f.getLspa()));
+                        serializeObject(f.getLspa(), buffer);
                     }
                     if (f.getBandwidth() != null) {
-                        buffer.writeBytes(serializeObject(f.getBandwidth()));
+                        serializeObject(f.getBandwidth(), buffer);
                     }
                     if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
                         for (final Metrics m : f.getMetrics()) {
-                            buffer.writeBytes(serializeObject(m.getMetric()));
+                            serializeObject(m.getMetric(), buffer);
                         }
                     }
                     if (f.getIro() != null) {
-                        buffer.writeBytes(serializeObject(f.getIro()));
+                        serializeObject(f.getIro(), buffer);
                     }
                 } else {
                     final SuccessCase s = (SuccessCase) reply.getResult();
                     for (final Paths p : s.getSuccess().getPaths()) {
-                        buffer.writeBytes(serializeObject(p.getEro()));
+                        serializeObject(p.getEro(), buffer);
                         if (p.getLspa() != null) {
-                            buffer.writeBytes(serializeObject(p.getLspa()));
+                            serializeObject(p.getLspa(), buffer);
                         }
                         if (p.getOf() != null) {
-                            buffer.writeBytes(serializeObject(p.getOf()));
+                            serializeObject(p.getOf(), buffer);
                         }
                         if (p.getBandwidth() != null) {
-                            buffer.writeBytes(serializeObject(p.getBandwidth()));
+                            serializeObject(p.getBandwidth(), buffer);
                         }
                         if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                             for (final Metrics m : p.getMetrics()) {
-                                buffer.writeBytes(serializeObject(m.getMetric()));
+                                serializeObject(m.getMetric(), buffer);
                             }
                         }
                         if (p.getIro() != null) {
-                            buffer.writeBytes(serializeObject(p.getIro()));
+                            serializeObject(p.getIro(), buffer);
                         }
                     }
                 }
index cd611e9fd8aeb1c32a655e6375aeeb7ddfaaad41..74a15c7a9e1038c298fd3feed744998e21eacc40 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -50,33 +51,30 @@ public final class Stateful02PCReportMessageParser extends AbstractMessageParser
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcrpt)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Nedded PcrptMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcrpt, "Wrong instance of Message. Passed instance of %s. Need Pcrpt.", message.getClass());
         final Pcrpt msg = (Pcrpt) message;
         final List<Reports> reports = msg.getPcrptMessage().getReports();
         ByteBuf buffer = Unpooled.buffer();
         for (final Reports report : reports) {
-            buffer.writeBytes(serializeObject(report.getLsp()));
+            serializeObject(report.getLsp(), buffer);
             final Path p = report.getPath();
             if (p != null) {
                 if (p.getEro() != null) {
-                    buffer.writeBytes(serializeObject(p.getEro()));
+                    serializeObject(p.getEro(), buffer);
                 }
                 if (p.getLspa() != null) {
-                    buffer.writeBytes(serializeObject(p.getLspa()));
+                    serializeObject(p.getLspa(), buffer);
                 }
                 if (p.getBandwidth() != null) {
-                    buffer.writeBytes(serializeObject(p.getBandwidth()));
+                    serializeObject(p.getBandwidth(), buffer);
                 }
                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                     for (final Metrics m : p.getMetrics()) {
-                        buffer.writeBytes(serializeObject(m.getMetric()));
+                        serializeObject(m.getMetric(), buffer);
                     }
                 }
                 if (p.getIro() != null) {
-                    buffer.writeBytes(serializeObject(p.getRro()));
+                    serializeObject(p.getRro(), buffer);
                 }
             }
         }
index 17deccee572501eb94fc4caa2b7ac4d6e92c34be..09ce406ad69a1fbd0256e2ff28b53a72820927c9 100644 (file)
@@ -53,48 +53,48 @@ public final class Stateful02PCRequestMessageParser extends PCEPRequestMessagePa
     @Override
     protected void serializeP2P(final ByteBuf buffer, final P2p p2p) {
         if (p2p.getEndpointsObj() != null) {
-            buffer.writeBytes(serializeObject(p2p.getEndpointsObj()));
+            serializeObject(p2p.getEndpointsObj(), buffer);
         }
         if (p2p.getAugmentation(P2p1.class) != null && p2p.getAugmentation(P2p1.class).getLsp() != null) {
-            buffer.writeBytes(serializeObject(p2p.getAugmentation(P2p1.class).getLsp()));
+            serializeObject(p2p.getAugmentation(P2p1.class).getLsp(), buffer);
         }
         if (p2p.getReportedRoute() != null) {
             final ReportedRoute rr = p2p.getReportedRoute();
             if (rr.getRro() != null) {
-                buffer.writeBytes(serializeObject(rr.getRro()));
+                serializeObject(rr.getRro(), buffer);
             }
             if (rr.getBandwidth() != null) {
-                buffer.writeBytes(serializeObject(rr.getBandwidth()));
+                serializeObject(rr.getBandwidth(), buffer);
             }
         }
         if (p2p.getLoadBalancing() != null) {
-            buffer.writeBytes(serializeObject(p2p.getLoadBalancing()));
+            serializeObject(p2p.getLoadBalancing(), buffer);
         }
         if (p2p.getLspa() != null) {
-            buffer.writeBytes(serializeObject(p2p.getLspa()));
+            serializeObject(p2p.getLspa(), buffer);
         }
         if (p2p.getBandwidth() != null) {
-            buffer.writeBytes(serializeObject(p2p.getBandwidth()));
+            serializeObject(p2p.getBandwidth(), buffer);
         }
         if (p2p.getMetrics() != null) {
             for (final Metrics m : p2p.getMetrics()) {
-                buffer.writeBytes(serializeObject(m.getMetric()));
+                serializeObject(m.getMetric(), buffer);
             }
         }
         if (p2p.getIro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getIro()));
+            serializeObject(p2p.getIro(), buffer);
         }
         if (p2p.getRro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getRro()));
+            serializeObject(p2p.getRro(), buffer);
         }
         if (p2p.getXro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getXro()));
+            serializeObject(p2p.getXro(), buffer);
         }
         if (p2p.getOf() != null) {
-            buffer.writeBytes(serializeObject(p2p.getOf()));
+            serializeObject(p2p.getOf(), buffer);
         }
         if (p2p.getClassType() != null) {
-            buffer.writeBytes(serializeObject(p2p.getClassType()));
+            serializeObject(p2p.getClassType(), buffer);
         }
     }
 
index d4b070417c3a885335485c2779a5e8b424434447..04e2e33df2b5545e708072e7dd6e7da4488156d2 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -50,31 +51,28 @@ public final class Stateful02PCUpdateRequestMessageParser extends AbstractMessag
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcupd)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
-                    + ". Nedded PcupdMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
         final Pcupd msg = (Pcupd) message;
         final List<Updates> updates = msg.getPcupdMessage().getUpdates();
         ByteBuf buffer = Unpooled.buffer();
         for (final Updates update : updates) {
-            buffer.writeBytes(serializeObject(update.getLsp()));
+            serializeObject(update.getLsp(), buffer);
             final Path p = update.getPath();
             if (p != null) {
-                buffer.writeBytes(serializeObject(p.getEro()));
+                serializeObject(p.getEro(), buffer);
                 if (p.getLspa() != null) {
-                    buffer.writeBytes(serializeObject(p.getLspa()));
+                    serializeObject(p.getLspa(), buffer);
                 }
                 if (p.getBandwidth() != null) {
-                    buffer.writeBytes(serializeObject(p.getBandwidth()));
+                    serializeObject(p.getBandwidth(), buffer);
                 }
                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                     for (final Metrics m : p.getMetrics()) {
-                        buffer.writeBytes(serializeObject(m.getMetric()));
+                        serializeObject(m.getMetric(), buffer);
                     }
                 }
                 if (p.getIro() != null) {
-                    buffer.writeBytes(serializeObject(p.getIro()));
+                    serializeObject(p.getIro(), buffer);
                 }
             }
         }
index 14a4572e330486f8ad773940ac5d40e71324c740..99bff2d5d485df593e2c56480ebac55327bb60a7 100644 (file)
@@ -78,7 +78,9 @@ public class PCEPObjectParserTest {
                 Tlvs2.class, statBuilder.build()).addAugmentation(Tlvs1.class, cleanupBuilder.build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -114,7 +116,9 @@ public class PCEPObjectParserTest {
                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class).getSymbolicPathName());
         // assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
         // 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -136,6 +140,8 @@ public class PCEPObjectParserTest {
         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.TlvsBuilder().setSymbolicPathName(
                 tlv2).build());
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 }
index e240076b6d1aac94ca8f9f78cfcc59969e12ec5e..8abce5982e0a45ac9367137351c8b586141f9bc0 100644 (file)
@@ -10,6 +10,7 @@ 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;
 
@@ -65,15 +66,14 @@ public class CInitiated00LspObjectParser extends Stateful07LspObjectParser {
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Lsp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
         final Lsp specObj = (Lsp) object;
+        final ByteBuf body = Unpooled.buffer();
 
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
+        final byte[] retBytes = new byte[4];
 
+        Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
         final int lspID = specObj.getPlspId().getValue().intValue();
         retBytes[0] = (byte) (lspID >> 12);
         retBytes[1] = (byte) (lspID >> 4);
@@ -97,7 +97,10 @@ public class CInitiated00LspObjectParser extends Stateful07LspObjectParser {
             final int op = specObj.getOperational().getIntValue();
             retBytes[3] |= (op & 7) << 4;
         }
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        body.writeBytes(retBytes);
+        // FIXME: switch to ByteBuf
+        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+        body.writeBytes(tlvs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 89955ad4718ff2633abea14002817842c92b6890..34ddac08486ef83596c80832657b6ac96dcff12e 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.initiated00;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -50,34 +51,31 @@ public final class CInitiated00PCInitiateMessageParser extends AbstractMessagePa
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof PcinitiateMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Needed PcinitiateMessage.");
-        }
+        Preconditions.checkArgument(message instanceof PcinitiateMessage, "Wrong instance of Message. Passed instance of %s. Need PcinitiateMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.PcinitiateMessage init = ((Pcinitiate) message).getPcinitiateMessage();
         ByteBuf buffer = Unpooled.buffer();
         for (final Requests req : init.getRequests()) {
-            buffer.writeBytes(serializeObject(req.getSrp()));
-            buffer.writeBytes(serializeObject(req.getLsp()));
+            serializeObject(req.getSrp(), buffer);
+            serializeObject(req.getLsp(), buffer);
             if (req.getEndpointsObj() != null) {
-                buffer.writeBytes(serializeObject(req.getEndpointsObj()));
+                serializeObject(req.getEndpointsObj(), buffer);
             }
             if (req.getEro() != null) {
-                buffer.writeBytes(serializeObject(req.getEro()));
+                serializeObject(req.getEro(), buffer);
             }
             if (req.getLspa() != null) {
-                buffer.writeBytes(serializeObject(req.getLspa()));
+                serializeObject(req.getLspa(), buffer);
             }
             if (req.getBandwidth() != null) {
-                buffer.writeBytes(serializeObject(req.getBandwidth()));
+                serializeObject(req.getBandwidth(), buffer);
             }
             if (req.getMetrics() != null && !req.getMetrics().isEmpty()) {
                 for (final Metrics m : req.getMetrics()) {
-                    buffer.writeBytes(serializeObject(m.getMetric()));
+                    serializeObject(m.getMetric(), buffer);
                 }
             }
             if (req.getIro() != null) {
-                buffer.writeBytes(serializeObject(req.getIro()));
+                serializeObject(req.getIro(), buffer);
             }
         }
         MessageUtil.formatMessage(TYPE, buffer, out);
index 24353f930c8680594eeb13efb77bd35afcdb3ea3..0954f3c55eeb837e5097083b12ecfafb345bcd9e 100644 (file)
@@ -10,6 +10,7 @@ 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;
 
@@ -58,26 +59,19 @@ public class CInitiated00SrpObjectParser extends Stateful07SrpObjectParser {
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Srp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
-        }
+    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 Long id = srp.getOperationId().getValue();
-
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-        if (tlvs != null) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        }
         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());
         }
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_SIZE), retBytes, 0);
-
-        System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_SIZE));
+        body.writeInt(srp.getOperationId().getValue().intValue());
+        body.writeBytes(tlvs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index ebcaafcc8dd4b6c660e4ba13dd43a12fc2e3f384..9bc6763760e8695ae9c3348221f55725df2e4d20 100644 (file)
@@ -67,23 +67,20 @@ public final class Stateful07ErrorMessageParser extends PCEPErrorMessageParser {
         if (err.getErrorType() instanceof RequestCase) {
             final List<Rps> rps = ((RequestCase) err.getErrorType()).getRequest().getRps();
             for (final Rps r : rps) {
-                buffer.writeBytes(serializeObject(r.getRp()));
+                serializeObject(r.getRp(), buffer);
             }
         }
-
         if (err.getErrorType() instanceof StatefulCase) {
             final List<Srps> srps = ((StatefulCase) err.getErrorType()).getStateful().getSrps();
             for (final Srps s : srps) {
-                buffer.writeBytes(serializeObject(s.getSrp()));
+                serializeObject(s.getSrp(), buffer);
             }
         }
-
         for (final Errors e : err.getErrors()) {
-            buffer.writeBytes(serializeObject(e.getErrorObject()));
+            serializeObject(e.getErrorObject(), buffer);
         }
-
         if (err.getErrorType() instanceof SessionCase) {
-            buffer.writeBytes(serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen()));
+            serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen(), buffer);
         }
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
index 229dec34b47a4a83e0f5cfb946c58f36d9339b9c..e1ebd35f76b07eeed1e5b8330c75b86e4b27022d 100644 (file)
@@ -8,8 +8,12 @@
 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.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
@@ -98,14 +102,12 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Lsp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.", object.getClass());
         final Lsp specObj = (Lsp) object;
+        final ByteBuf body = Unpooled.buffer();
 
-        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
+        final byte[] retBytes = new byte[4];
 
         Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
         final int lspID = specObj.getPlspId().getValue().intValue();
@@ -128,8 +130,11 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
             final int op = specObj.getOperational().getIntValue();
             retBytes[3] |= (op & 7) << 4;
         }
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        body.writeBytes(retBytes);
+        // FIXME: switch to ByteBuf
+        final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+        body.writeBytes(tlvs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 5ec9cc4ee0d7b0e1f884e74fd3945112ff480bc0..9e47f20610f9f9e9da0e29dcd8ed549ae9f6be1f 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -52,37 +53,34 @@ public final class Stateful07PCReportMessageParser extends AbstractMessageParser
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcrpt)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Nedded PcrptMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcrpt, "Wrong instance of Message. Passed instance of %s. Need Pcrpt.", message.getClass());
         final Pcrpt msg = (Pcrpt) message;
         final List<Reports> reports = msg.getPcrptMessage().getReports();
         ByteBuf buffer = Unpooled.buffer();
         for (final Reports report : reports) {
             if (report.getSrp() != null) {
-                buffer.writeBytes(serializeObject(report.getSrp()));
+                serializeObject(report.getSrp(), buffer);
             }
-            buffer.writeBytes(serializeObject(report.getLsp()));
+            serializeObject(report.getLsp(), buffer);
             final Path p = report.getPath();
             if (p != null) {
-                buffer.writeBytes(serializeObject(p.getEro()));
+                serializeObject(p.getEro(), buffer);
                 if (p.getLspa() != null) {
-                    buffer.writeBytes(serializeObject(p.getLspa()));
+                    serializeObject(p.getLspa(), buffer);
                 }
                 if (p.getBandwidth() != null) {
-                    buffer.writeBytes(serializeObject(p.getBandwidth()));
+                    serializeObject(p.getBandwidth(), buffer);
                 }
                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                     for (final Metrics m : p.getMetrics()) {
-                        buffer.writeBytes(serializeObject(m.getMetric()));
+                        serializeObject(m.getMetric(), buffer);
                     }
                 }
                 if (p.getIro() != null) {
-                    buffer.writeBytes(serializeObject(p.getIro()));
+                    serializeObject(p.getIro(), buffer);
                 }
                 if (p.getRro() != null) {
-                    buffer.writeBytes(serializeObject(p.getRro()));
+                    serializeObject(p.getRro(), buffer);
                 }
             }
         }
index 3d187dd338e98c5a1a026678e739688c9b69c67a..2fb4c4bdd646b1554a89ed83764588b6da2f41d4 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -51,32 +52,29 @@ public final class Stateful07PCUpdateRequestMessageParser extends AbstractMessag
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcupd)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
-                    + ". Nedded PcupdMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
         final Pcupd msg = (Pcupd) message;
         final List<Updates> updates = msg.getPcupdMessage().getUpdates();
         ByteBuf buffer = Unpooled.buffer();
         for (final Updates update : updates) {
-            buffer.writeBytes(serializeObject(update.getSrp()));
-            buffer.writeBytes(serializeObject(update.getLsp()));
+            serializeObject(update.getSrp(), buffer);
+            serializeObject(update.getLsp(), buffer);
             final Path p = update.getPath();
             if (p != null) {
-                buffer.writeBytes(serializeObject(p.getEro()));
+                serializeObject(p.getEro(), buffer);
                 if (p.getLspa() != null) {
-                    buffer.writeBytes(serializeObject(p.getLspa()));
+                    serializeObject(p.getLspa(), buffer);
                 }
                 if (p.getBandwidth() != null) {
-                    buffer.writeBytes(serializeObject(p.getBandwidth()));
+                    serializeObject(p.getBandwidth(), buffer);
                 }
                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                     for (final Metrics m : p.getMetrics()) {
-                        buffer.writeBytes(serializeObject(m.getMetric()));
+                        serializeObject(m.getMetric(), buffer);
                     }
                 }
                 if (p.getIro() != null) {
-                    buffer.writeBytes(serializeObject(p.getIro()));
+                    serializeObject(p.getIro(), buffer);
                 }
             }
         }
index b6fb055a66bd4977c5ae7f5bda5a8b28dd8a7cc9..c5e73910c8268f3cd4e75f46483b748696a26733 100644 (file)
@@ -10,12 +10,12 @@ 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.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.SrpIdNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
@@ -73,20 +73,17 @@ public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<SrpB
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Srp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
-        }
+    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());
+        body.writerIndex(body.writerIndex() + FLAGS_SIZE);
         final Long id = srp.getOperationId().getValue();
-        final byte[] retBytes = new byte[MIN_SIZE];
-        if (tlvs != null) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        }
-        System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        body.writeInt(id.intValue());
+        body.writeBytes(tlvs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 781a24361f2eae6e35f24e4444510a347486b233..29f0ae6b9e0cc997903d090039366dd100b82a7d 100644 (file)
@@ -81,7 +81,9 @@ public class PCEPObjectParserTest {
                 Tlvs1.class, statBuilder.build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -106,7 +108,9 @@ public class PCEPObjectParserTest {
         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspErrorCode(
                 tlv1).setSymbolicPathName(tlv2).build());
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -137,7 +141,9 @@ public class PCEPObjectParserTest {
         assertEquals(tlv, ((Lspa) o).getTlvs().getAugmentation(Tlvs2.class).getSymbolicPathName());
         // assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
         // 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -153,6 +159,8 @@ public class PCEPObjectParserTest {
         builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 }
index 0751383bb89f539ea742c4f9b6cbb99bfc9e7589..ab630ad4dc4e79d26fe3f0cf58929bfafdd38fc2 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
@@ -38,17 +40,14 @@ public class PCEPCloseMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof CloseMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Nedded CloseMessage.");
-        }
+        Preconditions.checkArgument(message instanceof CloseMessage, "Wrong instance of Message. Passed instance of %s. Need CloseMessage.", message.getClass());
         final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
 
         if (close.getCClose() == null) {
             throw new IllegalArgumentException("Close Object must be present in Close Message.");
         }
         ByteBuf buffer = Unpooled.buffer();
-        buffer.writeBytes(serializeObject(close.getCClose()));
+        serializeObject(close.getCClose(), buffer);
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
 
index 123a6e3a65236eb2500e89b1eba81e8f290a4310..c82dc64c0801f9608a80e6995775cf0f28677451 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
@@ -51,10 +53,7 @@ public class PCEPErrorMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof PcerrMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass()
-                    + ". Nedded ErrorMessage.");
-        }
+        Preconditions.checkArgument(message instanceof PcerrMessage, "Wrong instance of Message. Passed instance of %s. Need PcerrMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage err = ((PcerrMessage) message).getPcerrMessage();
 
         if (err.getErrors() == null || err.getErrors().isEmpty()) {
@@ -64,15 +63,15 @@ public class PCEPErrorMessageParser extends AbstractMessageParser {
         if (err.getErrorType() instanceof RequestCase) {
             final List<Rps> rps = ((RequestCase) err.getErrorType()).getRequest().getRps();
             for (final Rps r : rps) {
-                buffer.writeBytes(serializeObject(r.getRp()));
+                serializeObject(r.getRp(), buffer);
             }
         }
         for (final Errors e : err.getErrors()) {
-            buffer.writeBytes(serializeObject(e.getErrorObject()));
+            serializeObject(e.getErrorObject(), buffer);
         }
 
         if (err.getErrorType() instanceof SessionCase) {
-            buffer.writeBytes(serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen()));
+            serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen(), buffer);
         }
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
index 50f1ac6a16f2fc1fb22031f9da3208239bedf381..c2590608997bdf4d62547dca67037671d8beff08 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
@@ -35,10 +37,7 @@ public class PCEPKeepAliveMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof KeepaliveMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Need KeepaliveMessage.");
-        }
+        Preconditions.checkArgument(message instanceof KeepaliveMessage, "Wrong instance of Message. Passed instance of %s. Need KeepaliveMessage.", message.getClass());
         MessageUtil.formatMessage(TYPE, Unpooled.EMPTY_BUFFER, out);
     }
 
index 4d0a5dc4721e58569ec19999e1e4e2e5bdcac397..19447a3b9c583a5f0c7653b68f51f3c732dccc59 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -44,24 +45,21 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof PcntfMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Needed PcntfMessage.");
-        }
+        Preconditions.checkArgument(message instanceof PcntfMessage, "Wrong instance of Message. Passed instance of %s. Need PcntfMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
 
         ByteBuf buffer = Unpooled.buffer();
         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
             if (n.getRps() != null && !n.getRps().isEmpty()) {
                 for (final Rps rps : n.getRps()) {
-                    buffer.writeBytes(serializeObject(rps.getRp()));
+                    serializeObject(rps.getRp(), buffer);
                 }
             }
             if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
                 throw new IllegalArgumentException("Message must contain at least one notification object");
             } else {
                 for (final Notifications not : n.getNotifications()) {
-                    buffer.writeBytes(serializeObject(not.getCNotification()));
+                    serializeObject(not.getCNotification(), buffer);
                 }
             }
         }
index fda48f0a008c1c98fa573ce261d5d9ff85165ca4..348eecc9edbc796d77998ea579d8e04f6c531be1 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
@@ -36,16 +38,14 @@ public class PCEPOpenMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof OpenMessage)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Needed OpenMessage.");
-        }
+        Preconditions.checkArgument(message instanceof OpenMessage, "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
 
         if (open.getOpen() == null) {
             throw new IllegalArgumentException("Open Object must be present in Open Message.");
         }
         ByteBuf buffer = Unpooled.buffer();
-        buffer.writeBytes(serializeObject(open.getOpen()));
+        serializeObject(open.getOpen(), buffer);
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
 
index c0cbc7a2713545e5b8aff5d668d332c5cb2588c5..332920c33cef6fe1df8170ed243387ed483b6c27 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -58,10 +59,7 @@ public class PCEPReplyMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcrep)) {
-            throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                    + ". Nedded PcrepMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
         if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
             throw new IllegalArgumentException("Replies cannot be null or empty.");
@@ -71,45 +69,45 @@ public class PCEPReplyMessageParser extends AbstractMessageParser {
             if (reply.getRp() == null) {
                 throw new IllegalArgumentException("Reply must contain RP object.");
             }
-            buffer.writeBytes(serializeObject(reply.getRp()));
+            serializeObject(reply.getRp(), buffer);
             if (reply.getResult() != null) {
                 if (reply.getResult() instanceof FailureCase) {
                     final FailureCase f = ((FailureCase) reply.getResult());
-                    buffer.writeBytes(serializeObject(f.getNoPath()));
+                    serializeObject(f.getNoPath(), buffer);
                     if (f.getLspa() != null) {
-                        buffer.writeBytes(serializeObject(f.getLspa()));
+                        serializeObject(f.getLspa(), buffer);
                     }
                     if (f.getBandwidth() != null) {
-                        buffer.writeBytes(serializeObject(f.getBandwidth()));
+                        serializeObject(f.getBandwidth(), buffer);
                     }
                     if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
                         for (final Metrics m : f.getMetrics()) {
-                            buffer.writeBytes(serializeObject(m.getMetric()));
+                            serializeObject(m.getMetric(), buffer);
                         }
                     }
                     if (f.getIro() != null) {
-                        buffer.writeBytes(serializeObject(f.getIro()));
+                        serializeObject(f.getIro(), buffer);
                     }
                 } else {
                     final SuccessCase s = (SuccessCase) reply.getResult();
                     for (final Paths p : s.getSuccess().getPaths()) {
-                        buffer.writeBytes(serializeObject(p.getEro()));
+                        serializeObject(p.getEro(), buffer);
                         if (p.getLspa() != null) {
-                            buffer.writeBytes(serializeObject(p.getLspa()));
+                            serializeObject(p.getLspa(), buffer);
                         }
                         if (p.getOf() != null) {
-                            buffer.writeBytes(serializeObject(p.getOf()));
+                            serializeObject(p.getOf(), buffer);
                         }
                         if (p.getBandwidth() != null) {
-                            buffer.writeBytes(serializeObject(p.getBandwidth()));
+                            serializeObject(p.getBandwidth(), buffer);
                         }
                         if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
                             for (final Metrics m : p.getMetrics()) {
-                                buffer.writeBytes(serializeObject(m.getMetric()));
+                                serializeObject(m.getMetric(), buffer);
                             }
                         }
                         if (p.getIro() != null) {
-                            buffer.writeBytes(serializeObject(p.getIro()));
+                            serializeObject(p.getIro(), buffer);
                         }
                     }
                 }
index 4b169897f68b977092ab08e0443f0a0faf7f24a5..7fe6ab6fa97f4aafac5d4a89c374f073a6575c32 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 import io.netty.buffer.ByteBuf;
@@ -66,19 +67,16 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
 
     @Override
     public void serializeMessage(final Message message, final ByteBuf out) {
-        if (!(message instanceof Pcreq)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
-                    + ". Needed PcrepMessage.");
-        }
+        Preconditions.checkArgument(message instanceof Pcreq, "Wrong instance of Message. Passed instance of %s. Need Pcreq.", message.getClass());
         final PcreqMessage msg = ((Pcreq) message).getPcreqMessage();
         if (msg.getRequests() == null || msg.getRequests().isEmpty()) {
             throw new IllegalArgumentException("Requests cannot be null or empty.");
         }
         ByteBuf buffer = Unpooled.buffer();
         for (final Requests req : msg.getRequests()) {
-            buffer.writeBytes(serializeObject(req.getRp()));
+            serializeObject(req.getRp(), buffer);
             if (req.getPathKeyExpansion() != null) {
-                buffer.writeBytes(serializeObject(req.getPathKeyExpansion().getPathKey()));
+                serializeObject(req.getPathKeyExpansion().getPathKey(), buffer);
             }
             if (req.getSegmentComputation() != null) {
                 final SegmentComputation sc = req.getSegmentComputation();
@@ -89,19 +87,19 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
         }
         if (msg.getSvec() != null) {
             for (final Svec s : msg.getSvec()) {
-                buffer.writeBytes(serializeObject(s.getSvec()));
+                serializeObject(s.getSvec(), buffer);
                 if (s.getOf() != null) {
-                    buffer.writeBytes(serializeObject(s.getOf()));
+                    serializeObject(s.getOf(), buffer);
                 }
                 if (s.getGc() != null) {
-                    buffer.writeBytes(serializeObject(s.getGc()));
+                    serializeObject(s.getGc(), buffer);
                 }
                 if (s.getXro() != null) {
-                    buffer.writeBytes(serializeObject(s.getXro()));
+                    serializeObject(s.getXro(), buffer);
                 }
                 if (s.getMetric() != null) {
                     for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.Metric m : s.getMetric()) {
-                        buffer.writeBytes(serializeObject(m.getMetric()));
+                        serializeObject(m.getMetric(), buffer);
                     }
                 }
             }
@@ -111,45 +109,45 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
 
     protected void serializeP2P(final ByteBuf buffer, final P2p p2p) {
         if (p2p.getEndpointsObj() != null) {
-            buffer.writeBytes(serializeObject(p2p.getEndpointsObj()));
+            serializeObject(p2p.getEndpointsObj(), buffer);
         }
         if (p2p.getReportedRoute() != null) {
             final ReportedRoute rr = p2p.getReportedRoute();
             if (rr.getRro() != null) {
-                buffer.writeBytes(serializeObject(rr.getRro()));
+                serializeObject(rr.getRro(), buffer);
             }
             if (rr.getBandwidth() != null) {
-                buffer.writeBytes(serializeObject(rr.getBandwidth()));
+                serializeObject(rr.getBandwidth(), buffer);
             }
         }
         if (p2p.getLoadBalancing() != null) {
-            buffer.writeBytes(serializeObject(p2p.getLoadBalancing()));
+            serializeObject(p2p.getLoadBalancing(), buffer);
         }
         if (p2p.getLspa() != null) {
-            buffer.writeBytes(serializeObject(p2p.getLspa()));
+            serializeObject(p2p.getLspa(), buffer);
         }
         if (p2p.getBandwidth() != null) {
-            buffer.writeBytes(serializeObject(p2p.getBandwidth()));
+            serializeObject(p2p.getBandwidth(), buffer);
         }
         if (p2p.getMetrics() != null) {
             for (final Metrics m : p2p.getMetrics()) {
-                buffer.writeBytes(serializeObject(m.getMetric()));
+                serializeObject(m.getMetric(), buffer);
             }
         }
         if (p2p.getIro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getIro()));
+            serializeObject(p2p.getIro(), buffer);
         }
         if (p2p.getRro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getRro()));
+            serializeObject(p2p.getRro(), buffer);
         }
         if (p2p.getXro() != null) {
-            buffer.writeBytes(serializeObject(p2p.getXro()));
+            serializeObject(p2p.getXro(), buffer);
         }
         if (p2p.getOf() != null) {
-            buffer.writeBytes(serializeObject(p2p.getOf()));
+            serializeObject(p2p.getOf(), buffer);
         }
         if (p2p.getClassType() != null) {
-            buffer.writeBytes(serializeObject(p2p.getClassType()));
+            serializeObject(p2p.getClassType(), buffer);
         }
     }
 
@@ -327,7 +325,7 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
                 && builder.getBandwidth() != null
                 && builder.getReportedRoute().getBandwidth().getBandwidth() != new BandwidthBuilder().setBandwidth(
                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(new byte[] { 0 })).build()
-                && builder.getReportedRoute().getRro() == null) {
+                        && builder.getReportedRoute().getRro() == null) {
             errors.add(createErrorMsg(PCEPErrors.RRO_MISSING, rp));
             return null;
         }
index 3e8497026006233c063e18a94227158ae6dc948a..164e018fff574b2f1c1b30831cf6bccc694cd1a6 100644 (file)
@@ -10,11 +10,13 @@ package org.opendaylight.protocol.pcep.impl.object;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.BandwidthBuilder;
@@ -40,4 +42,14 @@ abstract class AbstractBandwidthParser extends AbstractObjectWithTlvsParser<Band
         builder.setBandwidth(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(ByteArray.getAllBytes(bytes)));
         return builder.build();
     }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Bandwidth, "Wrong instance of PCEPObject. Passed %s. Needed BandwidthObject.", object.getClass());
+        final ByteBuf body = Unpooled.buffer();
+        body.writeBytes(((Bandwidth) object).getBandwidth().getValue());
+        formatBandwidth(object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
+
+    protected abstract void formatBandwidth(final boolean processed, final boolean ignored, final ByteBuf body, final ByteBuf buffer);
 }
index d9f0863346147d8925e974ada5df2ece097661eb..38de20a9031666e4a7bcc4194cf8a5f4e9b79527 100644 (file)
@@ -7,15 +7,13 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
 
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
 
 /**
- * Parser for {@link Bandwidth}
+ * Parser for Bandwidth
  */
 public class PCEPBandwidthObjectParser extends AbstractBandwidthParser {
 
@@ -23,18 +21,12 @@ public class PCEPBandwidthObjectParser extends AbstractBandwidthParser {
 
     public static final int TYPE = 1;
 
-    private static final int BANDWIDTH_LENGTH = 4;
-
     public PCEPBandwidthObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Bandwidth)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
-        }
-        final byte[] retBytes = Arrays.copyOf(((Bandwidth) object).getBandwidth().getValue(), BANDWIDTH_LENGTH);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+    protected void formatBandwidth(boolean processed, boolean ignored, ByteBuf body, ByteBuf buffer) {
+        ObjectUtil.formatSubobject(TYPE, CLASS, processed, ignored, body, buffer);
     }
 }
index 3bcedff6dde1a78e774647085a87efea2973603e..b86077d4915ca2f1290e5deebd7a74529a57b178 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
@@ -48,7 +48,7 @@ public class PCEPClassTypeObjectParser extends AbstractObjectWithTlvsParser<Clas
     /**
      * Size of the object in bytes.
      */
-    private static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
+    private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
 
     public PCEPClassTypeObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -81,12 +81,11 @@ public class PCEPClassTypeObjectParser extends AbstractObjectWithTlvsParser<Clas
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof ClassType)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ClasstypeObject.");
-        }
-        final byte[] retBytes = new byte[SIZE];
-        retBytes[SIZE - 1] = UnsignedBytes.checkedCast(((ClassType) object).getClassType().getValue());
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.", object.getClass());
+        final ByteBuf body = Unpooled.buffer(SIZE);
+        body.writeZero(SIZE -1);
+        body.writeByte(((ClassType) object).getClassType().getValue());
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 15b2effbb78269d1fefec750ca996d6ac2c5eafb..46137653d96b196bc870d13e958a4bcdff196cda 100644 (file)
@@ -11,12 +11,12 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose;
@@ -35,19 +35,8 @@ public class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<CCloseBu
     /*
      * lengths of fields in bytes
      */
+    private static final int RESERVED = 2;
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int REASON_F_LENGTH = 1;
-
-    /*
-     * offsets of fields in bytes
-     */
-    private static final int FLAGS_F_OFFSET = 2;
-    private static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-
-    /*
-     * total size of object in bytes
-     */
-    private static final int TLVS_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
 
     public PCEPCloseObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -59,31 +48,25 @@ public class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<CCloseBu
         final CCloseBuilder builder = new CCloseBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
-        bytes.readerIndex(bytes.readerIndex() + REASON_F_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
         builder.setReason((short) UnsignedBytes.toInt(bytes.readByte()));
         parseTlvs(builder, bytes.slice());
         return builder.build();
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof CClose)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed CloseObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.", object.getClass());
         final CClose obj = (CClose) object;
-
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(RESERVED + FLAGS_F_LENGTH);
+        body.writeByte(obj.getReason());
+        // FIXME: switch to ByteBuf
         final byte[] tlvs = serializeTlvs(obj.getTlvs());
-        int tlvsLength = 0;
-        if (tlvs != null) {
-            tlvsLength = tlvs.length;
-        }
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
         if (tlvs != null) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+            body.writeBytes(tlvs);
         }
-        retBytes[REASON_F_OFFSET] = UnsignedBytes.checkedCast(obj.getReason());
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index bf529c6471021be81e43614fbb746004174bedb9..d3fbddd715d92aba1ec35049420d0383217a0720 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 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.spi.AbstractObjectWithTlvsParser;
@@ -38,14 +39,8 @@ public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<
     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
 
     public static final int CLASS = 4;
-    public static final int TYPE = 1;
-
-    /*
-     * fields lengths and offsets for IPv4 in bytes
-     */
 
-    private static final int SRC4_F_OFFSET = 0;
-    private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + Ipv4Util.IP4_LENGTH;
+    public static final int TYPE = 1;
 
     public PCEPEndPointsIpv4ObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -72,20 +67,14 @@ public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof EndpointsObj)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
         final EndpointsObj ePObj = (EndpointsObj) object;
-
         final AddressFamily afi = ePObj.getAddressFamily();
-
-        if (!(afi instanceof Ipv4Case)) {
-            throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
-        }
-        final byte[] retBytes = new byte[Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH];
-        ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
-        ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
+        final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
+        body.writeBytes(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()));
+        body.writeBytes(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()));
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 6dfbadaea8aed2e3b8e4d846aad4bdddce877d41..faf8056a924453ac78e286bc53906fc5ce3d43e7 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 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.spi.AbstractObjectWithTlvsParser;
@@ -38,13 +39,8 @@ public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<
     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv6ObjectParser.class);
 
     public static final int CLASS = 4;
-    public static final int TYPE = 2;
 
-    /*
-     * fields lengths and offsets for IPv6 in bytes
-     */
-    private static final int SRC6_F_OFFSET = 0;
-    private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + Ipv6Util.IPV6_LENGTH;
+    public static final int TYPE = 2;
 
     public PCEPEndPointsIpv6ObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -71,20 +67,14 @@ public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof EndpointsObj)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
         final EndpointsObj ePObj = (EndpointsObj) object;
-
         final AddressFamily afi = ePObj.getAddressFamily();
-
-        if (!(afi instanceof Ipv6Case)) {
-            throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
-        }
-        final byte[] retBytes = new byte[Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH];
-        ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
-        ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        Preconditions.checkArgument(afi instanceof Ipv6Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv6", afi.getClass());
+        final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH);
+        body.writeBytes(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()));
+        body.writeBytes(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()));
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index afadab32cb64badd166f451705e3df6f57bc4413..bb5f371c7d13e9d3415b5d4b5fa78d2e8fee9a9b 100644 (file)
@@ -11,12 +11,12 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -36,13 +36,8 @@ public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObj
     public static final int TYPE = 1;
 
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int ET_F_LENGTH = 1;
-    private static final int EV_F_LENGTH = 1;
 
-    private static final int FLAGS_F_OFFSET = 1;
-    private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-    private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
-    private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
+    private static final int RESERVED  = 1;
 
     public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -54,7 +49,7 @@ public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObj
         final ErrorObjectBuilder builder = new ErrorObjectBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
-        bytes.readerIndex(bytes.readerIndex() + ET_F_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
         builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
         builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
         parseTlvs(builder, bytes.slice());
@@ -69,21 +64,19 @@ public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObj
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof ErrorObject)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
         final ErrorObject errObj = (ErrorObject) object;
-
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(FLAGS_F_LENGTH + RESERVED);
+        body.writeByte(errObj.getType());
+        body.writeByte(errObj.getValue());
+        // FIXME: switch to ByteBuf
         final byte[] tlvs = serializeTlvs(errObj.getTlvs());
-
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
         if (tlvs.length != 0) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+            body.writeBytes(tlvs);
         }
-        retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
-        retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index f4b9c6dac2fc828e619483a99fd601fbdc803c71..8a6ca0bb996a13e941e8fe5391d46e22cd815c40 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
  *
-* This program and the accompanying materials are made available under the
+ * 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
  */
@@ -10,11 +10,11 @@ package org.opendaylight.protocol.pcep.impl.object;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
@@ -49,18 +49,17 @@ public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobject
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Xro)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Xro, "Wrong instance of PCEPObject. Passed %s. Needed XroObject.", object.getClass());
         final Xro obj = (Xro) object;
-        assert !(obj.getSubobject().isEmpty()) : "Empty Excluded Route Object.";
-        final byte[] bytes = serializeSubobject(obj.getSubobject());
-        final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
-        if (obj.getFlags().isFail()) {
-            result[FLAGS_OFFSET] = 1;
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(FLAGS_OFFSET);
+        if (obj.getFlags().isFail() != null) {
+            body.writeBoolean(obj.getFlags().isFail());
         }
-        ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), result);
+        // FIXME: switch to ByteBuf
+        final byte[] bytes = serializeSubobject(obj.getSubobject());
+        body.writeBytes(bytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index b12841e033943ee6e97a7082bbda79b4deb2ccba..c5e349c5e9def80a97902ae929cb55b712119ef2 100644 (file)
@@ -7,15 +7,13 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
 
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
 
 /**
- * Parser for {@link Bandwidth}
+ * Parser for Bandwidth
  */
 public class PCEPExistingBandwidthObjectParser extends AbstractBandwidthParser {
 
@@ -23,18 +21,12 @@ public class PCEPExistingBandwidthObjectParser extends AbstractBandwidthParser {
 
     public static final int TYPE = 2;
 
-    private static final int BANDWIDTH_LENGTH = 4;
-
     public PCEPExistingBandwidthObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Bandwidth)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
-        }
-        final byte[] retBytes = Arrays.copyOf(((Bandwidth) object).getBandwidth().getValue(), BANDWIDTH_LENGTH);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+    protected void formatBandwidth(boolean processed, boolean ignored, ByteBuf body, ByteBuf buffer) {
+        ObjectUtil.formatSubobject(TYPE, CLASS, processed, ignored, body, buffer);
     }
 }
index 16e4bcbc6726fa8ede71cc126fe43a558e3a7975..108f417244d73317c7936f81a63e434a89bdf061 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
@@ -44,15 +45,13 @@ public class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsPars
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Ero)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                    + ". Needed ExplicitRouteObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Ero, "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
         final Ero ero = ((Ero) object);
-
-        assert !(ero.getSubobject().isEmpty()) : "Empty Explicit Route Object.";
-
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(ero.getSubobject()));
+        final ByteBuf body = Unpooled.buffer();
+        // FIXME: switch to ByteBuf
+        final byte[] bytes = serializeSubobject(ero.getSubobject());
+        body.writeBytes(bytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index e65ef6ab700c8ef29b5761d736917bdd741ad119..cbf16002a4b0d1dcd43d535dd0ba312dfee475ec 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
@@ -30,18 +31,6 @@ public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsPar
 
     public static final int TYPE = 1;
 
-    private static final int MAX_HOP_F_LENGTH = 1;
-    private static final int MAX_UTIL_F_LENGTH = 1;
-    private static final int MIN_UTIL_F_LENGTH = 1;
-    private static final int OVER_BOOKING_FACTOR_F_LENGTH = 1;
-
-    private static final int MAX_HOP_F_OFFSET = 0;
-    private static final int MAX_UTIL_F_OFFSET = MAX_HOP_F_OFFSET + MAX_HOP_F_LENGTH;
-    private static final int MIN_UTIL_F_OFFSET = MAX_UTIL_F_OFFSET + MAX_UTIL_F_LENGTH;
-    private static final int OVER_BOOKING_FACTOR_F_OFFSET = MIN_UTIL_F_OFFSET + MIN_UTIL_F_LENGTH;
-
-    private static final int TLVS_OFFSET = OVER_BOOKING_FACTOR_F_OFFSET + OVER_BOOKING_FACTOR_F_LENGTH;
-
     public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
     }
@@ -62,16 +51,14 @@ public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsPar
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Gc)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed GcObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Gc, "Wrong instance of PCEPObject. Passed %s. Needed GcObject.", object.getClass());
         final Gc specObj = (Gc) object;
-        final byte[] retBytes = new byte[TLVS_OFFSET + 0];
-        retBytes[MAX_HOP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxHop());
-        retBytes[MAX_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxUtilization());
-        retBytes[MIN_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMinUtilization());
-        retBytes[OVER_BOOKING_FACTOR_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getOverBookingFactor());
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeByte(specObj.getMaxHop());
+        body.writeByte(specObj.getMaxUtilization());
+        body.writeByte(specObj.getMinUtilization());
+        body.writeByte(specObj.getOverBookingFactor());
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 052bb39e3e43a809db8dbb1666a4ab461a6fd032..9517eaa158896b182c49c0ca7b3285400a27204f 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 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.ArrayList;
 import java.util.List;
@@ -53,20 +53,18 @@ public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParse
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Iro)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed IncludeRouteObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Iro, "Wrong instance of PCEPObject. Passed %s. Needed IroObject.", object.getClass());
         final Iro iro = ((Iro) object);
-
-        assert !(iro.getSubobject().isEmpty()) : "Empty Include Route Object.";
-
-        final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
-
+        final ByteBuf body = Unpooled.buffer();
+        final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = new ArrayList<>();
         for (final Subobject s : iro.getSubobject()) {
             subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
                     false).setSubobjectType(s.getSubobjectType()).build());
         }
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
+        //FIXME: switch to Bytebuf
+        byte[] bytes = serializeSubobject(subs);
+        body.writeBytes(bytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 2caa57788f7d1f82ed8d3700a1f46f06bc71dfe1..5d3bf27b3782da5ae24f3aae483ee0f705e9ad88 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
@@ -32,15 +33,10 @@ public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<
 
     public static final int TYPE = 1;
 
+    private static final int RESERVED = 2;
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int MAX_LSP_F_LENGTH = 1;
-    private static final int MIN_BAND_F_LENGTH = 4;
 
-    private static final int FLAGS_F_OFFSET = 2;
-    private static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-    private static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
-
-    private static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
+    private static final int SIZE = 8;
 
     public PCEPLoadBalancingObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -56,22 +52,20 @@ public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<
         final LoadBalancingBuilder builder = new LoadBalancingBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
-        bytes.readerIndex(bytes.readerIndex() + MAX_LSP_F_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + RESERVED + FLAGS_F_LENGTH);
         builder.setMaxLsp((short) UnsignedBytes.toInt(bytes.readByte()));
-        builder.setMinBandwidth(new Bandwidth(ByteArray.readBytes(bytes, MIN_BAND_F_LENGTH)));
+        builder.setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)));
         return builder.build();
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof LoadBalancing)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                    + ". Needed LoadBalancingObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof LoadBalancing, "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.", object.getClass());
         final LoadBalancing specObj = (LoadBalancing) object;
-        final byte[] retBytes = new byte[SIZE];
-        retBytes[MAX_LSP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxLsp());
-        ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        final ByteBuf body = Unpooled.buffer(SIZE);
+        body.writeZero(RESERVED + FLAGS_F_LENGTH);
+        body.writeByte(specObj.getMaxLsp());
+        body.writeBytes(specObj.getMinBandwidth().getValue());
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 229656936d4b501cf6956e987c258d55d6343c4b..c1d9b5b498d89a5d4c429c2bcadc82b7969c1617 100644 (file)
@@ -11,6 +11,7 @@ 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;
 
@@ -39,11 +40,6 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
     /*
      * lengths of fields in bytes
      */
-    private static final int EXC_ANY_F_LENGTH = 4;
-    private static final int INC_ANY_F_LENGTH = 4;
-    private static final int INC_ALL_F_LENGTH = 4;
-    private static final int SET_PRIO_F_LENGTH = 1;
-    private static final int HOLD_PRIO_F_LENGTH = 1;
     private static final int FLAGS_F_LENGTH = 1;
 
     /*
@@ -51,16 +47,7 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
      */
     private static final int L_FLAG_OFFSET = 7;
 
-    /*
-     * offsets of fields in bytes
-     */
-    private static final int EXC_ANY_F_OFFSET = 0;
-    private static final int INC_ANY_F_OFFSET = EXC_ANY_F_OFFSET + EXC_ANY_F_LENGTH;
-    private static final int INC_ALL_F_OFFSET = INC_ANY_F_OFFSET + INC_ANY_F_LENGTH;
-    private static final int SET_PRIO_F_OFFSET = INC_ALL_F_OFFSET + INC_ALL_F_LENGTH;
-    private static final int HOLD_PRIO_F_OFFSET = SET_PRIO_F_OFFSET + SET_PRIO_F_LENGTH;
-    private static final int FLAGS_F_OFFSET = HOLD_PRIO_F_OFFSET + HOLD_PRIO_F_LENGTH;
-    private static final int TLVS_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH + 1;
+    private static final int RESERVED = 1;
 
     public PCEPLspaObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -82,48 +69,34 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
         final BitSet flags = ByteArray.bytesToBitSet(new byte[] { bytes.readByte() });
         builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
         final TlvsBuilder tbuilder = new TlvsBuilder();
-        bytes.readerIndex(TLVS_F_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + RESERVED);
         parseTlvs(tbuilder, bytes.slice());
         builder.setTlvs(tbuilder.build());
         return builder.build();
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Lspa)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspaObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Lspa, "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
         final Lspa lspaObj = (Lspa) object;
-
-        final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_F_OFFSET + tlvs.length + getPadding(TLVS_F_OFFSET + tlvs.length, PADDED_TO)];
-
-        if (lspaObj.getExcludeAny() != null) {
-            System.arraycopy(ByteArray.longToBytes(lspaObj.getExcludeAny().getValue(), EXC_ANY_F_LENGTH), 0, retBytes, EXC_ANY_F_OFFSET,
-                    EXC_ANY_F_LENGTH);
-        }
-        if (lspaObj.getIncludeAny() != null) {
-            System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAny().getValue(), INC_ANY_F_LENGTH), 0, retBytes, INC_ANY_F_OFFSET,
-                    INC_ANY_F_LENGTH);
-        }
-        if (lspaObj.getIncludeAll() != null) {
-            System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAll().getValue(), INC_ALL_F_LENGTH), 0, retBytes, INC_ALL_F_OFFSET,
-                    INC_ALL_F_LENGTH);
-        }
-        if (lspaObj.getSetupPriority() != null) {
-            retBytes[SET_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getSetupPriority());
-        }
-        if (lspaObj.getHoldPriority() != null) {
-            retBytes[HOLD_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getHoldPriority());
-        }
-
+        final ByteBuf body = Unpooled.buffer();
+        body.writeInt(lspaObj.getExcludeAny().getValue().intValue());
+        body.writeInt(lspaObj.getIncludeAny().getValue().intValue());
+        body.writeInt(lspaObj.getIncludeAll().getValue().intValue());
+        body.writeByte(lspaObj.getSetupPriority());
+        body.writeByte(lspaObj.getHoldPriority());
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        if (lspaObj.isLocalProtectionDesired() != null && lspaObj.isLocalProtectionDesired()) {
+        if (lspaObj.isLocalProtectionDesired() != null) {
             flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
         }
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_F_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        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);
+        }
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 03284cbd6b76c82d253a87ca9ea6f7566e1ffd79..2fd343772d529b7317091e5df7c8d3a852448fda 100644 (file)
@@ -11,6 +11,7 @@ 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;
 
@@ -38,15 +39,12 @@ public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricB
      * lengths of fields in bytes
      */
     private static final int FLAGS_F_LENGTH = 1;
-    private static final int TYPE_F_LENGTH = 1;
     private static final int METRIC_VALUE_F_LENGTH = 4;
 
     /*
      * offsets of fields in bytes
      */
-    private static final int FLAGS_F_OFFSET = 2;
-    private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-    private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
+    private static final int RESERVED = 2;
 
     /*
      * flags offsets inside flags field in bits
@@ -54,7 +52,7 @@ public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricB
     private static final int C_FLAG_OFFSET = 6;
     private static final int B_FLAG_OFFSET = 7;
 
-    private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
+    private static final int SIZE = 4 + METRIC_VALUE_F_LENGTH;
 
     public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -67,7 +65,7 @@ public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricB
             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
                     + ".");
         }
-        bytes.readerIndex(bytes.readerIndex() + FLAGS_F_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + RESERVED);
         final byte[] flagBytes = { bytes.readByte() };
         final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
         final MetricBuilder builder = new MetricBuilder();
@@ -81,18 +79,21 @@ public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricB
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Metric)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.", object.getClass());
         final Metric mObj = (Metric) object;
-        final byte[] retBytes = new byte[SIZE];
+        final ByteBuf body = Unpooled.buffer(SIZE);
+        body.writeZero(RESERVED);
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(C_FLAG_OFFSET, mObj.isComputed());
-        flags.set(B_FLAG_OFFSET, mObj.isBound());
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
-        retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
-        System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        if (mObj.isComputed() != null) {
+            flags.set(C_FLAG_OFFSET, mObj.isComputed());
+        }
+        if (mObj.isBound() != null) {
+            flags.set(B_FLAG_OFFSET, mObj.isBound());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        body.writeByte(mObj.getMetricType());
+        body.writeBytes(mObj.getValue().getValue());
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 6dbf6b0136d1a9f2631a6c47bf4102e8504ed573..fdbc0dc6a0b847c3d3c3e937daa6e11b8d3eaad5 100644 (file)
@@ -11,6 +11,7 @@ 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;
 
@@ -40,18 +41,9 @@ public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathB
     /*
      * lengths of fields in bytes
      */
-    private static final int NI_F_LENGTH = 1;
     private static final int FLAGS_F_LENGTH = 2;
     private static final int RESERVED_F_LENGTH = 1;
 
-    /*
-     * offsets of field in bytes
-     */
-    private static final int NI_F_OFFSET = 0;
-    private static final int FLAGS_F_OFFSET = NI_F_OFFSET + NI_F_LENGTH;
-    private static final int RESERVED_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-    private static final int TLVS_OFFSET = RESERVED_F_OFFSET + RESERVED_F_LENGTH;
-
     /*
      * defined flags
      */
@@ -72,7 +64,7 @@ public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathB
         final byte[] flagsByte = ByteArray.readBytes(bytes, FLAGS_F_LENGTH);
         final BitSet flags = ByteArray.bytesToBitSet(flagsByte);
         builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
-        bytes.readerIndex(TLVS_OFFSET);
+        bytes.readerIndex(bytes.readerIndex() + RESERVED_F_LENGTH);
         parseTlvs(builder, bytes.slice());
         return builder.build();
     }
@@ -85,23 +77,23 @@ public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathB
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof NoPath)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NoPathObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass());
         final NoPath nPObj = (NoPath) object;
-
+        final ByteBuf body = Unpooled.buffer();
+        body.writeByte(nPObj.getNatureOfIssue());
+        final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
+        if (nPObj.isUnsatisfiedConstraints() != null) {
+            flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+        body.writeZero(RESERVED_F_LENGTH);
+        // FIXME: switch to ByteBuf
         final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
         if (tlvs != null) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+            body.writeBytes(tlvs);
         }
-        final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
-        retBytes[NI_F_OFFSET] = UnsignedBytes.checkedCast(nPObj.getNatureOfIssue());
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
-        ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index ad30088657da702a0720e9705039e660fa6ff32f..a922f06e893141c7658ea9190574b41f7fa84c86 100644 (file)
@@ -11,12 +11,12 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -35,20 +35,10 @@ public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<C
 
     public static final int TYPE = 1;
 
-    /*
-     * lengths of fields
-     */
-    private static final int FLAGS_F_LENGTH = 1;
-    private static final int NT_F_LENGTH = 1;
-    private static final int NV_F_LENGTH = 1;
-
     /*
      * offsets of fields
      */
-    private static final int FLAGS_F_OFFSET = 1;
-    private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-    private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
-    private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
+    private static final int NT_F_OFFSET = 2;
 
     public PCEPNotificationObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -75,21 +65,19 @@ public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<C
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof CNotification)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof CNotification, "Wrong instance of PCEPObject. Passed %s. Needed CNotificationObject.", object.getClass());
         final CNotification notObj = (CNotification) object;
-
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(NT_F_OFFSET);
+        body.writeByte(notObj.getType());
+        body.writeByte(notObj.getValue());
+        // FIXME: switch to ByteBuf
         final byte[] tlvs = serializeTlvs(notObj.getTlvs());
-
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
         if (tlvs.length != 0) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+            body.writeBytes(tlvs);
         }
-        retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
-        retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 6bca80a31af32aee611dab4e1ea392cb7ae79569..1dffc2ed7c089ad26bd2761551e44e72e654dee3 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.protocol.pcep.impl.object;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
@@ -30,16 +30,8 @@ public class PCEPObjectiveFunctionObjectParser extends AbstractObjectWithTlvsPar
     public static final int CLASS = 21;
 
     public static final int TYPE = 1;
-    /*
-     * lengths of fields
-     */
-    private static final int OF_CODE_F_LENGTH = 2;
 
-    /*
-     * offsets of fields
-     */
-    private static final int OF_CODE_F_OFFSET = 0;
-    private static final int TLVS_OFFSET = OF_CODE_F_OFFSET + OF_CODE_F_LENGTH + 2;
+    private static final int RESERVED = 2;
 
     public PCEPObjectiveFunctionObjectParser(final TlvRegistry tlvReg) {
         super(tlvReg);
@@ -56,14 +48,12 @@ public class PCEPObjectiveFunctionObjectParser extends AbstractObjectWithTlvsPar
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Of)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                    + ". Needed PCEPObjectiveFunction.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Of, "Wrong instance of PCEPObject. Passed %s. Needed OfObject.", object.getClass());
         final Of specObj = (Of) object;
-        final byte[] retBytes = new byte[TLVS_OFFSET + 0];
-        ByteArray.copyWhole(ByteArray.shortToBytes(specObj.getCode().getValue().shortValue()), retBytes, OF_CODE_F_OFFSET);
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeShort(specObj.getCode().getValue().shortValue());
+        body.writeZero(RESERVED);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index bb7243aa18d3eeef852ebd15f61f93c5fbc5f55c..bf7df42f6e16c3d2f9b40306a52892dc334a74a5 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
@@ -42,28 +43,11 @@ public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
 
     public static final int TYPE = 1;
 
-    /*
-     * lengths of fields in bytes
-     */
-    private static final int VER_FLAGS_MF_LENGTH = 1;
-    private static final int KEEPALIVE_F_LENGTH = 1;
-    private static final int DEAD_TIMER_LENGTH = 1;
-    private static final int SID_F_LENGTH = 1;
-
     /*
      * lengths of subfields inside multi-field in bits
      */
     private static final int VERSION_SF_LENGTH = 3;
 
-    /*
-     * offsets of field in bytes
-     */
-    private static final int VER_FLAGS_MF_OFFSET = 0;
-    private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
-    private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
-    private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
-    private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
-
     /*
      * offsets of subfields inside multi-field in bits
      */
@@ -110,26 +94,17 @@ public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Open)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
         final Open open = (Open) object;
-
-        final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
-
-        final byte[] tlvs = serializeTlvs(open.getTlvs());
-
-        final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
-        bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
-        bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
-        bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
-        bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
-        if (tlvs.length != 0) {
-            ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
-        }
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeByte(PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
+        body.writeByte(open.getKeepalive());
+        body.writeByte(open.getDeadTimer());
+        body.writeByte(open.getSessionId());
+        //FIXME: switch to ByteBuf
+        body.writeBytes(serializeTlvs(open.getTlvs()));
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 00a9f6fcf51fa6e2f29002e0ca2d18ce0ee25eed..fe304658d2cbc7927515579943b8c7d064445e63 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 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.ArrayList;
 import java.util.List;
@@ -57,19 +57,20 @@ public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof PathKey)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PathKeyObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof PathKey, "Wrong instance of PCEPObject. Passed %s. Needed PathKeyObject.", object.getClass());
         final PathKey pkey = (PathKey) object;
+        final ByteBuf body = Unpooled.buffer();
         final List<PathKeys> pk = pkey.getPathKeys();
-        final List<Subobject> subs = Lists.newArrayList();
+        final List<Subobject> subs = new ArrayList<>();
         for (final PathKeys p : pk) {
             subs.add(new SubobjectBuilder().setLoose(p.isLoose()).setSubobjectType(
                     new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder().setPathKey(
                             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKeyBuilder().setPathKey(
                                     p.getPathKey()).setPceId(p.getPceId()).build()).build()).build());
         }
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
+        // FIXME: switch to ByteBuf
+        body.writeBytes(serializeSubobject(subs));
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 8d52581f4757894fe2da288eede139d675e9e52c..482f6eecc995e54d073c652b3b44e8913e33c52b 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
@@ -43,13 +44,13 @@ public class PCEPReportedRouteObjectParser extends AbstractRROWithSubobjectsPars
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Rro)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                    + ". Needed ReportedRouteObject.");
-        }
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Rro, "Wrong instance of PCEPObject. Passed %s. Needed RroObject.", object.getClass());
         final Rro obj = (Rro) object;
-        assert !(obj.getSubobject().isEmpty()) : "Empty Reported Route Object.";
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(obj.getSubobject()));
+        final ByteBuf body = Unpooled.buffer();
+        // FIXME: switch to ByteBuf
+        final byte[] subs = serializeSubobject(obj.getSubobject());
+        body.writeBytes(subs);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 14112c92e0fb8aee4357b2328091cf4cb01c8414..0a62f68b476378cd98a60ee09d0b26a22105b39c 100644 (file)
@@ -9,8 +9,10 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 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;
 
@@ -42,21 +44,12 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
      * lengths of fields in bytes
      */
     private static final int FLAGS_PRI_MF_LENGTH = 4;
-    private static final int RID_F_LENGTH = 4;
 
     /*
      * lengths of subfields inside multi-field in bits
      */
     private static final int FLAGS_SF_LENGTH = 29;
 
-    /*
-     * offsets of field in bytes
-     */
-
-    private static final int FLAGS_PRI_MF_OFFSET = 0;
-    private static final int RID_F_OFFSET = FLAGS_PRI_MF_OFFSET + FLAGS_PRI_MF_LENGTH;
-    private static final int TLVS_OFFSET = RID_F_OFFSET + RID_F_LENGTH;
-
     /*
      * offsets of subfields inside multi-field in bits
      */
@@ -103,15 +96,15 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, FLAGS_PRI_MF_LENGTH));
-        short priority = 0;
-        priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
-        priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
-        priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
 
         final RpBuilder builder = new RpBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
 
+        short priority = 0;
+        priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
+        priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
+        priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
         builder.setPriority(priority);
         builder.setFragmentation(flags.get(F_FLAG_OFFSET));
         builder.setP2mp(flags.get(N_FLAG_OFFSET));
@@ -137,37 +130,53 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Rp)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed RpObject.");
-        }
-        final Rp rPObj = (Rp) object;
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.", object.getClass());
+        final ByteBuf body = Unpooled.buffer();
+        final Rp rpObj = (Rp) object;
         final BitSet flags = new BitSet(FLAGS_PRI_MF_LENGTH * Byte.SIZE);
-
-        flags.set(R_FLAG_OFFSET, rPObj.isReoptimization());
-        flags.set(B_FLAG_OFFSET, rPObj.isBiDirectional());
-        flags.set(O_FLAG_OFFSET, rPObj.isLoose());
-        flags.set(M_FLAG_OFFSET, rPObj.isMakeBeforeBreak());
-        flags.set(D_FLAG_OFFSET, rPObj.isOrder());
-        flags.set(P_FLAG_OFFSET, rPObj.isPathKey());
-        flags.set(S_FLAG_OFFSET, rPObj.isSupplyOf());
-        flags.set(F_FLAG_OFFSET, rPObj.isFragmentation());
-        flags.set(N_FLAG_OFFSET, rPObj.isP2mp());
-        flags.set(E_FLAG_OFFSET, rPObj.isEroCompression());
-
-        flags.set(PRI_SF_OFFSET, (rPObj.getPriority() & 1 << 2) != 0);
-        flags.set(PRI_SF_OFFSET + 1, (rPObj.getPriority() & 1 << 1) != 0);
-        flags.set(PRI_SF_OFFSET + 2, (rPObj.getPriority() & 1) != 0);
-
-        final byte[] tlvs = serializeTlvs(rPObj.getTlvs());
-        final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH), retBytes, FLAGS_PRI_MF_OFFSET);
-        ByteArray.copyWhole(ByteArray.longToBytes(rPObj.getRequestId().getValue(), RID_F_LENGTH), retBytes, RID_F_OFFSET);
+        if (rpObj.isReoptimization() != null) {
+            flags.set(R_FLAG_OFFSET, rpObj.isReoptimization());
+        }
+        if (rpObj.isBiDirectional() != null) {
+            flags.set(B_FLAG_OFFSET, rpObj.isBiDirectional());
+        }
+        if (rpObj.isLoose() != null) {
+            flags.set(O_FLAG_OFFSET, rpObj.isLoose());
+        }
+        if (rpObj.isMakeBeforeBreak() != null) {
+            flags.set(M_FLAG_OFFSET, rpObj.isMakeBeforeBreak());
+        }
+        if (rpObj.isOrder() != null) {
+            flags.set(D_FLAG_OFFSET, rpObj.isOrder());
+        }
+        if (rpObj.isPathKey() != null) {
+            flags.set(P_FLAG_OFFSET, rpObj.isPathKey());
+        }
+        if (rpObj.isSupplyOf() != null) {
+            flags.set(S_FLAG_OFFSET, rpObj.isSupplyOf());
+        }
+        if (rpObj.isFragmentation() != null) {
+            flags.set(F_FLAG_OFFSET, rpObj.isFragmentation());
+        }
+        if (rpObj.isP2mp() != null) {
+            flags.set(N_FLAG_OFFSET, rpObj.isP2mp());
+        }
+        if (rpObj.isEroCompression() != null) {
+            flags.set(E_FLAG_OFFSET, rpObj.isEroCompression());
+        }
+        if (rpObj.getPriority() != null) {
+            final byte[] p = { 0, 0, 0, UnsignedBytes.checkedCast(rpObj.getPriority().shortValue())};
+            flags.or(ByteArray.bytesToBitSet(p));
+        }
+        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) {
-            ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+            body.writeBytes(tlvs);
         }
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
     public byte[] serializeTlvs(final Tlvs tlvs) {
index 083ad76a8fba9d9ddd35b7dad2a147511c03d117..16f26554e969f8dee03cf1eea17ee0dff60bcd3c 100644 (file)
@@ -11,6 +11,7 @@ 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.BitSet;
 import java.util.List;
@@ -39,13 +40,11 @@ public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuild
      * field lengths in bytes
      */
     private static final int FLAGS_F_LENGTH = 3;
-    private static final int REQ_LIST_ITEM_LENGTH = 4;
 
     /*
      * fields offsets in bytes
      */
     private static final int FLAGS_F_OFFSET = 1;
-    private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
 
     /*
      * flags offsets inside flags field in bits
@@ -93,26 +92,28 @@ public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuild
     }
 
     @Override
-    public byte[] serializeObject(final Object object) {
-        if (!(object instanceof Svec)) {
-            throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
-        }
-
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Svec, "Wrong instance of PCEPObject. Passed %s. Needed SvecObject.", object.getClass());
         final Svec svecObj = (Svec) object;
-        final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
-        final List<RequestId> requestIDs = svecObj.getRequestsIds();
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(FLAGS_F_OFFSET);
         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
-        flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
-        flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
-
-        ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
+        if (svecObj.isLinkDiverse() != null) {
+            flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
+        }
+        if (svecObj.isNodeDiverse() != null) {
+            flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
+        }
+        if (svecObj.isSrlgDiverse() != null) {
+            flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
+        }
+        body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
 
+        final List<RequestId> requestIDs = svecObj.getRequestsIds();
+        assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
         for (int i = 0; i < requestIDs.size(); i++) {
-            System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
-                    * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
+            body.writeInt(requestIDs.get(i).getValue().intValue());
         }
-        assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
-        return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
index 277bce5d82cc869ce78b9030dd552597996754af..f028467eac2f3d8df56674de375f366b442f7cc5 100644 (file)
@@ -139,7 +139,9 @@ public class PCEPObjectParserTest {
         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -153,7 +155,9 @@ public class PCEPObjectParserTest {
         builder.setReason((short) 5);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -168,7 +172,9 @@ public class PCEPObjectParserTest {
         builder.setMinBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -182,19 +188,21 @@ public class PCEPObjectParserTest {
         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
                 true).setSubobjectType(
-                new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0xffffL)).build()).build()).build());
+                        new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0xffffL)).build()).build()).build());
         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
                 true).setSubobjectType(
-                new IpPrefixCaseBuilder().setIpPrefix(
-                        new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).build());
+                        new IpPrefixCaseBuilder().setIpPrefix(
+                                new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).build());
         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
                 true).setSubobjectType(
-                new UnnumberedCaseBuilder().setUnnumbered(
-                        new UnnumberedBuilder().setRouterId(0xffffffffL).setInterfaceId(0xffffffffL).build()).build()).build());
+                        new UnnumberedCaseBuilder().setUnnumbered(
+                                new UnnumberedBuilder().setRouterId(0xffffffffL).setInterfaceId(0xffffffffL).build()).build()).build());
         builder.setSubobject(subs);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -222,7 +230,9 @@ public class PCEPObjectParserTest {
         builder.setSubobject(subs);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -240,21 +250,23 @@ public class PCEPObjectParserTest {
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
                                 new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
-                false).build());
+                                        false).build());
         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
                                 new IpPrefix(Ipv6Util.prefixForBytes(ip6PrefixBytes, 22))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
-                false).build());
+                                        false).build());
         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder().setUnnumbered(
                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder().setRouterId(
                                 0x1245678L).setInterfaceId(0x9abcdef0L).build()).build()).setProtectionAvailable(false).setProtectionInUse(
-                false).build());
+                                        false).build());
         builder.setSubobject(subs);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -268,7 +280,9 @@ public class PCEPObjectParserTest {
         builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -282,7 +296,9 @@ public class PCEPObjectParserTest {
         builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -301,7 +317,9 @@ public class PCEPObjectParserTest {
                         Ipv4Util.addressForBytes(destIPBytes)).build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -322,7 +340,9 @@ public class PCEPObjectParserTest {
                         Ipv6Util.addressForBytes(destIPBytes)).build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -337,7 +357,9 @@ public class PCEPObjectParserTest {
         builder.setValue((short) 1);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPErrorObject3.bin"));
 
@@ -346,7 +368,9 @@ public class PCEPObjectParserTest {
         builder.setTlvs(new TlvsBuilder().setReqMissing(new ReqMissingBuilder().setRequestId(new RequestId(0x00001155L)).build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -366,7 +390,9 @@ public class PCEPObjectParserTest {
         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder().build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject2UpperBounds.bin"));
 
@@ -378,7 +404,9 @@ public class PCEPObjectParserTest {
         builder.setLocalProtectionDesired(true);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -395,7 +423,9 @@ public class PCEPObjectParserTest {
         builder.setValue(new Float32(new byte[] { 0, 0, 0, 0 }));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPMetricObject2UpperBounds.bin"));
 
@@ -405,7 +435,9 @@ public class PCEPObjectParserTest {
         builder.setValue(new Float32(new byte[] { (byte) 0x4f, (byte) 0x70, (byte) 0x00, (byte) 0x00 }));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -420,7 +452,9 @@ public class PCEPObjectParserTest {
         builder.setUnsatisfiedConstraints(true);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNoPathObject2WithTLV.bin"));
 
@@ -433,7 +467,9 @@ public class PCEPObjectParserTest {
                 b.build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -448,7 +484,9 @@ public class PCEPObjectParserTest {
         builder.setValue((short) 0xff);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNotificationObject1WithTlv.bin"));
 
@@ -458,7 +496,9 @@ public class PCEPObjectParserTest {
                 new OverloadDurationBuilder().setDuration(0xff0000a2L).build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -483,7 +523,9 @@ public class PCEPObjectParserTest {
         builder.setRequestId(new RequestId(0xdeadbeefL));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPRPObject2.bin"));
 
@@ -499,7 +541,9 @@ public class PCEPObjectParserTest {
                 b.build()).build());
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -516,7 +560,9 @@ public class PCEPObjectParserTest {
         builder.setRequestsIds(Lists.newArrayList(new RequestId(0xFFL)));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
 
         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPSvecObject1_10ReqIDs.bin"));
 
@@ -539,7 +585,9 @@ public class PCEPObjectParserTest {
         builder.setRequestsIds(requestIDs);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -553,7 +601,9 @@ public class PCEPObjectParserTest {
         builder.setClassType(new ClassType((short) 4));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -569,13 +619,15 @@ public class PCEPObjectParserTest {
         subs.add(new SubobjectBuilder().setMandatory(true).setSubobjectType(
                 new IpPrefixCaseBuilder().setIpPrefix(
                         new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("192.168.0.0/16"))).build()).build()).setAttribute(
-                Attribute.Node).build());
+                                Attribute.Node).build());
         subs.add(new SubobjectBuilder().setMandatory(false).setSubobjectType(
                 new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x1234L)).build()).build()).build());
         builder.setSubobject(subs);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -592,7 +644,9 @@ public class PCEPObjectParserTest {
         builder.setPathKeys(list);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -606,7 +660,9 @@ public class PCEPObjectParserTest {
         builder.setCode(new OfId(4));
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -623,7 +679,9 @@ public class PCEPObjectParserTest {
         builder.setOverBookingFactor((short) 99);
 
         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
     }
 
     @Test
index 66a2c8c9df7c425ef337ea025fb53624b6b4f694..4af9f95fc3afaddba063feecb557db5493b04f04 100644 (file)
@@ -10,9 +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.Before;
 import org.junit.Test;
 import org.opendaylight.protocol.pcep.segment.routing02.SegmentRoutingActivator;
@@ -20,6 +20,7 @@ import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
@@ -82,7 +83,9 @@ public class PcepObjectParserTest {
         final ByteBuf result = Unpooled.wrappedBuffer(rpObjectWithPstTlvBytes);
         assertEquals(builder.build(),
                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(rpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(rpObjectWithPstTlvBytes,ByteArray.getAllBytes(buf));
     }
 
     @Test
@@ -93,14 +96,15 @@ public class PcepObjectParserTest {
         builder.setIgnore(false);
         builder.setOperationId(new SrpIdNumber(1L));
         builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
-        builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder()
-                .addAugmentation(Tlvs8.class,
-                        new Tlvs8Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build())
+        builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder().addAugmentation(Tlvs8.class,
+                new Tlvs8Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build())
                 .build());
 
         final ByteBuf result = Unpooled.wrappedBuffer(srpObjectWithPstTlvBytes);
         assertEquals(builder.build(),
                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(srpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(srpObjectWithPstTlvBytes,ByteArray.getAllBytes(buf));
     }
 }
index b9e8907b583e68ca9a6c3da9800479f1bd2fe3da..d3197551e3193a841543927271ae490f32671db3 100644 (file)
@@ -10,15 +10,16 @@ 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.Before;
 import org.junit.Test;
 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+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.SrPceCapabilityBuilder;
@@ -47,7 +48,7 @@ public class PcepObjectParserTest {
 
     @Test
     public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
-        final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(tlvRegistry);
+        final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(this.tlvRegistry);
 
         final OpenBuilder builder = new OpenBuilder();
         builder.setProcessingRule(false);
@@ -64,7 +65,9 @@ public class PcepObjectParserTest {
         final ByteBuf result = Unpooled.wrappedBuffer(openObjectBytes);
         assertEquals(builder.build(),
                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
-        assertArrayEquals(openObjectBytes, parser.serializeObject(builder.build()));
+        ByteBuf buf = Unpooled.buffer();
+        parser.serializeObject(builder.build(), buf);
+        assertArrayEquals(openObjectBytes,ByteArray.getAllBytes(buf));
     }
 
 }
index ae6215f8e6983a550bd50ec5008c0c0dc81af33b..4b1907a2e06a911873709bc8fff00c83dd5da31e 100644 (file)
@@ -54,11 +54,11 @@ public abstract class AbstractMessageParser implements MessageParser, MessageSer
         this.registry = Preconditions.checkNotNull(registry);
     }
 
-    protected byte[] serializeObject(final Object object) {
+    protected void serializeObject(final Object object, final ByteBuf buffer) {
         if (object == null) {
-            return new byte[] {};
+            return;
         }
-        return this.registry.serializeObject(object);
+        this.registry.serializeObject(object, buffer);
     }
 
     private List<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
@@ -112,9 +112,9 @@ public abstract class AbstractMessageParser implements MessageParser, MessageSer
                 new PcerrMessageBuilder().setErrorType(
                         new RequestCaseBuilder().setRequest(
                                 new RequestBuilder().setRps(Lists.newArrayList(new RpsBuilder().setRp(rp).build())).build()).build()).setErrors(
-                        Arrays.asList(new ErrorsBuilder().setErrorObject(
-                                new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).getType()).setValue(
-                                        maping.getFromErrorsEnum(e).getValue()).build()).build())).build()).build();
+                                        Arrays.asList(new ErrorsBuilder().setErrorObject(
+                                                new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).getType()).setValue(
+                                                        maping.getFromErrorsEnum(e).getValue()).build()).build())).build()).build();
     }
 
     protected abstract Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException;
index a4a1a950b5c9258353b3784de2a57bc99902ad7e..50fa8103c820d9df876e343e48a71380bd7bed61 100644 (file)
@@ -13,5 +13,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 
 public interface MessageSerializer {
 
+    /**
+     * Serializes given message to bytes wrapped in given ByteBuf.
+     * @param message PCEP message to be serialized
+     * @param buffer ByteBuf wrapper around serialized message
+     */
     void serializeMessage(Message message, ByteBuf buffer);
 }
index 5a0b04b5c2f27e99c3cbc216efb092473144556c..de1d380a226398ca5323019b56d383808056162a 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.pcep.spi;
 
 import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
 
@@ -21,13 +20,11 @@ public final class MessageUtil {
     }
 
     public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
-        final int msgLength = body.readableBytes();
-        final byte[] header = new byte[] { UnsignedBytes.checkedCast(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)),
-            UnsignedBytes.checkedCast(messageType),
-            UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) / 256),
-            UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) % 256) };
-        Preconditions.checkState(header.length == PCEPMessageConstants.COMMON_HEADER_LENGTH);
-        out.writeBytes(header);
+        final int msgLength = body.writerIndex();
+        out.writeByte(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
+        out.writeByte(messageType);
+        out.writeShort(msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH);
+        Preconditions.checkState(out.writerIndex() == PCEPMessageConstants.COMMON_HEADER_LENGTH);
         out.writeBytes(body);
     }
 }
index b226e144b149fcedfcfc71a2bd0cb9f8e333eacf..69889bc7cfd4b9492f89605ac785615f0705a5de 100644 (file)
@@ -29,7 +29,7 @@ public interface ObjectRegistry {
      * Find serializer for given object. Delegates parsing to found serializer.
      *
      * @param object to be parsed
-     * @return null if the serializer for this object could not be found
+     * @param buffer ByteBuf wrapped aroung bytes representing given object
      */
-    byte[] serializeObject(final Object object);
+    void serializeObject(final Object object, final ByteBuf buffer);
 }
index 11cdce58d812aa680d0fa5ef0ca28517a987e990..7b38d004c6ccfabf90e1ed82626731341668822f 100644 (file)
@@ -7,9 +7,16 @@
  */
 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.Object;
 
 public interface ObjectSerializer {
 
-    byte[] serializeObject(Object object);
+    /**
+     * Serializes given object to bytes wrapped in given ByteBuf.
+     * @param message PCEP object to be serialized
+     * @param buffer ByteBuf wrapper around serialized object
+     */
+    void serializeObject(final Object object, final ByteBuf buffer);
 }
index 3f954c714da450d012c867822c1461c3b6a8183a..7545a126b9e59bde076294c3c060993fc1c8688d 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
-import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
+
+import java.util.BitSet;
 
 import org.opendaylight.protocol.util.ByteArray;
 
@@ -15,17 +17,9 @@ public final class ObjectUtil {
 
     private static final int HEADER_SIZE = 4;
 
-    private static final int OC_F_LENGTH = 1;
-    private static final int OT_FLAGS_MF_LENGTH = 1;
-    private static final int OBJ_LENGTH_F_LENGTH = 2;
-
-    private static final int OC_F_OFFSET = 0;
-    private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
-    private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
-
     private static final int OT_SF_LENGTH = 4;
     /*
-     * flags offsets inside multi-filed
+     * flags offsets inside multi-field
      */
     private static final int P_FLAG_OFFSET = 6;
     private static final int I_FLAG_OFFSET = 7;
@@ -33,27 +27,20 @@ public final class ObjectUtil {
     private ObjectUtil() {
     }
 
-    public static byte[] formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore,
-            final byte[] valueBytes) {
-
-        final byte[] bytes = new byte[HEADER_SIZE + valueBytes.length];
-
-        // objClass
-        bytes[0] = UnsignedBytes.checkedCast(objectClass);
-
-        // objType_flags multi-field
-        bytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(objectType << (Byte.SIZE - OT_SF_LENGTH));
-        if (processingRule != null && processingRule) {
-            bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
-        }
+    public static void formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore,
+            final ByteBuf body, final ByteBuf out) {
+        out.writeByte(objectClass);
+        BitSet flags = new BitSet(Byte.SIZE);
         if (ignore != null && ignore) {
-            bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
+            flags.set(I_FLAG_OFFSET);
         }
-
-        // objLength
-        System.arraycopy(ByteArray.intToBytes(valueBytes.length + HEADER_SIZE, OBJ_LENGTH_F_LENGTH), 0, bytes, OBJ_LENGTH_F_OFFSET,
-                OBJ_LENGTH_F_LENGTH);
-        System.arraycopy(valueBytes, 0, bytes, HEADER_SIZE, valueBytes.length);
-        return bytes;
+        if (processingRule != null && processingRule) {
+            flags.set(P_FLAG_OFFSET);
+        }
+        byte[] flagB = ByteArray.bitSetToBytes(flags, 1);
+        int typeByte = objectType << OT_SF_LENGTH | flagB[0];
+        out.writeByte(typeByte);
+        out.writeShort(body.writerIndex() + HEADER_SIZE);
+        out.writeBytes(body);
     }
 }
index da7110d8f04db7bf1c7b1db794539c1c5f9f0aa9..7a85ecb536ddb1a2c8131cdeee0657f594f32791 100644 (file)
@@ -68,11 +68,11 @@ public final class SimpleObjectRegistry implements ObjectRegistry {
     }
 
     @Override
-    public byte[] serializeObject(Object object) {
+    public void serializeObject(final Object object, final ByteBuf buffer) {
         final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
         if (serializer == null) {
-            return null;
+            return;
         }
-        return serializer.serializeObject(object);
+        serializer.serializeObject(object, buffer);
     }
 }
index b1b2eb99a805a070cb026afd565e273a349538a9..00cf6a1a9bc06eb650541c57f7e4f5daab1df0a2 100644 (file)
@@ -18,13 +18,13 @@ public class UtilsTest {
 
     @Test
     public void testLabelUtil() {
-        byte[] expected = new byte[] { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
+        byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
         assertArrayEquals(expected, LabelUtil.formatLabel(4, true, true, new byte[] { 1, 2, 3, 4 }));
     }
 
     @Test
     public void testMessageUtil() {
-        byte[] expected = new byte[] { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+        byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
         ByteBuf out = Unpooled.buffer();
         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
         MessageUtil.formatMessage(8, body, out);
@@ -33,7 +33,19 @@ public class UtilsTest {
 
     @Test
     public void testObjectUtil() {
-        byte[] expected = new byte[] { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
-        assertArrayEquals(expected, ObjectUtil.formatSubobject(1, 8, true, true, new byte[] { 1, 2 }));
+        byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
+        ByteBuf out = Unpooled.buffer();
+        ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
+        ObjectUtil.formatSubobject(1, 8, true, true, body, out);
+        assertArrayEquals(expected, ByteArray.getAllBytes(out));
+    }
+
+    @Test
+    public void testObjectUtilFalse() {
+        byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
+        ByteBuf out = Unpooled.buffer();
+        ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
+        ObjectUtil.formatSubobject(1, 8, false, false, body, out);
+        assertArrayEquals(expected, ByteArray.getAllBytes(out));
     }
 }