From bb1a5b0139b814666b85de460a89604bea5db0ec Mon Sep 17 00:00:00 2001 From: Dana Kutenicsova Date: Sun, 1 Jun 2014 23:14:44 +0200 Subject: [PATCH] BUG-612 : switched ERO to ByteBuf. Change-Id: Ifb376786285f8dc0abce2405dcdadb9f1f7d0883 Signed-off-by: Dana Kutenicsova --- .../AbstractEROWithSubobjectsParser.java | 54 ++++++----------- .../object/PCEPExplicitRouteObjectParser.java | 8 +-- .../object/PCEPIncludeRouteObjectParser.java | 4 +- .../impl/object/PCEPPathKeyObjectParser.java | 4 +- .../subobject/EROAsNumberSubobjectParser.java | 17 +++--- ...ExplicitExclusionRouteSubobjectParser.java | 59 +++++++------------ .../EROIpv4PrefixSubobjectParser.java | 20 +++---- .../EROIpv6PrefixSubobjectParser.java | 20 +++---- .../subobject/EROLabelSubobjectParser.java | 22 ++++--- .../EROPathKey128SubobjectParser.java | 20 +++---- .../EROPathKey32SubobjectParser.java | 20 +++---- ...EROUnnumberedInterfaceSubobjectParser.java | 20 +++---- .../pcep/impl/PCEPEROSubobjectParserTest.java | 33 ++++++----- .../protocol/pcep/spi/EROSubobjectParser.java | 4 +- .../pcep/spi/EROSubobjectRegistry.java | 6 +- .../spi/pojo/SimpleEROSubobjectRegistry.java | 4 +- 16 files changed, 139 insertions(+), 176 deletions(-) diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java index 5fd58c8f82..38a471151a 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java @@ -7,30 +7,29 @@ */ package org.opendaylight.protocol.pcep.impl.object; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; import java.util.List; import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; import org.opendaylight.protocol.pcep.spi.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.primitives.UnsignedBytes; public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer { private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class); - private static final int SUB_TYPE_FLAG_F_LENGTH = 1; - private static final int SUB_LENGTH_F_LENGTH = 1; - - private static final int TYPE_FLAG_F_OFFSET = 0; - private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH; - private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH; + private static final int HEADER_LENGTH = 2; private final EROSubobjectRegistry subobjReg; @@ -38,43 +37,26 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O this.subobjReg = Preconditions.checkNotNull(subobjReg); } - protected List parseSubobjects(final byte[] bytes) throws PCEPDeserializerException { - if (bytes == null) { - throw new IllegalArgumentException("Byte array is mandatory."); - } - - boolean loose = false; - int type; - byte[] soContentsBytes; - int length; - int offset = 0; - - final List subs = Lists.newArrayList(); - - while (offset < bytes.length) { - - loose = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false; - length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH)); - - type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7); - - if (length > bytes.length - offset) { + protected List parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + final List subs = new ArrayList<>(); + while (buffer.isReadable()) { + boolean loose = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false; + int type = (buffer.readByte() & 0xff) & ~(1 << 7); + int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH; + if (length > buffer.readableBytes()) { throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " - + (bytes.length - offset)); + + buffer.readableBytes()); } - - soContentsBytes = new byte[length - SO_CONTENTS_OFFSET]; - System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET); - - LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes)); - final Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, loose); + LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer)); + final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), loose); if (sub == null) { LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } - offset += length; + buffer.readerIndex(buffer.readerIndex() + length); } return subs; } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java index 98ffd68d4e..4c06b9ca1e 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java @@ -12,7 +12,6 @@ import io.netty.buffer.ByteBuf; import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; import org.opendaylight.protocol.pcep.spi.ObjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.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.explicit.route.object.Ero; @@ -34,13 +33,12 @@ public class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsPars } @Override - public Ero 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."); + public Ero parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final EroBuilder builder = new EroBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); - //FIXME: switch to ByteBuf - builder.setSubobject(parseSubobjects(ByteArray.readAllBytes(bytes))); + builder.setSubobject(parseSubobjects(buffer)); return builder.build(); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java index 67c0079b21..0196394ee2 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java @@ -15,7 +15,6 @@ import java.util.List; import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; import org.opendaylight.protocol.pcep.spi.ObjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.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.include.route.object.Iro; @@ -46,8 +45,7 @@ public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParse builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); final List subs = new ArrayList<>(); - //FIXME: switch to ByteBuf - for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject s : parseSubobjects(ByteArray.readAllBytes(bytes))) { + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject s : parseSubobjects(bytes)) { subs.add(new SubobjectBuilder().setSubobjectType(s.getSubobjectType()).build()); } builder.setSubobject(subs); diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPPathKeyObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPPathKeyObjectParser.java index 6c753f10a4..57e85947bb 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPPathKeyObjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPPathKeyObjectParser.java @@ -15,7 +15,6 @@ import java.util.List; import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; import org.opendaylight.protocol.pcep.spi.ObjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.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.explicit.route.object.ero.Subobject; @@ -48,8 +47,7 @@ public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser { builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); final List pk = new ArrayList<>(); - //FIXME: switch to ByteBuf - final List subs = parseSubobjects(ByteArray.readAllBytes(bytes)); + final List subs = parseSubobjects(bytes); for (final Subobject s : subs) { final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCase k = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCase) s.getSubobjectType(); pk.add(new PathKeysBuilder().setLoose(s.isLoose()).setPceId(k.getPathKey().getPceId()).setPathKey(k.getPathKey().getPathKey()).build()); diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROAsNumberSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROAsNumberSubobjectParser.java index ecc83b7aa7..a3b3a2bcee 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROAsNumberSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROAsNumberSubobjectParser.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer; @@ -20,6 +22,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder; +import com.google.common.base.Preconditions; + /** * Parser for {@link AsNumberCase} */ @@ -34,18 +38,15 @@ public class EROAsNumberSubobjectParser implements EROSubobjectParser, EROSubobj public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - if (buffer.length != CONTENT_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: " + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + if (buffer.readableBytes() != CONTENT_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + CONTENT_LENGTH + "."); } - return new SubobjectBuilder().setLoose(loose).setSubobjectType( new AsNumberCaseBuilder().setAsNumber( - new AsNumberBuilder().setAsNumber(new AsNumber(ByteArray.bytesToLong(buffer))).build()).build()).build(); + new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build()).build(); } @Override 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 88cc7e5880..682a5f87e6 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 @@ -7,6 +7,9 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + +import java.util.ArrayList; import java.util.List; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; @@ -22,18 +25,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.primitives.UnsignedBytes; public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer { public static final int TYPE = 33; - private static final int SUB_TYPE_FLAG_F_LENGTH = 1; - private static final int SUB_LENGTH_F_LENGTH = 1; - - private static final int TYPE_FLAG_F_OFFSET = 0; - private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH; - private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH; + private static final int HEADER_LENGTH = 2; private final XROSubobjectRegistry registry; @@ -42,10 +42,8 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar } @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose); final List list = parseSubobject(buffer); @@ -68,7 +66,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar + ". Needed Exrs."); } final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs(); - final List list = Lists.newArrayList(); + final List list = new ArrayList<>(); for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs ex : e.getExrs()) { final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder(); b.setAttribute(ex.getAttribute()); @@ -80,36 +78,19 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar } private List parseSubobject( - final byte[] bytes) throws PCEPDeserializerException { - if (bytes == null) { - throw new IllegalArgumentException("Byte array is mandatory."); - } - - int type; - - byte[] soContentsBytes; - int length; - int offset = 0; - - final List subs = Lists.newArrayList(); - - while (offset < bytes.length) { - - length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH)); - - final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false; - type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7); - if (length > bytes.length - offset) { + final ByteBuf buffer) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + final List subs = new ArrayList<>(); + while (buffer.isReadable()) { + final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false; + int type = (buffer.readByte() & 0xff) & ~(1 << 7); + int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH; + if (length > buffer.readableBytes()) { throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " - + (bytes.length - offset)); + + buffer.readableBytes()); } - - soContentsBytes = new byte[length - SO_CONTENTS_OFFSET]; - System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET); - - subs.add(this.registry.parseSubobject(type, soContentsBytes, mandatory)); - - offset += length; + //FIXME: switch to ByteBuf + subs.add(this.registry.parseSubobject(type, ByteArray.readBytes(buffer, length), mandatory)); } return subs; } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv4PrefixSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv4PrefixSubobjectParser.java index 54cebdb135..3ca7d96041 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv4PrefixSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv4PrefixSubobjectParser.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.concepts.Ipv4Util; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; @@ -21,6 +23,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder; +import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; /** @@ -38,19 +41,16 @@ public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubo private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose); - - if (buffer.length != CONTENT4_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";"); + if (buffer.readableBytes() != CONTENT4_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } - final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]); - builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix( - new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build()).build()); + 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))); + builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); return builder.build(); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv6PrefixSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv6PrefixSubobjectParser.java index 764fbff595..0736fcf2c0 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv6PrefixSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROIpv6PrefixSubobjectParser.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.concepts.Ipv4Util; import org.opendaylight.protocol.concepts.Ipv6Util; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; @@ -22,6 +24,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder; +import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; /** @@ -40,19 +43,16 @@ public class EROIpv6PrefixSubobjectParser implements EROSubobjectParser, EROSubo private static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose); - - if (buffer.length != CONTENT_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";"); + if (buffer.readableBytes() != CONTENT_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } - final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]); - builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix( - new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build()).build()); + final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX_F_OFFSET)); + IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, IP_F_LENGTH), length))); + builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());; return builder.build(); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROLabelSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROLabelSubobjectParser.java index 8261b951fc..a817874bf2 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROLabelSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROLabelSubobjectParser.java @@ -7,7 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; -import java.util.Arrays; +import io.netty.buffer.ByteBuf; + import java.util.BitSet; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; @@ -50,20 +51,17 @@ public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobject } @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - if (buffer.length < HEADER_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >" + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + if (buffer.readableBytes() < HEADER_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + HEADER_LENGTH + "."); } - final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, RES_F_OFFSET, RES_F_LENGTH)); - - final short cType = (short) UnsignedBytes.toInt(buffer[C_TYPE_F_OFFSET]); - - final LabelType labelType = this.registry.parseLabel(cType, ByteArray.cutBytes(buffer, HEADER_LENGTH)); + final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH)); + final short cType = (short) UnsignedBytes.toInt(buffer.readByte()); + //FIXME: switch to ByteBuf + final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer)); if (labelType == null) { throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey128SubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey128SubobjectParser.java index 6646fb32f5..a57134a3ef 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey128SubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey128SubobjectParser.java @@ -7,7 +7,7 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; -import java.util.Arrays; +import io.netty.buffer.ByteBuf; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; @@ -22,6 +22,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder; import 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; +import com.google.common.base.Preconditions; + /** * Parser for {@link PathKey} */ @@ -39,21 +41,19 @@ public class EROPathKey128SubobjectParser implements EROSubobjectParser, EROSubo private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - if (buffer.length != CONTENT128_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >" + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + if (buffer.readableBytes() != CONTENT128_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + CONTENT128_LENGTH + "."); } - final byte[] pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH); - final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET)); + final int pathKey = buffer.readUnsignedShort(); + final byte[] pceId = ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH); final SubobjectBuilder builder = new SubobjectBuilder(); - builder.setLoose(loose); final PathKeyBuilder pBuilder = new PathKeyBuilder(); pBuilder.setPceId(new PceId(pceId)); pBuilder.setPathKey(new PathKey(pathKey)); + builder.setLoose(loose); builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build()); return builder.build(); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey32SubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey32SubobjectParser.java index 5225aac6e9..4780090bc1 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey32SubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROPathKey32SubobjectParser.java @@ -7,7 +7,7 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; -import java.util.Arrays; +import io.netty.buffer.ByteBuf; import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; @@ -22,6 +22,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder; import 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; +import com.google.common.base.Preconditions; + /** * Parser for {@link PathKey} */ @@ -38,21 +40,19 @@ public class EROPathKey32SubobjectParser implements EROSubobjectParser, EROSubob private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - if (buffer.length != CONTENT_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >" + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + if (buffer.readableBytes() != CONTENT_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + CONTENT_LENGTH + "."); } - final byte[] pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT_LENGTH); - final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET)); + final int pathKey = buffer.readUnsignedShort(); + final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH); final SubobjectBuilder builder = new SubobjectBuilder(); - builder.setLoose(loose); final PathKeyBuilder pBuilder = new PathKeyBuilder(); pBuilder.setPceId(new PceId(pceId)); pBuilder.setPathKey(new PathKey(pathKey)); + builder.setLoose(loose); builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build()); return builder.build(); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROUnnumberedInterfaceSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROUnnumberedInterfaceSubobjectParser.java index aae0b61e97..6f068ec13e 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROUnnumberedInterfaceSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROUnnumberedInterfaceSubobjectParser.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer; @@ -19,7 +21,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder; -import com.google.common.primitives.UnsignedInts; +import com.google.common.base.Preconditions; /** * Parser for {@link UnnumberedCase} @@ -37,20 +39,18 @@ public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - if (buffer.length != CONTENT_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: " + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + if (buffer.readableBytes() != CONTENT_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + CONTENT_LENGTH + "."); } final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose); final UnnumberedBuilder ubuilder = new UnnumberedBuilder(); - ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH))); - ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET, - INTERFACE_ID_NUMBER_LENGTH)))); + buffer.readerIndex(buffer.readerIndex() + ROUTER_ID_NUMBER_OFFSET); + ubuilder.setRouterId(buffer.readUnsignedInt()); + ubuilder.setInterfaceId(buffer.readUnsignedInt()); builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build()); return builder.build(); } diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPEROSubobjectParserTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPEROSubobjectParserTest.java index b73468ba35..1a6bc1aad8 100644 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPEROSubobjectParserTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPEROSubobjectParserTest.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.Unpooled; import java.util.List; @@ -51,20 +52,20 @@ import com.google.common.collect.Lists; public class PCEPEROSubobjectParserTest { private static final byte[] ip4PrefixBytes = { (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x16, (byte) 0x00 }; + (byte) 0x16, (byte) 0x00 }; private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00 }; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00 }; private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 }; private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34, - (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; + (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, - (byte) 0x50, (byte) 0x00 }; + (byte) 0x50, (byte) 0x00 }; private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, - (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; + (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; private static final byte[] labelBytes = { (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25, - (byte) 0xFF }; + (byte) 0xFF }; private static final byte[] exrsBytes = { (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 }; private SimplePCEPExtensionProviderContext ctx; @@ -84,7 +85,7 @@ public class PCEPEROSubobjectParserTest { subs.setLoose(true); subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix( new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip4PrefixBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), true)); assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build())); } @@ -98,7 +99,7 @@ public class PCEPEROSubobjectParserTest { (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()); subs.setLoose(false); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip6PrefixBytes, 2), false)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), false)); assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build())); } @@ -108,7 +109,7 @@ public class PCEPEROSubobjectParserTest { final SubobjectBuilder subs = new SubobjectBuilder(); subs.setLoose(true); subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(asNumberBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true)); assertArrayEquals(asNumberBytes, parser.serializeSubobject(subs.build())); } @@ -119,7 +120,7 @@ public class PCEPEROSubobjectParserTest { subs.setLoose(true); subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered( new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(unnumberedBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true)); assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build())); } @@ -132,7 +133,7 @@ public class PCEPEROSubobjectParserTest { pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 })); pBuilder.setPathKey(new PathKey(4660)); subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey32Bytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true)); assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build())); } @@ -146,7 +147,7 @@ public class PCEPEROSubobjectParserTest { (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 })); pBuilder.setPathKey(new PathKey(4660)); subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey128Bytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true)); assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build())); } @@ -161,7 +162,7 @@ public class PCEPEROSubobjectParserTest { new GeneralizedLabelCaseBuilder().setGeneralizedLabel( new GeneralizedLabelBuilder().setGeneralizedLabel( new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(labelBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2)), true)); assertArrayEquals(labelBytes, parser.serializeSubobject(subs.build())); } @@ -176,7 +177,7 @@ public class PCEPEROSubobjectParserTest { builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build()); list.add(builder.build()); subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(exrsBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(exrsBytes, 2)), true)); assertArrayEquals(exrsBytes, parser.serializeSubobject(subs.build())); } } diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectParser.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectParser.java index d290b923c8..88072b0528 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectParser.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectParser.java @@ -7,8 +7,10 @@ */ 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.explicit.route.object.ero.Subobject; public interface EROSubobjectParser { - Subobject parseSubobject(byte[] buffer, boolean loose) throws PCEPDeserializerException; + Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException; } diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java index bc97b26954..a295ed341d 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java @@ -7,18 +7,20 @@ */ 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.explicit.route.object.ero.Subobject; public interface EROSubobjectRegistry { /** * Finds parser for given subobject type in the registry. Delegates parsing to found parser. * @param type subobject type, key in parser registry - * @param buffer subobject raw binary value to be parsed + * @param buffer subobject wrapped in ByteBuf * @param loose ERO specific common field * @return null if the parser for this subobject could not be found * @throws PCEPDeserializerException if the parsing did not succeed */ - Subobject parseSubobject(final int subobjectType, final byte[] buffer, final boolean loose) throws PCEPDeserializerException; + Subobject parseSubobject(final int subobjectType, final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException; /** * Find serializer for given subobject. Delegates parsing to found serializer. diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java index cd042c5472..d5063cc012 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.spi.pojo; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.concepts.HandlerRegistry; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; @@ -33,7 +35,7 @@ public final class SimpleEROSubobjectRegistry implements EROSubobjectRegistry { } @Override - public Subobject parseSubobject(int type, byte[] buffer, boolean loose) throws PCEPDeserializerException { + public Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE); final EROSubobjectParser parser = this.handlers.getParser(type); if (parser == null) { -- 2.36.6