BUG-612 : switched PCEP XRO subobject serializers to ByteBuf 65/8065/4
authorDana Kutenicsova <dkutenic@cisco.com>
Tue, 17 Jun 2014 10:21:08 +0000 (12:21 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 22 Jun 2014 11:59:35 +0000 (11:59 +0000)
Change-Id: Ibc07ffa75cb7c38f870f40d92e45b35a9700d59a
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
16 files changed:
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.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/XROSubobjectUtil.java [deleted file]
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROAsNumberSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv6PrefixSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey128SubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROUnnumberedInterfaceSubobjectParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPXROSubobjectParserTest.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectSerializer.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectUtil.java [new file with mode: 0644]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java

index 25d8bdc3712c44cfdd6abd802290a875d3129bfb..e1a5f94e1a35a2dbfa7c60b67a8d5091698cf8a6 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
 
 import io.netty.buffer.ByteBuf;
@@ -61,24 +60,9 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O
         return subs;
     }
 
-    protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
-        final List<byte[]> result = Lists.newArrayList();
-        int finalLength = 0;
+    protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
         for (final Subobject subobject : subobjects) {
-            final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
-            if (bytes == null) {
-                LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
-            } else {
-                finalLength += bytes.length;
-                result.add(bytes);
-            }
-        }
-        final byte[] resultBytes = new byte[finalLength];
-        int byteOffset = 0;
-        for (final byte[] b : result) {
-            System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
-            byteOffset += b.length;
+            this.subobjReg.serializeSubobject(subobject, buffer);
         }
-        return resultBytes;
     }
 }
index 8a6ca0bb996a13e941e8fe5391d46e22cd815c40..cff7277677d8525497a61692c0db72422e285da6 100644 (file)
@@ -57,9 +57,7 @@ public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobject
         if (obj.getFlags().isFail() != null) {
             body.writeBoolean(obj.getFlags().isFail());
         }
-        // FIXME: switch to ByteBuf
-        final byte[] bytes = serializeSubobject(obj.getSubobject());
-        body.writeBytes(bytes);
+        serializeSubobject(obj.getSubobject(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 }
diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/XROSubobjectUtil.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/XROSubobjectUtil.java
deleted file mode 100644 (file)
index 4a8b6cb..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.pcep.impl.object;
-
-import com.google.common.primitives.UnsignedBytes;
-
-public final class XROSubobjectUtil {
-
-    private static final int HEADER_SIZE = 2;
-
-    private XROSubobjectUtil() {
-    }
-
-    public static byte[] formatSubobject(final int type, final boolean mandatory, final byte[] value) {
-        final byte[] bytes = new byte[HEADER_SIZE + value.length];
-        bytes[0] = (byte) (UnsignedBytes.checkedCast(type) | (mandatory ? 1 << 7 : 0));
-        bytes[1] = UnsignedBytes.checkedCast(value.length + HEADER_SIZE);
-        System.arraycopy(value, 0, bytes, HEADER_SIZE, value.length);
-        return bytes;
-    }
-}
index c77fef67e28476201e37f1f0ebcf3295cd513260..31e323990c3c2ba70fc287cd70596f879a4c9461 100644 (file)
@@ -71,8 +71,9 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
             b.setSubobjectType(ex.getSubobjectType());
             list.add(b.build());
         }
-        //FIXME: switch to ByteBuf
-        EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), Unpooled.copiedBuffer(serializeSubobject(list)), buffer);
+        final ByteBuf body = Unpooled.buffer();
+        serializeSubobject(list, body);
+        EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
     }
 
     private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> parseSubobject(
@@ -93,26 +94,10 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
         return subs;
     }
 
-    private byte[] serializeSubobject(
-            final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects) {
-
-        final List<byte[]> result = Lists.newArrayList();
-
-        int finalLength = 0;
-
+    private void serializeSubobject(
+            final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects, final ByteBuf body) {
         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
-
-            final byte[] bytes = this.registry.serializeSubobject(subobject);
-            finalLength += bytes.length;
-            result.add(bytes);
-        }
-
-        final byte[] resultBytes = new byte[finalLength];
-        int byteOffset = 0;
-        for (final byte[] b : result) {
-            System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
-            byteOffset += b.length;
+            this.registry.serializeSubobject(subobject, body);
         }
-        return resultBytes;
     }
 }
index ad3f29a67fa0941260d6a500361d543f1d4d9911..a706adcaf2440ab0fb058628e779899228c01820 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
@@ -31,11 +31,7 @@ public class XROAsNumberSubobjectParser implements XROSubobjectParser, XROSubobj
 
     public static final int TYPE = 32;
 
