Replaced slice() with readSlice() where appropriate. 60/15360/3
authorDana Kutenicsova <dkutenic@cisco.com>
Mon, 16 Feb 2015 12:14:15 +0000 (13:14 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Mon, 16 Feb 2015 13:32:18 +0000 (13:32 +0000)
- reduces the need to move readerIndex manually

Change-Id: I3cfe1f0d9e64a0efd7a71db02dacbe40d79eaaaa
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
25 files changed:
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/attribute/LinkAttributesParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/attribute/LinkstateAttributeParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/LinkNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/LinkstateNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NodeNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/CapabilityParameterParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/CommunitiesAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParser.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleAttributeRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractVendorInformationObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractMessageParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java
pcep/spi/src/test/java/org/opendaylight/protocol/pcep/spi/AbstractMessageParserTest.java

index 8b3e78c342e04d192eba4fcc7ce81ce7aafc6f41..ee9d727557b8a1dd17abd855cde8a1c168628d07 100644 (file)
@@ -115,9 +115,8 @@ final class LinkAttributesParser {
             case UNRESERVED_BANDWIDTH:
                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
                 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
-                    final ByteBuf v = value.slice(value.readerIndex(), BANDWIDTH_LENGTH);
+                    final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
                     unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
-                    value.skipBytes(BANDWIDTH_LENGTH);
                 }
                 builder.setUnreservedBandwidth(unreservedBandwidth);
                 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
index ddf51d7e5d2efb86f58f009bdcffa6a279c7c8c1..7e711c96a7a3d16a0d3c7579c189d3efb0dfb27e 100644 (file)
@@ -99,9 +99,8 @@ public class LinkstateAttributeParser implements AttributeParser, AttributeSeria
         while (buffer.isReadable()) {
             final int type = buffer.readUnsignedShort();
             final int length = buffer.readUnsignedShort();
-            final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+            final ByteBuf value = buffer.readSlice(length);
             map.put(type, value);
-            buffer.skipBytes(length);
         }
         final LinkstatePathAttributeBuilder builder = new LinkstatePathAttributeBuilder();
 
index 8d01a54a2696d778459ae85194b75034e19c064d..4fc9e82de4615b1d89c1541c7a57df8eb8535c32 100644 (file)
@@ -38,7 +38,7 @@ final class LinkNlriParser {
         while (buffer.isReadable()) {
             final int type = buffer.readUnsignedShort();
             final int length = buffer.readUnsignedShort();
-            final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+            final ByteBuf value = buffer.readSlice(length);
             LOG.trace("Parsing Link Descriptor: {}", ByteBufUtil.hexDump(value));
             switch (type) {
             case LINK_LR_IDENTIFIERS:
@@ -75,7 +75,6 @@ final class LinkNlriParser {
             default:
                 throw new BGPParsingException("Link Descriptor not recognized, type: " + type);
             }
-            buffer.skipBytes(length);
         }
         LOG.trace("Finished parsing Link descriptors.");
         return builder.build();
index 602030feae765104ff587c47d2f28e7cc5278c8a..dfa8ebc433ed09be9769d33b222da7b4e09719c5 100644 (file)
@@ -69,8 +69,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         final int length = buffer.readUnsignedShort();
         final NodeIdentifier remote = null;
         if (type == REMOTE_NODE_DESCRIPTORS) {
-            builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(buffer.slice(buffer.readerIndex(), length), false));
-            buffer.skipBytes(length);
+            builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(buffer.readSlice(length), false));
         }
         builder.setLinkDescriptors(LinkNlriParser.parseLinkDescriptors(buffer.slice()));
         return remote;
@@ -118,14 +117,13 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
             final int localtype = nlri.readUnsignedShort();
             locallength = nlri.readUnsignedShort();
             if (localtype == LOCAL_NODE_DESCRIPTORS) {
-                localDescriptor = NodeNlriParser.parseNodeDescriptors(nlri.slice(nlri.readerIndex(), locallength), true);
+                localDescriptor = NodeNlriParser.parseNodeDescriptors(nlri.readSlice(locallength), true);
             }
-            nlri.skipBytes(locallength);
             builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
             final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
                 - TYPE_LENGTH - LENGTH_SIZE - locallength;
             LOG.trace("Restlength {}", restLength);
-            final ByteBuf rest = nlri.slice(nlri.readerIndex(), restLength);
+            final ByteBuf rest = nlri.readSlice(restLength);
             switch (type) {
             case Link:
                 parseLink(builder, rest);
@@ -142,7 +140,6 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
             default:
                 break;
             }
-            nlri.skipBytes(restLength);
             dests.add(builder.build());
         }
         return dests;
