From: Dana Kutenicsova Date: Sun, 1 Jun 2014 21:53:46 +0000 (+0200) Subject: BUG-612 : switch XRO to ByteBuf. X-Git-Tag: release/helium~244 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=d36af969d3ef712b074fd9056e852f19b0b14d50;p=bgpcep.git BUG-612 : switch XRO to ByteBuf. Change-Id: I2122ef661f5697a7e8cff8c293aa24b7c200cd0f Signed-off-by: Dana Kutenicsova --- 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 a7bee2d2c3..42290ad48b 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 @@ -7,13 +7,16 @@ */ 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.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,13 +29,7 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O private static final Logger LOG = LoggerFactory.getLogger(AbstractXROWithSubobjectsParser.class); - private static final int SUB_TYPE_FLAG_F_LENGTH = 1; - private static final int SUB_LENGTH_F_LENGTH = 1; - private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH; - - 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 subobjReg; @@ -40,43 +37,26 @@ public abstract class AbstractXROWithSubobjectsParser 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."); - } - int type; - byte[] soContentsBytes; - int length; - int offset = 0; - - final List subs = Lists.newArrayList(); - - while (offset < bytes.length) { - - final boolean mandatory = ((bytes[offset] & (1 << 7)) != 0) ? true : false; - type = UnsignedBytes.checkedCast((bytes[offset] & 0xff) & ~(1 << 7)); - - offset += SUB_TYPE_FLAG_F_LENGTH; - - length = UnsignedBytes.toInt(bytes[offset]); - - offset += SUB_LENGTH_F_LENGTH; - - if (length - SUB_HEADER_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()) { + final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false; + int type = UnsignedBytes.checkedCast((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 = ByteArray.subByte(bytes, offset, length - SO_CONTENTS_OFFSET); - - LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes)); - final Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, mandatory); + LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer)); + final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory); if (sub == null) { LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } - offset += soContentsBytes.length; + buffer.readerIndex(buffer.readerIndex() + length); } return subs; } 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 2ca3d85d45..03ff72cf9d 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 @@ -44,8 +44,7 @@ public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobject builder.setProcessingRule(header.isProcessingRule()); bytes.readerIndex(bytes.readerIndex() + FLAGS_OFFSET); builder.setFlags(new Flags(bytes.readBoolean())); - //FIXME: switch to ByteBuf - builder.setSubobject(parseSubobjects(ByteArray.readAllBytes(bytes))); + builder.setSubobject(parseSubobjects(bytes.slice())); return builder.build(); } 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 682a5f87e6..851d13ae37 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 @@ -17,7 +17,6 @@ import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry; -import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase; @@ -89,8 +88,8 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes()); } - //FIXME: switch to ByteBuf - subs.add(this.registry.parseSubobject(type, ByteArray.readBytes(buffer, length), mandatory)); + subs.add(this.registry.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory)); + buffer.readerIndex(buffer.readerIndex() + length); } return subs; } 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 2c4741d3f0..07104c5353 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 @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectParser; @@ -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,17 +38,15 @@ public class XROAsNumberSubobjectParser implements XROSubobjectParser, XROSubobj private static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 mandatory) 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().setMandatory(mandatory).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/XROIpv4PrefixSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROIpv4PrefixSubobjectParser.java index 4fe45b4246..3c167b2d45 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 @@ -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.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -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; /** @@ -42,20 +45,18 @@ public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubo private static final int CONTENT4_LENGTH = ATTRIBUTE4_OFFSET + ATTRIBUTE_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 != CONTENT4_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";"); - } + public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) 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.setMandatory(mandatory); - final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]); - builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer[ATTRIBUTE4_OFFSET]))); - builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix( - new IpPrefixBuilder().setIpPrefix( - new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build()).build()); + if (buffer.readableBytes() != CONTENT4_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); + } + 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()); + buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH); + builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte()))); return builder.build(); } 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 b4b6707e08..ce051bbdb8 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 @@ -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.XROSubobjectUtil; @@ -23,6 +25,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; /** @@ -44,20 +47,18 @@ public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubo private static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 != CONTENT6_LENGTH) { - throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";"); - } + public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) 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.setMandatory(mandatory); - final int length = UnsignedBytes.toInt(buffer[PREFIX6_F_OFFSET]); - builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer[ATTRIBUTE6_OFFSET]))); - builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix( - new IpPrefixBuilder().setIpPrefix( - new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP6_F_LENGTH), length))).build()).build()); + if (buffer.readableBytes() != CONTENT6_LENGTH) { + throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); + } + 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))); + builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); + buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH); + builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte()))); return builder.build(); } 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 b5fa38a80d..f85f5206f2 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 @@ -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.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -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 XROPathKey128SubobjectParser implements XROSubobjectParser, XROSubo private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 mandatory) 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.setMandatory(mandatory); final PathKeyBuilder pBuilder = new PathKeyBuilder(); pBuilder.setPceId(new PceId(pceId)); pBuilder.setPathKey(new PathKey(pathKey)); + builder.setMandatory(mandatory); 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/XROPathKey32SubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROPathKey32SubobjectParser.java index 29945abd8d..b0c2e45ba0 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 @@ -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.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -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 XROPathKey32SubobjectParser implements XROSubobjectParser, XROSubob private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 mandatory) 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.setMandatory(mandatory); final PathKeyBuilder pBuilder = new PathKeyBuilder(); pBuilder.setPceId(new PceId(pceId)); pBuilder.setPathKey(new PathKey(pathKey)); + builder.setMandatory(mandatory); 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/XROSRLGSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/XROSRLGSubobjectParser.java index 222a936e28..01623b7eef 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 @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectParser; @@ -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.SrlgCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder; +import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; /** @@ -39,12 +42,10 @@ public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectS private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 mandatory) 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 + "."); } @@ -53,7 +54,7 @@ public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectS builder.setAttribute(Attribute.Srlg); builder.setSubobjectType(new SrlgCaseBuilder().setSrlg( new SrlgBuilder().setSrlgId( - new SrlgId(ByteArray.bytesToLong(ByteArray.subByte(buffer, SRLG_ID_NUMBER_OFFSET, SRLG_ID_NUMBER_LENGTH)))).build()).build()); + new SrlgId(buffer.readUnsignedInt())).build()).build()); return builder.build(); } 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 dacc511dee..c659416c34 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 @@ -7,6 +7,8 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectParser; @@ -20,8 +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.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.base.Preconditions; import com.google.common.primitives.UnsignedBytes; -import com.google.common.primitives.UnsignedInts; /** * Parser for {@link UnnumberedCase} @@ -41,23 +43,20 @@ public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH; @Override - public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) 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 mandatory) 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 + "."); } - + buffer.readerIndex(buffer.readerIndex() + ATTRIBUTE_OFFSET); final SubobjectBuilder builder = new SubobjectBuilder(); + builder.setMandatory(mandatory); + builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte()))); 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)))); + ubuilder.setRouterId(buffer.readUnsignedInt()); + ubuilder.setInterfaceId(buffer.readUnsignedInt()); builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build()); - builder.setMandatory(mandatory); - builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer[ATTRIBUTE_OFFSET]))); return builder.build(); } 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 bcd3517d2d..0d53d4cca1 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.Unpooled; import org.junit.Test; import org.opendaylight.protocol.concepts.Ipv6Util; @@ -43,19 +44,19 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev public class PCEPXROSubobjectParserTest { private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x16, (byte) 0x00 }; + (byte) 0x16, (byte) 0x00 }; private static final byte[] ip6PrefixBytes = { (byte) 0x82, (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) 0x01 }; + (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) 0x01 }; private static final byte[] srlgBytes = { (byte) 0xa2, (byte) 0x07, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x02 }; private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (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[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 }; 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 }; @Test public void testXROIp4PrefixSubobject() throws PCEPDeserializerException { @@ -65,7 +66,7 @@ public class PCEPXROSubobjectParserTest { subs.setAttribute(Attribute.Interface); 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), false)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false)); assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build())); } @@ -80,7 +81,7 @@ public class PCEPXROSubobjectParserTest { new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (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 }, 22))).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip6PrefixBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true)); assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build())); } @@ -91,7 +92,7 @@ public class PCEPXROSubobjectParserTest { subs.setMandatory(true); subs.setAttribute(Attribute.Srlg); subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build()); - assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(srlgBytes, 2), true)); + assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true)); assertArrayEquals(srlgBytes, parser.serializeSubobject(subs.build())); } @@ -103,7 +104,7 @@ public class PCEPXROSubobjectParserTest { subs.setAttribute(Attribute.Node); 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())); } @@ -113,7 +114,7 @@ public class PCEPXROSubobjectParserTest { final SubobjectBuilder subs = new SubobjectBuilder(); subs.setMandatory(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())); } @@ -126,7 +127,7 @@ public class PCEPXROSubobjectParserTest { 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())); } @@ -140,7 +141,7 @@ public class PCEPXROSubobjectParserTest { (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())); } } diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectParser.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectParser.java index 6a0d3338f6..6728bf939d 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectParser.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectParser.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.exclude.route.object.xro.Subobject; public interface XROSubobjectParser { - Subobject parseSubobject(byte[] buffer, boolean mandatory) throws PCEPDeserializerException; + Subobject parseSubobject(final ByteBuf buffer, boolean mandatory) throws PCEPDeserializerException; } 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 69b03273e1..5a9cb1e95e 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 @@ -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.exclude.route.object.xro.Subobject; public interface XROSubobjectRegistry { /** * 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 mandatory XRO 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 type, final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException; + Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean mandatory) 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/SimpleXROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java index 85e4b3dc30..cf6fdb9d08 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 @@ -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.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.XROSubobjectParser; @@ -33,7 +35,7 @@ public final class SimpleXROSubobjectRegistry implements XROSubobjectRegistry { } @Override - public Subobject parseSubobject(int type, byte[] buffer, boolean mandatory) throws PCEPDeserializerException { + public Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException { Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE); final XROSubobjectParser parser = this.handlers.getParser(type); if (parser == null) {