-    private static final int AS_NUMBER_LENGTH = 2;
-
-    private static final int AS_NUMBER_OFFSET = 0;
-
-    private static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
+    private static final int CONTENT_LENGTH = 2;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -50,15 +46,9 @@ public class XROAsNumberSubobjectParser implements XROSubobjectParser, XROSubobj
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
-        if (!(subobject.getSubobjectType() instanceof AsNumberCase)) {
-            throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                    + ". Needed AsNumberCase.");
-        }
-        final byte[] retBytes = new byte[CONTENT_LENGTH];
-        final AsNumberSubobject obj = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
-        System.arraycopy(ByteArray.longToBytes(obj.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
-                AS_NUMBER_LENGTH);
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof AsNumberCase, "Unknown subobject instance. Passed %s. Needed AsNumberCase.", subobject.getSubobjectType().getClass());
+        final AsNumberSubobject s = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), Unpooled.copyShort(s.getAsNumber().getValue().shortValue()), buffer);
     }
 }
index 4a33f648c45ef5d48b6cceb268a7268b2e0dc808..172e5189ccb5cb6eaec0d74adab6cc07c27e101d 100644 (file)
@@ -11,12 +11,13 @@ 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.concepts.Ipv4Util;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
@@ -34,15 +35,11 @@ public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubo
 
     public static final int TYPE = 1;
 
-    private static final int IP4_F_LENGTH = 4;
     private static final int PREFIX_F_LENGTH = 1;
-    private static final int ATTRIBUTE_LENGTH = 1;
 
-    private static final int IP_F_OFFSET = 0;
-    private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
-    private static final int ATTRIBUTE4_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
+    private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
 
-    private static final int CONTENT4_LENGTH = ATTRIBUTE4_OFFSET + ATTRIBUTE_LENGTH;
+    private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX_F_LENGTH + 1;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -54,7 +51,7 @@ public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubo
         }
         final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
         IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
-                IP4_F_LENGTH), length)));
+                Ipv4Util.IP4_LENGTH), length)));
         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
         buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
         builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
@@ -62,26 +59,18 @@ public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubo
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
-        if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
-            throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                    + ". Needed IpPrefixCase.");
-        }
-
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
         final IpPrefix prefix = specObj.getIpPrefix();
-
-        if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
-            throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
-        }
-
-        if (prefix.getIpv4Prefix() == null) {
-            return new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
+        Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
+        if (prefix.getIpv6Prefix() != null) {
+            new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
+        } else {
+            final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
+            body.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
+            body.writeByte(subobject.getAttribute().getIntValue());
+            XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
         }
-        final byte[] retBytes = new byte[CONTENT4_LENGTH];
-        ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
-        retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
-        retBytes[ATTRIBUTE4_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
     }
 }
index d5b8a01e67558e75bfb551a66f8135f0a684de79..4ef07c9bb9f45b83ebb66e9d687ede02f6c8682a 100644 (file)
@@ -11,13 +11,13 @@ 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.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
@@ -36,15 +36,10 @@ public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubo
     public static final int TYPE = 2;
 
     private static final int PREFIX_F_LENGTH = 1;
-    private static final int ATTRIBUTE_LENGTH = 1;
 
-    private static final int IP_F_OFFSET = 0;
+    private static final int PREFIX6_F_OFFSET = Ipv6Util.IPV6_LENGTH;
 
-    private static final int IP6_F_LENGTH = 16;
-    private static final int PREFIX6_F_OFFSET = IP_F_OFFSET + IP6_F_LENGTH;
-    private static final int ATTRIBUTE6_OFFSET = PREFIX6_F_OFFSET + PREFIX_F_LENGTH;
-
-    private static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
+    private static final int CONTENT6_LENGTH = PREFIX6_F_OFFSET + PREFIX_F_LENGTH + 1;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -56,7 +51,7 @@ public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubo
         }
         final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX6_F_OFFSET));
         IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer,
-                IP6_F_LENGTH), length)));
+                Ipv6Util.IPV6_LENGTH), length)));
         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
         buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
         builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
@@ -64,21 +59,13 @@ public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubo
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
-        if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
-            throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                    + ". Needed IpPrefixCase.");
-        }
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
         final IpPrefix prefix = specObj.getIpPrefix();
-
-        if (prefix.getIpv6Prefix() == null) {
-            throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
-        }
-        final byte[] retBytes = new byte[CONTENT6_LENGTH];
-        ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
-        retBytes[PREFIX6_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
-        retBytes[ATTRIBUTE6_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+        final ByteBuf body = Unpooled.buffer(CONTENT6_LENGTH);
+        body.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
+        body.writeByte(subobject.getAttribute().getIntValue());
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
     }
 }
