From 02b812aaa9f43565fb27bd261263fbe10426f080 Mon Sep 17 00:00:00 2001 From: Dana Kutenicsova Date: Tue, 17 Jun 2014 12:21:08 +0200 Subject: [PATCH] BUG-612 : switched PCEP XRO subobject serializers to ByteBuf Change-Id: Ibc07ffa75cb7c38f870f40d92e45b35a9700d59a Signed-off-by: Dana Kutenicsova --- .../AbstractXROWithSubobjectsParser.java | 20 +-------- .../object/PCEPExcludeRouteObjectParser.java | 4 +- .../pcep/impl/object/XROSubobjectUtil.java | 26 ------------ ...ExplicitExclusionRouteSubobjectParser.java | 27 +++--------- .../subobject/XROAsNumberSubobjectParser.java | 24 ++++------- .../XROIpv4PrefixSubobjectParser.java | 41 +++++++------------ .../XROIpv6PrefixSubobjectParser.java | 35 +++++----------- .../XROPathKey128SubobjectParser.java | 23 ++++------- .../XROPathKey32SubobjectParser.java | 22 ++++------ .../subobject/XROSRLGSubobjectParser.java | 33 ++++----------- ...XROUnnumberedInterfaceSubobjectParser.java | 37 ++++++----------- .../pcep/impl/PCEPXROSubobjectParserTest.java | 29 +++++++++---- .../pcep/spi/XROSubobjectRegistry.java | 4 +- .../pcep/spi/XROSubobjectSerializer.java | 4 +- .../protocol/pcep/spi/XROSubobjectUtil.java | 24 +++++++++++ .../spi/pojo/SimpleXROSubobjectRegistry.java | 6 +-- 16 files changed, 136 insertions(+), 223 deletions(-) delete mode 100644 pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/XROSubobjectUtil.java create mode 100644 pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectUtil.java diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java index 25d8bdc371..e1a5f94e1a 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java @@ -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 subobjects) { - final List result = Lists.newArrayList(); - int finalLength = 0; + protected final void serializeSubobject(final List 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; } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExcludeRouteObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExcludeRouteObjectParser.java index 8a6ca0bb99..cff7277677 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExcludeRouteObjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExcludeRouteObjectParser.java @@ -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 index 4a8b6cb3e3..0000000000 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/XROSubobjectUtil.java +++ /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; - } -} diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java index c77fef67e2..31e323990c 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java @@ -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 parseSubobject( @@ -93,26 +94,10 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar return subs; } - private byte[] serializeSubobject( - final List subobjects) { - - final List result = Lists.newArrayList(); - - int finalLength = 0; - + private void serializeSubobject( + final List 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; } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROAsNumberSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROAsNumberSubobjectParser.java index ad3f29a67f..a706adcaf2 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROAsNumberSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROAsNumberSubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java index 4a33f648c4..172e5189cc 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv6PrefixSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv6PrefixSubobjectParser.java index d5b8a01e67..4ef07c9bb9 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv6PrefixSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv6PrefixSubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey128SubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey128SubobjectParser.java index 8c2d853cd8..1af2c28c0a 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey128SubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey128SubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java index 529253c725..4019a00c83 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java index f7e156b037..c424fd6b57 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROUnnumberedInterfaceSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROUnnumberedInterfaceSubobjectParser.java index 2a4e4c2e5a..63f42f0e85 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROUnnumberedInterfaceSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROUnnumberedInterfaceSubobjectParser.java @@ -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); } } diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPXROSubobjectParserTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPXROSubobjectParserTest.java index 79d16f3ebc..0644cedc1d 100644 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPXROSubobjectParserTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPXROSubobjectParserTest.java @@ -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)); } } diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java index 8df7ed902c..f6b28017fa 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java @@ -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); } diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectSerializer.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectSerializer.java index 0b3318e9f0..3be736480f 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectSerializer.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectSerializer.java @@ -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 index 0000000000..ef87644dfa --- /dev/null +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectUtil.java @@ -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); + } +} diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java index 2fd70ca534..083feede9e 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java @@ -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); } } -- 2.36.6