index e5bdd2b56f8eabc57557f2e428efaf42a353df17..a60a903632bf48b20859d50f04a90f22cf9ddc4a 100644 (file)
@@ -65,7 +65,7 @@ final class NodeNlriParser {
         while (buffer.isReadable()) {
             final int type = buffer.readUnsignedShort();
             final int length = buffer.readUnsignedShort();
-            final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+            final ByteBuf value = buffer.readSlice(length);
             if (LOG.isTraceEnabled()) {
                 LOG.trace("Parsing Node Descriptor: {}", ByteBufUtil.hexDump(value));
             }
@@ -89,7 +89,6 @@ final class NodeNlriParser {
             default:
                 throw new BGPParsingException("Node Descriptor not recognized, type: " + type);
             }
-            buffer.skipBytes(length);
         }
         LOG.trace("Finished parsing Node descriptors.");
         return (local) ? new LocalNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(
index cf43bc73b2d8443c4d81cc961dc2fca4bb6c99fa..79f2253af4f2fcdb284b2ae227350adc0a1688da 100644 (file)
@@ -37,7 +37,7 @@ final class PrefixNlriParser {
         while (buffer.isReadable()) {
             final int type = buffer.readUnsignedShort();
             final int length = buffer.readUnsignedShort();
-            final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+            final ByteBuf value = buffer.readSlice(length);
             if (LOG.isTraceEnabled()) {
                 LOG.trace("Parsing Prefix Descriptor: {}", ByteBufUtil.hexDump(value));
             }
@@ -72,7 +72,6 @@ final class PrefixNlriParser {
             default:
                 throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
             }
-            buffer.skipBytes(length);
         }
         LOG.debug("Finished parsing Prefix descriptors.");
         return builder.build();
index 1694c73d70b63244eb1832ad1142f1df8bb64a06..31dac1913cf1d9471696efc10a54e470f17f082b 100644 (file)
@@ -129,7 +129,7 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
 
         final List<BgpParameters> optParams = new ArrayList<>();
         if (optLength > 0) {
-            fillParams(body.slice(body.readerIndex(), optLength), optParams);
+            fillParams(body.slice(), optParams);
         }
         LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
         return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(
@@ -145,7 +145,7 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
             }
             final int paramType = buffer.readUnsignedByte();
             final int paramLength = buffer.readUnsignedByte();
-            final ByteBuf paramBody = buffer.slice(buffer.readerIndex(), paramLength);
+            final ByteBuf paramBody = buffer.readSlice(paramLength);
 
             final BgpParameters param;
             try {
@@ -158,7 +158,6 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
             } else {
                 LOG.debug("Ignoring BGP Parameter type: {}", paramType);
             }
-            buffer.skipBytes(paramLength);
         }
         LOG.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
     }
index 2fbc681c233e5823a0a1ffd849532c4bcf1b9a13..33d100ae7d7ad5108670505c8611fcc3317da623 100644 (file)
@@ -81,8 +81,7 @@ public class BGPUpdateMessageParser implements MessageParser, MessageSerializer
         }
         if (totalPathAttrLength > 0) {
             try {
-                final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
-                buffer.skipBytes(totalPathAttrLength);
+                final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength));
                 eventBuilder.setPathAttributes(pathAttributes);
             } catch (final BGPParsingException | RuntimeException e) {
                 // Catch everything else and turn it into a BGPDocumentedException
index ddedab068a28fa9c05d35b0cc0867b554380d617..c3da520c9fbd02bc2e545384dad6b82b8e7beade 100644 (file)
@@ -59,8 +59,7 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
     private OptionalCapabilities parseOptionalCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
         final int capCode = buffer.readUnsignedByte();
         final int capLength = buffer.readUnsignedByte();
-        final ByteBuf paramBody = buffer.slice(buffer.readerIndex(), capLength);
-        buffer.skipBytes(capLength);
+        final ByteBuf paramBody = buffer.readSlice(capLength);
         final CParameters ret = this.reg.parseCapability(capCode, paramBody);
         if (ret == null) {
             LOG.debug("Ignoring unsupported capability {}", capCode);
@@ -70,7 +69,7 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
     }
 
     @Override
-    public void serializeParameter(final BgpParameters parameter, ByteBuf byteAggregator) {
+    public void serializeParameter(final BgpParameters parameter, final ByteBuf byteAggregator) {
         if (parameter.getOptionalCapabilities() != null && !parameter.getOptionalCapabilities().isEmpty()) {
             LOG.trace("Started serializing BGP Capability: {}", parameter.getOptionalCapabilities());
             final ByteBuf buffer = Unpooled.buffer();
index 9e67a847753e01c509a3636b7970e67622a6e378..7db909945a05c462f82a574c2e02fc14741a8c82 100644 (file)
@@ -75,18 +75,14 @@ public final class AsPathAttributeParser implements AttributeParser, AttributeSe
             final int count = UnsignedBytes.toInt(buffer.readByte());
 
             if (segmentType == SegmentType.AS_SEQUENCE) {
-                final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.slice(buffer.readerIndex(),
-                    count * AsPathSegmentParser.AS_NUMBER_LENGTH));
+                final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
                 ases.add(new SegmentsBuilder().setCSegment(
                     new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(numbers).build()).build()).build());
                 isSequence = true;
             } else {
-                final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.slice(buffer.readerIndex(), count
-                    * AsPathSegmentParser.AS_NUMBER_LENGTH));
+                final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
                 ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
-
             }
-            buffer.skipBytes(count * AsPathSegmentParser.AS_NUMBER_LENGTH);
         }
         if (!isSequence) {
             throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
index 6d72698f73170ad4e006f1833ac945a387722336..880ab145f3c5eb7d9f3a1d4410c4221e79b05a90 100644 (file)
@@ -49,8 +49,7 @@ public final class CommunitiesAttributeParser implements AttributeParser, Attrib
     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
         final List<Communities> set = Lists.newArrayList();
         while (buffer.isReadable()) {
-            set.add((Communities) parseCommunity(this.refCache, buffer.slice(buffer.readerIndex(), COMMUNITY_LENGTH)));
-            buffer.skipBytes(COMMUNITY_LENGTH);
+            set.add((Communities) parseCommunity(this.refCache, buffer.readSlice(COMMUNITY_LENGTH)));
         }
         builder.setCommunities(set);
     }