index 8c2d853cd8b3c4722140d4fd364052c3a0b8afda..1af2c28c0a06f9d6cee6e10f437293a8ace3798d 100644 (file)
@@ -10,11 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
@@ -31,14 +32,9 @@ public class XROPathKey128SubobjectParser implements XROSubobjectParser, XROSubo
 
     public static final int TYPE = 65;
 
-    private static final int PK_F_LENGTH = 2;
-
     private static final int PCE128_ID_F_LENGTH = 16;
 
-    private static final int PK_F_OFFSET = 0;
-    private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
-
-    private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
+    private static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -59,13 +55,12 @@ public class XROPathKey128SubobjectParser implements XROSubobjectParser, XROSubo
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
-        final int pathKey = pk.getPathKey().getValue();
-        final byte[] pceId = pk.getPceId().getBinary();
-        final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
-        System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
-        System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeShort(pk.getPathKey().getValue().shortValue());
+        body.writeBytes(pk.getPceId().getBinary());
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
     }
 }
index 529253c725a7c6c33d2995fd5c0530f5153d3d66..4019a00c83478e34a86b191197a7723c6068d172 100644 (file)
@@ -10,11 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import com.google.common.base.Preconditions;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
@@ -31,13 +32,9 @@ public class XROPathKey32SubobjectParser implements XROSubobjectParser, XROSubob
 
     public static final int TYPE = 64;
 
-    private static final int PK_F_LENGTH = 2;
     private static final int PCE_ID_F_LENGTH = 4;
 
-    private static final int PK_F_OFFSET = 0;
-    private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
-
-    private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
+    private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -58,13 +55,12 @@ public class XROPathKey32SubobjectParser implements XROSubobjectParser, XROSubob
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
-        final int pathKey = pk.getPathKey().getValue();
-        final byte[] pceId = pk.getPceId().getBinary();
-        final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
-        System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
-        System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+        final ByteBuf body = Unpooled.buffer();
+        body.writeShort(pk.getPathKey().getValue().shortValue());
+        body.writeBytes(pk.getPceId().getBinary());
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
     }
 }
index f7e156b03764ce9e3aac9b2841e1eab22dd41e2c..c424fd6b57c0801c25540b2bdbea0a495576e227 100644 (file)
@@ -8,15 +8,14 @@
 package org.opendaylight.protocol.pcep.impl.subobject;
 
 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.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
@@ -33,13 +32,7 @@ public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectS
 
     public static final int TYPE = 34;
 
-    private static final int SRLG_ID_NUMBER_LENGTH = 4;
-    private static final int ATTRIBUTE_LENGTH = 1;
-
-    private static final int SRLG_ID_NUMBER_OFFSET = 0;
-    private static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH;
-
-    private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH;
+    private static final int CONTENT_LENGTH = 5;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -48,7 +41,6 @@ public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectS
             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
                     + CONTENT_LENGTH + ".");
         }
-
         final SubobjectBuilder builder = new SubobjectBuilder();
         builder.setMandatory(mandatory);
         builder.setAttribute(Attribute.Srlg);
@@ -57,19 +49,12 @@ public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectS
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
-        if (!(subobject.getSubobjectType() instanceof SrlgCase)) {
-            throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                    + ". Needed SrlgCase.");
-        }
-
-        byte[] retBytes;
-        retBytes = new byte[CONTENT_LENGTH];
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
-
-        ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue(), SRLG_ID_NUMBER_LENGTH), retBytes, SRLG_ID_NUMBER_OFFSET);
-        retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
-
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        body.writeInt(specObj.getSrlgId().getValue().intValue());
+        body.writeByte(subobject.getAttribute().getIntValue());
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
     }
 }
index 2a4e4c2e5a352b6bfa9f750e5db25d53a7618370..63f42f0e850feb794b14a928bbaf65bbaab8aab4 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.impl.object.XROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
@@ -32,15 +32,9 @@ public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser
 
     public static final int TYPE = 4;
 
-    private static final int ATTRIBUTE_LENGTH = 1;
-    private static final int ROUTER_ID_NUMBER_LENGTH = 4;
-    private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
+    private static final int RESERVED = 1;
 
-    private static final int ATTRIBUTE_OFFSET = 1;
-    private static final int ROUTER_ID_NUMBER_OFFSET = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
-    private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
-
-    private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
+    private static final int CONTENT_LENGTH = 10;
 
     @Override
     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