index c9d49b163db9ed7ddf11db861c0b813f39da1b24..62e3c504662bca2abf47103b37059685edbd90d1 100644 (file)
@@ -90,8 +90,7 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
         while (buffer.isReadable()) {
             final ExtendedCommunitiesBuilder exBuilder = new ExtendedCommunitiesBuilder();
             parseHeader(exBuilder, buffer);
-            final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, exBuilder, buffer.slice(buffer.readerIndex(), EXTENDED_COMMUNITY_LENGTH));
-            buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
+            final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, exBuilder, buffer.readSlice(EXTENDED_COMMUNITY_LENGTH));
             set.add(comm);
         }
         builder.setExtendedCommunities(set);
index 34a19888d23716a625be264213406b6f4d501eb4..1c3352378672b4691ca211ddeff15ca55727b6d3 100644 (file)
@@ -10,11 +10,11 @@ package org.opendaylight.protocol.bgp.parser.impl;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import io.netty.buffer.ByteBuf;
@@ -42,7 +42,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
@@ -322,7 +321,14 @@ public class ParserTest {
 
     @Test
     public void testParseUpdMsgWithUnrecognizedAttribute() throws BGPDocumentedException, BGPParsingException {
-        final Update updMsg = (Update) reg.parseMessage(Unpooled.copiedBuffer(updMsgWithUnrecognizedAttribute));
-        assertNotNull(updMsg.getPathAttributes());
+        try {
+            reg.parseMessage(Unpooled.copiedBuffer(updMsgWithUnrecognizedAttribute));
+            fail("Exception should have occured.");
+        } catch (final BGPDocumentedException e) {
+            assertEquals("Well known attribute not recognized.", e.getMessage());
+            assertEquals(BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED, e.getError());
+            return;
+        }
+        fail();
     }
 }
index 9d83e103c8a05248debc7af586732d456afe7cb0..14aa5d9f52534b98885173f36de2cf152f6c5277 100644 (file)
@@ -54,15 +54,9 @@ public abstract class AbstractMessageRegistry implements MessageRegistry {
                     + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
         }
 
-        final ByteBuf msgBody = buffer.slice(buffer.readerIndex(), messageLength - MessageUtil.COMMON_HEADER_LENGTH);
+        final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
 
-        Notification msg = null;
-        try {
-            msg = parseBody(messageType, msgBody, messageLength);
-        } finally {
-            // Always reads body bytes
-            buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
-        }
+        final Notification msg = parseBody(messageType, msgBody, messageLength);
         if (msg == null) {
             throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes });
         }
index 4500485723b44286c1f16bb7175894c3cf79e1b4..ad5304664568a95bcd870fd3e62c08a28e650e6f 100644 (file)
@@ -92,12 +92,11 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                     LOG.warn("Ignoring unrecognized attribute type {}. Some data might be missing from the output.", type);
                 }
             } else {
-                attributes.put(type, new RawAttribute(parser, buffer.slice(buffer.readerIndex(), len)));
+                attributes.put(type, new RawAttribute(parser, buffer.readSlice(len)));
             }
         } else {
             LOG.debug("Ignoring duplicate attribute type {}", type);
         }
-        buffer.skipBytes(len);
     }
 
     @Override
index 40b6c2e457fc26b975f62e52b810e54543ec0030..7e43f1c902716bfa38fd4b5c109f449364c4f643 100644 (file)
@@ -10,13 +10,10 @@ package org.opendaylight.protocol.bgp.parser.spi.pojo;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
 import com.google.common.primitives.UnsignedBytes;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-
 import org.opendaylight.bgp.concepts.NextHopUtil;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
@@ -121,7 +118,7 @@ final class SimpleNlriRegistry implements NlriRegistry {
     }
 
     @Override
-    public void serializeMpReach(MpReachNlri mpReachNlri, ByteBuf byteAggregator) {
+    public void serializeMpReach(final MpReachNlri mpReachNlri, final ByteBuf byteAggregator) {
         byteAggregator.writeShort(this.afiReg.numberForClass(mpReachNlri.getAfi()));
         byteAggregator.writeByte(this.safiReg.numberForClass(mpReachNlri.getSafi()));
 
@@ -134,7 +131,7 @@ final class SimpleNlriRegistry implements NlriRegistry {
     }
 
     @Override
-    public void serializeMpUnReach(MpUnreachNlri mpUnreachNlri, ByteBuf byteAggregator) {
+    public void serializeMpUnReach(final MpUnreachNlri mpUnreachNlri, final ByteBuf byteAggregator) {
         byteAggregator.writeShort(this.afiReg.numberForClass(mpUnreachNlri.getAfi()));
         byteAggregator.writeByte(this.safiReg.numberForClass(mpUnreachNlri.getSafi()));
     }
@@ -153,8 +150,8 @@ final class SimpleNlriRegistry implements NlriRegistry {
         final NlriParser parser = this.handlers.get(createKey(builder.getAfi(), builder.getSafi()));
 
         final int nextHopLength = UnsignedBytes.toInt(buffer.readByte());
-        builder.setCNextHop(NextHopUtil.parseNextHop(buffer.slice(buffer.readerIndex(), nextHopLength)));
-        buffer.skipBytes(nextHopLength + RESERVED);
+        builder.setCNextHop(NextHopUtil.parseNextHop(buffer.readSlice(nextHopLength)));
+        buffer.skipBytes(RESERVED);
 
         final ByteBuf nlri = buffer.slice();
         parser.parseNlri(nlri, builder);
index b4939983d618d6512c82890c16287b4032b274e6..694b025675797709b13913c02cb6e33ae0afbf6c 100644 (file)
@@ -47,14 +47,13 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O
                         + buffer.readableBytes());
             }
             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
-            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), loose);
+            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), loose);
             if (sub == null) {
                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
             } else {
                 LOG.debug("Subobject was parsed. {}", sub);
                 subs.add(sub);
             }
-            buffer.readerIndex(buffer.readerIndex() + length);
         }
         return subs;
     }
index 9eb0b930731560f79607a24c6369e33c234eeec4..8305a983ee32431ad2024c2d9b2bad2bc9a0be75 100644 (file)
@@ -37,21 +37,20 @@ public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, O
         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
         final List<Subobject> subs = new ArrayList<>();
         while (buffer.isReadable()) {
-            int type = UnsignedBytes.toInt(buffer.readByte());
-            int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
+            final int type = UnsignedBytes.toInt(buffer.readByte());
+            final int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
             if (length > buffer.readableBytes()) {
                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
                         + buffer.readableBytes());
             }
             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