@@ -49,7 +43,7 @@ public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser
             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
                     + CONTENT_LENGTH + ".");
         }
-        buffer.readerIndex(buffer.readerIndex() + ATTRIBUTE_OFFSET);
+        buffer.readerIndex(buffer.readerIndex() + RESERVED);
         final SubobjectBuilder builder = new SubobjectBuilder();
         builder.setMandatory(mandatory);
         builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
@@ -61,19 +55,14 @@ public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser
     }
 
     @Override
-    public byte[] serializeSubobject(final Subobject subobject) {
-        if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
-            throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                    + ". Needed UnnumberedCase.");
-        }
-
-        final byte[] retBytes = new byte[CONTENT_LENGTH];
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
-
-        retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
-        ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
-        System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
-                INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
-        return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        body.writeZero(RESERVED);
+        body.writeByte(subobject.getAttribute().getIntValue());
+        body.writeInt(specObj.getRouterId().intValue());
+        body.writeInt(specObj.getInterfaceId().intValue());
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
     }
 }
index 79d16f3ebcc9d8d3835884d277e0d1c1ab5f696f..0644cedc1df90fa5b71be058e31ea7b54d93b8b8 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.impl;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 
 import org.junit.Test;
@@ -67,7 +68,9 @@ public class PCEPXROSubobjectParserTest {
         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false));
-        assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -82,7 +85,9 @@ public class PCEPXROSubobjectParserTest {
                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true));
-        assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -93,7 +98,9 @@ public class PCEPXROSubobjectParserTest {
         subs.setAttribute(Attribute.Srlg);
         subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true));
-        assertArrayEquals(srlgBytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(srlgBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -105,7 +112,9 @@ public class PCEPXROSubobjectParserTest {
         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
-        assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -115,7 +124,9 @@ public class PCEPXROSubobjectParserTest {
         subs.setMandatory(true);
         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
-        assertArrayEquals(asNumberBytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -128,7 +139,9 @@ public class PCEPXROSubobjectParserTest {
         pBuilder.setPathKey(new PathKey(4660));
         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
-        assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
     }
 
     @Test
@@ -142,6 +155,8 @@ public class PCEPXROSubobjectParserTest {
         pBuilder.setPathKey(new PathKey(4660));
         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
-        assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build()));
+        final ByteBuf buff = Unpooled.buffer();
+        parser.serializeSubobject(subs.build(), buff);
+        assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
     }
 }
index 8df7ed902c87422108478465434b032c91fd3f26..f6b28017fa2017dc1fe1e52b6ec013535005b547 100644 (file)
@@ -27,7 +27,7 @@ public interface XROSubobjectRegistry {
      * Find serializer for given subobject. Delegates parsing to found serializer.
      *
      * @param subobject to be parsed
-     * @return null if the serializer for this subobject could not be found
+     * @param buffer buffer where the serialized subobject will be parsed
      */
-    byte[] serializeSubobject(final Subobject subobject);
+    void serializeSubobject(final Subobject subobject, final ByteBuf buffer);
 }
index 0b3318e9f0964597b926d55f95ce68ee5157fa33..3be736480fdbb240e14c79e520d1555eddb717e4 100644 (file)
@@ -7,9 +7,11 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 
 public interface XROSubobjectSerializer {
 
-    byte[] serializeSubobject(Subobject subobject);
+    void serializeSubobject(final Subobject subobject, final ByteBuf buffer);
 }
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectUtil.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectUtil.java
new file mode 100644 (file)
index 0000000..ef87644
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.pcep.spi;
+
+import io.netty.buffer.ByteBuf;
+
+public final class XROSubobjectUtil {
+
+    private static final int HEADER_SIZE = 2;
+
+    private XROSubobjectUtil() {
+    }
+
+    public static void formatSubobject(final int type, final boolean mandatory, final ByteBuf body, final ByteBuf buffer) {
+        buffer.writeByte(type | (mandatory ? 1 << 7 : 0));
+        buffer.writeByte(body.writerIndex() + HEADER_SIZE);
+        buffer.writeBytes(body);
+    }
+}
index 2fd70ca53457722845930e6c63a8f09766373200..083feede9e488bd725f16fd816c4100ae0c70c40 100644 (file)
@@ -45,11 +45,11 @@ public final class SimpleXROSubobjectRegistry implements XROSubobjectRegistry {
     }
 
     @Override
-    public byte[] serializeSubobject(Subobject subobject) {
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
         final XROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
         if (serializer == null) {
-            return null;
+            return;
         }
-        return serializer.serializeSubobject(subobject);
+        serializer.serializeSubobject(subobject, buffer);
     }
 }