-            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length));
+            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length));
             if (sub == null) {
                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
             } else {
                 LOG.debug("Subobject was parsed. {}", sub);
                 subs.add(sub);
             }
-            buffer.readerIndex(buffer.readerIndex() + length);
         }
         return subs;
     }
index b1414c930f59d68ec96dc1189e59661cae70ab72..e1781ede930ad915b9b741b35e9d3762b3d8357c 100644 (file)
@@ -13,10 +13,8 @@ import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_IN
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
 
 import com.google.common.base.Preconditions;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import org.opendaylight.protocol.pcep.spi.EnterpriseSpecificInformationParser;
 import org.opendaylight.protocol.pcep.spi.ObjectParser;
 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
@@ -34,7 +32,6 @@ public abstract class AbstractVendorInformationObjectParser implements ObjectSer
     public final Object 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 VendorInformationObjectBuilder builder = new VendorInformationObjectBuilder();
-        buffer.readUnsignedInt();
         builder.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
         builder.setEnterpriseSpecificInformation(parseEnterpriseSpecificInformation(buffer));
         return builder.build();
index 290c6c14c5ac068eac832fcd37c20da9644bbbe2..3c4dcd18c4516d37767cf88756a39c98782a68f4 100644 (file)
@@ -46,14 +46,13 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O
                         + buffer.readableBytes());
             }
             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
-            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory);
+            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), mandatory);
             if (sub == null) {
                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
             } else {
                 LOG.debug("Subobject was parsed. {}", sub);
                 subs.add(sub);
             }
-            buffer.readerIndex(buffer.readerIndex() + length);
         }
         return subs;
     }
index cf1bc443b1041f4e1ed17ddfd5b774479d633f43..b1790cb2c9f41f778a95c11fcb08f74cc214a562 100644 (file)
@@ -86,8 +86,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
                         + buffer.readableBytes());
             }
-            subs.add(this.registry.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory));
-            buffer.readerIndex(buffer.readerIndex() + length);
+            subs.add(this.registry.parseSubobject(type, buffer.readSlice(length), mandatory));
         }
         return subs;
     }
index e463013ee02b83f7b4c23abe0311d7ff5d0f3bc6..6cdfdb13bddc14f4460d5e2e6757e9178ec673a1 100644 (file)
@@ -1100,8 +1100,9 @@ public class PCEPObjectParserTest {
         final VendorInformationObject viObj = new VendorInformationObjectBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
                 .setEnterpriseSpecificInformation(esInfo).build();
         final ByteBuf result = Unpooled.wrappedBuffer(viObjBytes);
-
-        assertEquals(viObj, parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
+        result.readerIndex(8);
+        final VendorInformationObject o = (VendorInformationObject) parser.parseObject(new ObjectHeaderImpl(false, false), result.readSlice(result.readableBytes()));
+        assertEquals(viObj, o);
 
         final ByteBuf buf = Unpooled.buffer(viObjBytes.length);
         parser.serializeObject(viObj, buf);
index 4022e512d1cf9b7dfe46202e0a30ce4257518693..423c344969ca1737e4cb915ba2f1709868fe076c 100644 (file)
@@ -96,13 +96,13 @@ public abstract class AbstractMessageParser implements MessageParser, MessageSer
                         + objLength + ".");
             }
             // copy bytes for deeper parsing
-            final ByteBuf bytesToPass = bytes.slice(bytes.readerIndex(), objLength - COMMON_OBJECT_HEADER_LENGTH);
+            final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);
 
             final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
 
             if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
                 Preconditions.checkState(this.viRegistry != null);
-                final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(bytes.getUnsignedInt(bytes.readerIndex()));
+                final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(bytesToPass.readUnsignedInt());
                 final Optional<? extends Object> obj = this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, bytesToPass);
                 if (obj.isPresent()) {
                     objs.add(obj.get());
@@ -114,8 +114,6 @@ public abstract class AbstractMessageParser implements MessageParser, MessageSer
                     objs.add(o);
                 }
             }
-
-            bytes.readerIndex(bytes.readerIndex() + objLength - COMMON_OBJECT_HEADER_LENGTH);
         }
 
         return objs;
index f63587dbd60269ffe40001b0c6cc026774566ed9..65c9321756d1d5408031b2cb960a446446fdddd6 100644 (file)
@@ -41,13 +41,13 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
         }
         final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
         while (bytes.isReadable()) {
-            int type = bytes.readUnsignedShort();
-            int length = bytes.readUnsignedShort();
+            final int type = bytes.readUnsignedShort();
+            final int length = bytes.readUnsignedShort();
             if (length > bytes.readableBytes()) {
                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
                     + ".");
             }
-            final ByteBuf tlvBytes = bytes.slice(bytes.readerIndex(), length);
+            final ByteBuf tlvBytes = bytes.readSlice(length);
             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
 
             if (VendorInformationUtil.isVendorInformationTlv(type)) {
@@ -64,7 +64,7 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
                     addTlv(builder, tlv);
                 }
             }
-            bytes.skipBytes(length + TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
+            bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
         }
         addVendorInformationTlvs(builder, viTlvs);
     }
index b2e48ec272d14d918885e0b57b6a147d884dc3ef..dadbe468741ffbce9996cf6623e7befc57d6b1cb 100644 (file)
@@ -50,20 +50,20 @@ public class AbstractMessageParserTest {
 
     private class Abs extends AbstractMessageParser {
 
-        protected Abs(ObjectRegistry registry) {
+        protected Abs(final ObjectRegistry registry) {
             super(registry);
         }
 
-        protected Abs(ObjectRegistry registry, VendorInformationObjectRegistry viRegistry) {
+        protected Abs(final ObjectRegistry registry, final VendorInformationObjectRegistry viRegistry) {
             super(registry, viRegistry);
         }
 
         @Override
-        public void serializeMessage(Message message, ByteBuf buffer) {
+        public void serializeMessage(final Message message, final ByteBuf buffer) {
         }
 
         @Override
-        protected Message validate(List<Object> objects, List<Message> errors) throws PCEPDeserializerException {
+        protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
             if (objects.get(0) instanceof VendorInformationObject) {
                 final RepliesBuilder repsBuilder = new RepliesBuilder();
                 repsBuilder.setVendorInformationObject(addVendorInformationObjects(objects));
@@ -85,20 +85,20 @@ public class AbstractMessageParserTest {
         this.object = new ErrorObjectBuilder().setType((short) 1).setValue((short) 1).build();
         this.viObject = new VendorInformationObjectBuilder().setEnterpriseNumber(EN).build();
         Mockito.doNothing().when(this.viRegistry).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
-        Mockito.doReturn(Optional.of(this.viObject)).when(this.viRegistry).parseVendorInformationObject(EN, new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0 }));
+        Mockito.doReturn(Optional.of(this.viObject)).when(this.viRegistry).parseVendorInformationObject(Mockito.eq(EN), Mockito.eq(new ObjectHeaderImpl(true, true)), Mockito.any(ByteBuf.class));
         Mockito.doNothing().when(this.registry).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
         Mockito.doReturn(this.object).when(this.registry).parseObject(13, 1, new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(new byte[] { 0, 0, 1, 1 }));
     }
 
     @Test
     public void testParseObjects() throws PCEPDeserializerException {
-        Abs a = new Abs(this.registry);
-        ByteBuf buffer = Unpooled.buffer();
+        final Abs a = new Abs(this.registry);
+        final ByteBuf buffer = Unpooled.buffer();
         a.serializeObject(this.object, buffer);
 
         Mockito.verify(this.registry, Mockito.only()).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
 
-        Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }), Collections.<Message> emptyList());
+        final Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }), Collections.<Message> emptyList());
 
         assertEquals(this.object, ((Pcerr) b).getPcerrMessage().getErrors().get(0).getErrorObject());
     }
@@ -108,7 +108,7 @@ public class AbstractMessageParserTest {
         final Abs parser = new Abs(this.registry, this.viRegistry);
         final ByteBuf buffer = Unpooled.buffer();
 
-        parser.serializeVendorInformationObjects(Lists.newArrayList(viObject), buffer);
+        parser.serializeVendorInformationObjects(Lists.newArrayList(this.viObject), buffer);
         Mockito.verify(this.viRegistry, Mockito.only()).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
 
         final Message msg = parser.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x22, 0x13, 0x00, 0x08, 0, 0, 0, 0 }), Collections.<Message> emptyList());