BUG-64 : refactor BGP parser to use ByteBuf 04/5804/9
authorDana Kutenicsova <dkutenic@cisco.com>
Fri, 28 Mar 2014 17:34:31 +0000 (18:34 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Thu, 3 Apr 2014 10:27:17 +0000 (10:27 +0000)
Change-Id: Ibbd892d29fe709facaf06cf03273ee0ceea174a9
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
61 files changed:
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateAttributeParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/TlvCode.java
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/LinkstateAttributeParserTest.java
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParserTest.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPKeepAliveMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.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/As4CapabilityHandler.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/open/GracefulCapabilityHandler.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/MultiProtocolCapabilityHandler.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AS4AggregatorAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AS4PathAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AggregatorAttributeParser.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/AsPathSegmentParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AtomicAggregateAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ClusterIdAttributeParser.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/CommunitiesParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/IpNlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/Ipv4NlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/Ipv6NlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/LocalPreferenceAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MPReachAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MPUnreachAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MultiExitDiscriminatorAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/NextHopAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/OriginAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/OriginatorIdAttributeParser.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java
bgp/parser-mock/src/main/java/org/opendaylight/protocol/bgp/parser/mock/BGPMessageParserMock.java
bgp/parser-mock/src/test/java/org/opendaylight/protocol/bgp/parser/mock/BGPMessageParserMockTest.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/AttributeParser.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AttributeRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/CapabilityParser.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/CapabilityRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageParser.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/NlriParser.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/NlriRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/ParameterParser.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/ParameterRegistry.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/SimpleCapabilityRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleMessageRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleParameterRegistry.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPByteToMessageDecoder.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPSessionNegotiator.java
bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java
util/pom.xml
util/src/main/java/org/opendaylight/protocol/util/ByteArray.java
util/src/test/java/org/opendaylight/protocol/util/ByteArrayTest.java

index 2d726ba5fc4a9bf62c289ad98b89d6c54fadbd64..27b60dee650848274c39edf748dbfa1b9a36931d 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.linkstate;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -73,10 +75,6 @@ public class LinkstateAttributeParser implements AttributeParser {
 
        private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
 
-       private static final int TYPE_LENGTH = 2;
-
-       private static final int LENGTH_SIZE = 2;
-
        private static final int ROUTE_TAG_LENGTH = 4;
 
        private static final int EXTENDED_ROUTE_TAG_LENGTH = 8;
@@ -112,26 +110,23 @@ public class LinkstateAttributeParser implements AttributeParser {
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPParsingException {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPParsingException {
                final NlriType type = getNlriType(builder);
                if (type == null) {
                        LOG.warn("No Linkstate NLRI found, not parsing Linkstate attribute");
                        return;
                }
 
-               final PathAttributes1 a = new PathAttributes1Builder().setLinkstatePathAttribute(parseLinkState(type, bytes)).build();
+               final PathAttributes1 a = new PathAttributes1Builder().setLinkstatePathAttribute(parseLinkState(type, buffer)).build();
                builder.addAugmentation(PathAttributes1.class, a);
        }
 
-       private static LinkstatePathAttribute parseLinkState(final NlriType nlri, final byte[] bytes) throws BGPParsingException {
-               final Map<Short, ByteList> map = new TreeMap<>();
-               int byteOffset = 0;
-               while (byteOffset != bytes.length) {
-                       final Short type = ByteArray.bytesToShort(ByteArray.subByte(bytes, byteOffset, TYPE_LENGTH));
-                       byteOffset += TYPE_LENGTH;
-                       final int length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
-                       final byte[] value = ByteArray.subByte(bytes, byteOffset, length);
+       private static LinkstatePathAttribute parseLinkState(final NlriType nlri, final ByteBuf buffer) throws BGPParsingException {
+               final Map<Integer, ByteList> map = new TreeMap<>();
+               while (buffer.readableBytes() != 0) {
+                       final int type = buffer.readUnsignedShort();
+                       final int length = buffer.readUnsignedShort();
+                       final byte[] value = ByteArray.readBytes(buffer, length);
 
                        // TODO: does the specification allow for a TLV to occur multiple times? If so, provide a reference to
                        // that section and replace this comment which description of what we are doing here.
@@ -142,7 +137,6 @@ public class LinkstateAttributeParser implements AttributeParser {
 
                        values.add(value);
                        map.put(type, values);
-                       byteOffset += length;
                }
                final LinkstatePathAttributeBuilder builder = new LinkstatePathAttributeBuilder();
 
@@ -168,10 +162,10 @@ public class LinkstateAttributeParser implements AttributeParser {
         * @param attributes key is the tlv type and value is the value of the tlv
         * @return {@link LinkStateAttribute}
         */
-       private static LinkStateAttribute parseLinkAttributes(final Map<Short, ByteList> attributes) {
+       private static LinkStateAttribute parseLinkAttributes(final Map<Integer, ByteList> attributes) {
 
                final LinkAttributesBuilder builder = new LinkAttributesBuilder();
-               for (final Entry<Short, ByteList> entry : attributes.entrySet()) {
+               for (final Entry<Integer, ByteList> entry : attributes.entrySet()) {
                        LOG.trace("Link attribute TLV {}", entry.getKey());
 
                        for (final byte[] value : entry.getValue().getBytes()) {
@@ -276,11 +270,11 @@ public class LinkstateAttributeParser implements AttributeParser {
         * @param attributes key is the tlv type and value is the value of the tlv
         * @return {@link LinkStateAttribute}
         */
-       private static LinkStateAttribute parseNodeAttributes(final Map<Short, ByteList> attributes) {
+       private static LinkStateAttribute parseNodeAttributes(final Map<Integer, ByteList> attributes) {
                final List<TopologyIdentifier> topologyMembership = Lists.newArrayList();
                final List<IsisAreaIdentifier> areaMembership = Lists.newArrayList();
                final NodeAttributesBuilder builder = new NodeAttributesBuilder();
-               for (final Entry<Short, ByteList> entry : attributes.entrySet()) {
+               for (final Entry<Integer, ByteList> entry : attributes.entrySet()) {
                        LOG.trace("Node attribute TLV {}", entry.getKey());
                        for (final byte[] value : entry.getValue().getBytes()) {
                                switch (entry.getKey()) {
@@ -339,11 +333,11 @@ public class LinkstateAttributeParser implements AttributeParser {
         * @param attributes key is the tlv type and value are the value bytes of the tlv
         * @return {@link LinkStateAttribute}
         */
-       private static LinkStateAttribute parsePrefixAttributes(final Map<Short, ByteList> attributes) {
+       private static LinkStateAttribute parsePrefixAttributes(final Map<Integer, ByteList> attributes) {
                final PrefixAttributesBuilder builder = new PrefixAttributesBuilder();
                final List<RouteTag> routeTags = Lists.newArrayList();
                final List<ExtendedRouteTag> exRouteTags = Lists.newArrayList();
-               for (final Entry<Short, ByteList> entry : attributes.entrySet()) {
+               for (final Entry<Integer, ByteList> entry : attributes.entrySet()) {
                        LOG.trace("Prefix attribute TLV {}", entry.getKey());
                        for (final byte[] value : entry.getValue().getBytes()) {
                                switch (entry.getKey()) {
index a984aadc2d88b33fffdd8d9139fcdabd6f3b1856..72623217b5664b9036a75dfd0c903eacc79041b5 100644 (file)
@@ -97,31 +97,25 @@ public final class LinkstateNlriParser implements NlriParser {
                this.isVpn = isVpn;
        }
 
-       private static NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final byte[] bytes) throws BGPParsingException {
-               int byteOffset = 0;
-               final int type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TYPE_LENGTH));
-               byteOffset += TYPE_LENGTH;
-               final int length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, LENGTH_SIZE));
-               byteOffset += LENGTH_SIZE;
+       private static NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final ByteBuf buffer) throws BGPParsingException {
+               final int type = buffer.readUnsignedShort();
+               final int length = buffer.readUnsignedShort();
                final NodeIdentifier remote = null;
                if (type == TlvCode.REMOTE_NODE_DESCRIPTORS) {
-                       builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) parseNodeDescriptors(ByteArray.subByte(bytes, byteOffset, length),
+                       builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) parseNodeDescriptors(buffer.slice(buffer.readerIndex(), length),
                                        false));
-                       byteOffset += length;
+                       buffer.skipBytes(length);
                }
-               builder.setLinkDescriptors(parseLinkDescriptors(ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset)));
+               builder.setLinkDescriptors(parseLinkDescriptors(buffer.slice()));
                return remote;
        }
 
-       private static LinkDescriptors parseLinkDescriptors(final byte[] bytes) throws BGPParsingException {
-               int byteOffset = 0;
+       private static LinkDescriptors parseLinkDescriptors(final ByteBuf buffer) throws BGPParsingException {
                final LinkDescriptorsBuilder builder = new LinkDescriptorsBuilder();
-               while (byteOffset != bytes.length) {
-                       final int type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TYPE_LENGTH));
-                       byteOffset += TYPE_LENGTH;
-                       final int length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
-                       final byte[] value = ByteArray.subByte(bytes, byteOffset, length);
+               while (buffer.readableBytes() != 0) {
+                       final int type = buffer.readUnsignedShort();
+                       final int length = buffer.readUnsignedShort();
+                       final byte[] value = ByteArray.readBytes(buffer, length);
                        LOG.trace("Parsing Link Descriptor: {}", Arrays.toString(value));
                        switch (type) {
                        case TlvCode.LINK_LR_IDENTIFIERS:
@@ -159,24 +153,20 @@ public final class LinkstateNlriParser implements NlriParser {
                        default:
                                throw new BGPParsingException("Link Descriptor not recognized, type: " + type);
                        }
-                       byteOffset += length;
                }
                LOG.trace("Finished parsing Link descriptors.");
                return builder.build();
        }
 
-       private static NodeIdentifier parseNodeDescriptors(final byte[] bytes, final boolean local) throws BGPParsingException {
-               int byteOffset = 0;
+       private static NodeIdentifier parseNodeDescriptors(final ByteBuf buffer, final boolean local) throws BGPParsingException {
                AsNumber asnumber = null;
                DomainIdentifier bgpId = null;
                AreaIdentifier ai = null;
                CRouterIdentifier routerId = null;
-               while (byteOffset != bytes.length) {
-                       final int type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TYPE_LENGTH));
-                       byteOffset += TYPE_LENGTH;
-                       final int length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
-                       final byte[] value = ByteArray.subByte(bytes, byteOffset, length);
+               while (buffer.readableBytes() != 0) {
+                       final int type = buffer.readUnsignedShort();
+                       final int length = buffer.readUnsignedShort();
+                       final byte[] value = ByteArray.readBytes(buffer, length);
                        LOG.trace("Parsing Node Descriptor: {}", Arrays.toString(value));
                        switch (type) {
                        case TlvCode.AS_NUMBER:
@@ -198,7 +188,6 @@ public final class LinkstateNlriParser implements NlriParser {
                        default:
                                throw new BGPParsingException("Node Descriptor not recognized, type: " + type);
                        }
-                       byteOffset += length;
                }
                LOG.trace("Finished parsing Node descriptors.");
                return (local) ? new LocalNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(
@@ -237,15 +226,12 @@ public final class LinkstateNlriParser implements NlriParser {
                throw new BGPParsingException("Router Id of invalid length " + value.length);
        }
 
-       private static PrefixDescriptors parsePrefixDescriptors(final byte[] bytes, final boolean ipv4) throws BGPParsingException {
-               int byteOffset = 0;
+       private static PrefixDescriptors parsePrefixDescriptors(final ByteBuf buffer, final boolean ipv4) throws BGPParsingException {
                final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
-               while (byteOffset != bytes.length) {
-                       final int type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TYPE_LENGTH));
-                       byteOffset += TYPE_LENGTH;
-                       final int length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
-                       final byte[] value = ByteArray.subByte(bytes, byteOffset, length);
+               while (buffer.readableBytes() != 0) {
+                       final int type = buffer.readUnsignedShort();
+                       final int length = buffer.readUnsignedShort();
+                       final byte[] value = ByteArray.readBytes(buffer, length);
                        LOG.trace("Parsing Prefix Descriptor: {}", Arrays.toString(value));
                        switch (type) {
                        case TlvCode.MULTI_TOPOLOGY_ID:
@@ -281,7 +267,6 @@ public final class LinkstateNlriParser implements NlriParser {
                        default:
                                throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
                        }
-                       byteOffset += length;
                }
                LOG.debug("Finished parsing Prefix descriptors.");
                return builder.build();
@@ -294,82 +279,72 @@ public final class LinkstateNlriParser implements NlriParser {
         * @return {@link CLinkstateDestination}
         * @throws BGPParsingException if parsing was unsuccessful
         */
-       private List<CLinkstateDestination> parseNlri(final byte[] nlri) throws BGPParsingException {
-               if (nlri.length == 0) {
+       private List<CLinkstateDestination> parseNlri(final ByteBuf nlri) throws BGPParsingException {
+               if (nlri.readableBytes() == 0) {
                        return null;
                }
-               int byteOffset = 0;
-
                final List<CLinkstateDestination> dests = Lists.newArrayList();
 
                CLinkstateDestinationBuilder builder = null;
 
-               while (byteOffset != nlri.length) {
+               while (nlri.readableBytes() != 0) {
                        builder = new CLinkstateDestinationBuilder();
-                       final NlriType type = NlriType.forValue(ByteArray.bytesToInt(ByteArray.subByte(nlri, byteOffset, TYPE_LENGTH)));
+                       final NlriType type = NlriType.forValue(nlri.readUnsignedShort());
                        builder.setNlriType(type);
 
-                       byteOffset += TYPE_LENGTH;
                        // length means total length of the tlvs including route distinguisher not including the type field
-                       final int length = ByteArray.bytesToInt(ByteArray.subByte(nlri, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
+                       final int length = nlri.readUnsignedShort();
                        RouteDistinguisher distinguisher = null;
                        if (this.isVpn) {
                                // this parses route distinguisher
-                               distinguisher = new RouteDistinguisher(BigInteger.valueOf(ByteArray.bytesToLong(ByteArray.subByte(nlri, byteOffset,
-                                               ROUTE_DISTINGUISHER_LENGTH))));
+                               distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
                                builder.setDistinguisher(distinguisher);
-                               byteOffset += ROUTE_DISTINGUISHER_LENGTH;
                        }
                        // parse source protocol
-                       final ProtocolId sp = ProtocolId.forValue(ByteArray.bytesToInt(ByteArray.subByte(nlri, byteOffset, PROTOCOL_ID_LENGTH)));
-                       byteOffset += PROTOCOL_ID_LENGTH;
+                       final ProtocolId sp = ProtocolId.forValue(UnsignedBytes.toInt(nlri.readByte()));
                        builder.setProtocolId(sp);
 
                        // parse identifier
-                       final Identifier identifier = new Identifier(BigInteger.valueOf(ByteArray.bytesToLong(ByteArray.subByte(nlri, byteOffset,
-                                       IDENTIFIER_LENGTH))));
-                       byteOffset += IDENTIFIER_LENGTH;
+                       final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
                        builder.setIdentifier(identifier);
 
                        // if we are dealing with linkstate nodes/links, parse local node descriptor
                        NodeIdentifier localDescriptor = null;
                        int locallength = 0;
-                       final int localtype = ByteArray.bytesToInt(ByteArray.subByte(nlri, byteOffset, TYPE_LENGTH));
-                       byteOffset += TYPE_LENGTH;
-                       locallength = ByteArray.bytesToInt(ByteArray.subByte(nlri, byteOffset, LENGTH_SIZE));
-                       byteOffset += LENGTH_SIZE;
+                       final int localtype = nlri.readUnsignedShort();
+                       locallength = nlri.readUnsignedShort();
                        if (localtype == TlvCode.LOCAL_NODE_DESCRIPTORS) {
-                               localDescriptor = parseNodeDescriptors(ByteArray.subByte(nlri, byteOffset, locallength), true);
+                               localDescriptor = parseNodeDescriptors(nlri.slice(nlri.readerIndex(), locallength), true);
                        }
-                       byteOffset += locallength;
+                       nlri.skipBytes(locallength);
                        builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
                        final int restLength = length - (this.isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
                                        - TYPE_LENGTH - LENGTH_SIZE - locallength;
                        LOG.trace("Restlength {}", restLength);
+                       ByteBuf rest = nlri.slice(nlri.readerIndex(), restLength);
                        switch (type) {
                        case Link:
-                               parseLink(builder, ByteArray.subByte(nlri, byteOffset, restLength));
+                               parseLink(builder, rest);
                                break;
                        case Ipv4Prefix:
-                               builder.setPrefixDescriptors(parsePrefixDescriptors(ByteArray.subByte(nlri, byteOffset, restLength), true));
+                               builder.setPrefixDescriptors(parsePrefixDescriptors(rest, true));
                                break;
                        case Ipv6Prefix:
-                               builder.setPrefixDescriptors(parsePrefixDescriptors(ByteArray.subByte(nlri, byteOffset, restLength), false));
+                               builder.setPrefixDescriptors(parsePrefixDescriptors(rest, false));
                                break;
                        case Node:
                                // node nlri is already parsed as it contains only the common fields for node and link nlri
                                break;
                        }
-                       byteOffset += restLength;
+                       nlri.skipBytes(restLength);
                        dests.add(builder.build());
                }
                return dests;
        }
 
        @Override
-       public void parseNlri(final byte[] nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
-               if (nlri.length == 0) {
+       public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
+               if (nlri.readableBytes() == 0) {
                        return;
                }
                final List<CLinkstateDestination> dst = parseNlri(nlri);
@@ -381,8 +356,8 @@ public final class LinkstateNlriParser implements NlriParser {
        }
 
        @Override
-       public void parseNlri(final byte[] nlri, final byte[] nextHop, final MpReachNlriBuilder builder) throws BGPParsingException {
-               if (nlri.length == 0) {
+       public void parseNlri(final ByteBuf nlri, final byte[] nextHop, final MpReachNlriBuilder builder) throws BGPParsingException {
+               if (nlri.readableBytes() == 0) {
                        return;
                }
                final List<CLinkstateDestination> dst = parseNlri(nlri);
index f57fbdaa1086c6468da5a62ce1f72157c3aba885..93c549ba499c952b06a10cbd406099d0483d66c7 100644 (file)
@@ -12,93 +12,93 @@ final class TlvCode {
        private TlvCode() {
        }
 
-       static final short LOCAL_NODE_DESCRIPTORS = 256;
+       static final int LOCAL_NODE_DESCRIPTORS = 256;
 
-       static final short REMOTE_NODE_DESCRIPTORS = 257;
+       static final int REMOTE_NODE_DESCRIPTORS = 257;
 
        /* Link Descriptor TLVs */
 
-       static final short LINK_LR_IDENTIFIERS = 258;
+       static final int LINK_LR_IDENTIFIERS = 258;
 
-       static final short IPV4_IFACE_ADDRESS = 259;
+       static final int IPV4_IFACE_ADDRESS = 259;
 
-       static final short IPV4_NEIGHBOR_ADDRESS = 260;
+       static final int IPV4_NEIGHBOR_ADDRESS = 260;
 
-       static final short IPV6_IFACE_ADDRESS = 261;
+       static final int IPV6_IFACE_ADDRESS = 261;
 
-       static final short IPV6_NEIGHBOR_ADDRESS = 262;
+       static final int IPV6_NEIGHBOR_ADDRESS = 262;
 
        /* Link Attribute TLVs */
 
-       static final short LOCAL_IPV4_ROUTER_ID = 1028;
+       static final int LOCAL_IPV4_ROUTER_ID = 1028;
 
-       static final short LOCAL_IPV6_ROUTER_ID = 1029;
+       static final int LOCAL_IPV6_ROUTER_ID = 1029;
 
-       static final short REMOTE_IPV4_ROUTER_ID = 1030;
+       static final int REMOTE_IPV4_ROUTER_ID = 1030;
 
-       static final short REMOTE_IPV6_ROUTER_ID = 1031;
+       static final int REMOTE_IPV6_ROUTER_ID = 1031;
 
-       static final short ADMIN_GROUP = 1088;
+       static final int ADMIN_GROUP = 1088;
 
-       static final short MAX_BANDWIDTH = 1089;
+       static final int MAX_BANDWIDTH = 1089;
 
-       static final short MAX_RESERVABLE_BANDWIDTH = 1090;
+       static final int MAX_RESERVABLE_BANDWIDTH = 1090;
 
-       static final short UNRESERVED_BANDWIDTH = 1091;
+       static final int UNRESERVED_BANDWIDTH = 1091;
 
-       static final short TE_METRIC = 1092;
+       static final int TE_METRIC = 1092;
 
-       static final short LINK_PROTECTION_TYPE = 1093;
+       static final int LINK_PROTECTION_TYPE = 1093;
 
-       static final short MPLS_PROTOCOL = 1094;
+       static final int MPLS_PROTOCOL = 1094;
 
-       static final short METRIC = 1095;
+       static final int METRIC = 1095;
 
-       static final short SHARED_RISK_LINK_GROUP = 1096;
+       static final int SHARED_RISK_LINK_GROUP = 1096;
 
-       static final short LINK_OPAQUE = 1097;
+       static final int LINK_OPAQUE = 1097;
 
-       static final short LINK_NAME = 1098;
+       static final int LINK_NAME = 1098;
 
        /* Prefix Descriptor TLVs */
 
-       static final short MULTI_TOPOLOGY_ID = 263;
+       static final int MULTI_TOPOLOGY_ID = 263;
 
-       static final short OSPF_ROUTE_TYPE = 264;
+       static final int OSPF_ROUTE_TYPE = 264;
 
-       static final short IP_REACHABILITY = 265;
+       static final int IP_REACHABILITY = 265;
 
        /* Prefix Attribute TLVs */
 
-       static final short IGP_FLAGS = 1152;
+       static final int IGP_FLAGS = 1152;
 
-       static final short ROUTE_TAG = 1153;
+       static final int ROUTE_TAG = 1153;
 
-       static final short EXTENDED_ROUTE_TAG = 1154;
+       static final int EXTENDED_ROUTE_TAG = 1154;
 
-       static final short PREFIX_METRIC = 1155;
+       static final int PREFIX_METRIC = 1155;
 
-       static final short FORWARDING_ADDRESS = 1156;
+       static final int FORWARDING_ADDRESS = 1156;
 
-       static final short PREFIX_OPAQUE = 1157;
+       static final int PREFIX_OPAQUE = 1157;
 
        /* Node Descriptor TLVs */
 
-       static final short AS_NUMBER = 512;
+       static final int AS_NUMBER = 512;
 
-       static final short BGP_LS_ID = 513;
+       static final int BGP_LS_ID = 513;
 
-       static final short AREA_ID = 514;
+       static final int AREA_ID = 514;
 
-       static final short IGP_ROUTER_ID = 515;
+       static final int IGP_ROUTER_ID = 515;
 
        /* Node Attribute TLVs */
 
-       static final short NODE_FLAG_BITS = 1024;
+       static final int NODE_FLAG_BITS = 1024;
 
-       static final short NODE_OPAQUE = 1025;
+       static final int NODE_OPAQUE = 1025;
 
-       static final short DYNAMIC_HOSTNAME = 1026;
+       static final int DYNAMIC_HOSTNAME = 1026;
 
-       static final short ISIS_AREA_IDENTIFIER = 1027;
+       static final int ISIS_AREA_IDENTIFIER = 1027;
 }
index ffeb00870348ad90437e82238a8bb2654348d833..dcebbe14fe70335d2305d0efc7e467107d76e362 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import io.netty.buffer.Unpooled;
 
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
@@ -39,35 +40,35 @@ import com.google.common.collect.Lists;
 public class LinkstateAttributeParserTest {
 
        private static final byte[] LINK_ATTR = new byte[] { (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0x2a, (byte) 0x2a,
-                       (byte) 0x2a, (byte) 0x2a, (byte) 0x04, (byte) 0x06, (byte) 0x00, (byte) 0x04, (byte) 0x2b, (byte) 0x2b, (byte) 0x2b,
-                       (byte) 0x2b, (byte) 0x04, (byte) 0x40, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-                       (byte) 0x04, (byte) 0x41, (byte) 0x00, (byte) 0x04, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x04,
-                       (byte) 0x42, (byte) 0x00, (byte) 0x04, (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00, (byte) 0x04, (byte) 0x43,
-                       (byte) 0x00, (byte) 0x20, (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00, (byte) 0x46, (byte) 0x43, (byte) 0x50,
-                       (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-                       (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-                       (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x44,
-                       (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x45, (byte) 0x00, (byte) 0x01,
-                       (byte) 0x08, (byte) 0x04, (byte) 0x46, (byte) 0x00, (byte) 0x01, (byte) 0xc0, (byte) 0x04, (byte) 0x47, (byte) 0x00,
-                       (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x04, (byte) 0x48, (byte) 0x00, (byte) 0x08, (byte) 0x12,
-                       (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x4a,
-                       (byte) 0x00, (byte) 0x05, (byte) 0x31, (byte) 0x32, (byte) 0x4b, (byte) 0x2d, (byte) 0x32,
-
-                       (byte) 0x04, (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
+               (byte) 0x2a, (byte) 0x2a, (byte) 0x04, (byte) 0x06, (byte) 0x00, (byte) 0x04, (byte) 0x2b, (byte) 0x2b, (byte) 0x2b,
+               (byte) 0x2b, (byte) 0x04, (byte) 0x40, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+               (byte) 0x04, (byte) 0x41, (byte) 0x00, (byte) 0x04, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x04,
+               (byte) 0x42, (byte) 0x00, (byte) 0x04, (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00, (byte) 0x04, (byte) 0x43,
+               (byte) 0x00, (byte) 0x20, (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00, (byte) 0x46, (byte) 0x43, (byte) 0x50,
+               (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+               (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+               (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x44,
+               (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x45, (byte) 0x00, (byte) 0x01,
+               (byte) 0x08, (byte) 0x04, (byte) 0x46, (byte) 0x00, (byte) 0x01, (byte) 0xc0, (byte) 0x04, (byte) 0x47, (byte) 0x00,
+               (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x04, (byte) 0x48, (byte) 0x00, (byte) 0x08, (byte) 0x12,
+               (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x4a,
+               (byte) 0x00, (byte) 0x05, (byte) 0x31, (byte) 0x32, (byte) 0x4b, (byte) 0x2d, (byte) 0x32,
+
+               (byte) 0x04, (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
 
        private static final byte[] NODE_ATTR = new byte[] { (byte) 0x01, (byte) 0x07, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x2a,
-                       (byte) 0x00, (byte) 0x2b, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xb0, (byte) 0x04, (byte) 0x02,
-                       (byte) 0x00, (byte) 0x05, (byte) 0x31, (byte) 0x32, (byte) 0x4b, (byte) 0x2d, (byte) 0x32, (byte) 0x04, (byte) 0x03,
-                       (byte) 0x00, (byte) 0x01, (byte) 0x72, (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0x29, (byte) 0x29,
-                       (byte) 0x29, (byte) 0x29, (byte) 0x04, (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
+               (byte) 0x00, (byte) 0x2b, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xb0, (byte) 0x04, (byte) 0x02,
+               (byte) 0x00, (byte) 0x05, (byte) 0x31, (byte) 0x32, (byte) 0x4b, (byte) 0x2d, (byte) 0x32, (byte) 0x04, (byte) 0x03,
+               (byte) 0x00, (byte) 0x01, (byte) 0x72, (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0x29, (byte) 0x29,
+               (byte) 0x29, (byte) 0x29, (byte) 0x04, (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
 
        private static final byte[] P4_ATTR = new byte[] { (byte) 0x04, (byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0xff, (byte) 0x04,
-                       (byte) 0x81, (byte) 0x00, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30,
-                       (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x82, (byte) 0x00, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56,
-                       (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x83, (byte) 0x00, (byte) 0x04,
-                       (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x04, (byte) 0x84, (byte) 0x00, (byte) 0x04, (byte) 0x0a,
-                       (byte) 0x19, (byte) 0x02, (byte) 0x1b, (byte) 0x04, (byte) 0x85, (byte) 0x00, (byte) 0x01, (byte) 0x0a, (byte) 0x04,
-                       (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
+               (byte) 0x81, (byte) 0x00, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30,
+               (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x82, (byte) 0x00, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56,
+               (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50, (byte) 0x70, (byte) 0x04, (byte) 0x83, (byte) 0x00, (byte) 0x04,
+               (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x04, (byte) 0x84, (byte) 0x00, (byte) 0x04, (byte) 0x0a,
+               (byte) 0x19, (byte) 0x02, (byte) 0x1b, (byte) 0x04, (byte) 0x85, (byte) 0x00, (byte) 0x01, (byte) 0x0a, (byte) 0x04,
+               (byte) 0x88, (byte) 0x00, (byte) 0x01, (byte) 0x0a };
 
        private final LinkstateAttributeParser parser = new LinkstateAttributeParser();
 
@@ -85,7 +86,7 @@ public class LinkstateAttributeParserTest {
        @Test
        public void testPositiveLinks() throws BGPParsingException {
                final PathAttributesBuilder builder = createBuilder(NlriType.Link);
-               this.parser.parseAttribute(LINK_ATTR, builder);
+               this.parser.parseAttribute(Unpooled.copiedBuffer(LINK_ATTR), builder);
                final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
                final LinkAttributes ls = ((LinkAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getLinkAttributes();
                assertNotNull(ls);
@@ -109,7 +110,7 @@ public class LinkstateAttributeParserTest {
        @Test
        public void testPositiveNodes() throws BGPParsingException {
                final PathAttributesBuilder builder = createBuilder(NlriType.Node);
-               this.parser.parseAttribute(NODE_ATTR, builder);
+               this.parser.parseAttribute(Unpooled.copiedBuffer(NODE_ATTR), builder);
 
                final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
                final NodeAttributes ls = ((NodeAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getNodeAttributes();
@@ -131,7 +132,7 @@ public class LinkstateAttributeParserTest {
        @Test
        public void testPositiveV4Prefixes() throws BGPParsingException {
                final PathAttributesBuilder builder = createBuilder(NlriType.Ipv4Prefix);
-               this.parser.parseAttribute(P4_ATTR, builder);
+               this.parser.parseAttribute(Unpooled.copiedBuffer(P4_ATTR), builder);
 
                final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
                final PrefixAttributes ls = ((PrefixAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getPrefixAttributes();
index a838059a534b254951f53b93a810c30c9dcc0f23..84fed1234b3c2c0951affae4b91fc5f63c0e47b1 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.bgp.linkstate;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import io.netty.buffer.Unpooled;
 
 import java.math.BigInteger;
 
@@ -75,7 +76,7 @@ public class LinkstateNlriParserTest {
        public void testNodeNlri() throws BGPParsingException {
                final LinkstateNlriParser parser = new LinkstateNlriParser(false);
                final MpReachNlriBuilder builder = new MpReachNlriBuilder();
-               parser.parseNlri(this.nodeNlri, this.nextHop, builder);
+               parser.parseNlri(Unpooled.copiedBuffer(this.nodeNlri), this.nextHop, builder);
 
                assertEquals("10.25.2.27", ((Ipv4NextHopCase) builder.getCNextHop()).getIpv4NextHop().getGlobal().getValue());
 
@@ -109,7 +110,7 @@ public class LinkstateNlriParserTest {
        public void testLinkNlri() throws BGPParsingException {
                final LinkstateNlriParser parser = new LinkstateNlriParser(false);
                final MpReachNlriBuilder builder = new MpReachNlriBuilder();
-               parser.parseNlri(this.linkNlri, this.nextHop, builder);
+               parser.parseNlri(Unpooled.copiedBuffer(this.linkNlri), this.nextHop, builder);
 
                assertEquals("10.25.2.27", ((Ipv4NextHopCase) builder.getCNextHop()).getIpv4NextHop().getGlobal().getValue());
 
@@ -148,7 +149,7 @@ public class LinkstateNlriParserTest {
        public void testPrefixNlri() throws BGPParsingException {
                final LinkstateNlriParser parser = new LinkstateNlriParser(false);
                final MpReachNlriBuilder builder = new MpReachNlriBuilder();
-               parser.parseNlri(this.prefixNlri, this.nextHop, builder);
+               parser.parseNlri(Unpooled.copiedBuffer(this.prefixNlri), this.nextHop, builder);
 
                assertEquals("10.25.2.27", ((Ipv4NextHopCase) builder.getCNextHop()).getIpv4NextHop().getGlobal().getValue());
 
index 9dce9495b3812d706072d0c339a90736aad0890e..1c10731f27b114aa8646300709607ccfeacf203a 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
@@ -24,8 +26,8 @@ public class BGPKeepAliveMessageParser implements MessageParser, MessageSerializ
        private final byte[] bytes = MessageUtil.formatMessage(TYPE, new byte[0]);
 
        @Override
-       public Keepalive parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
-               if (body.length != 0) {
+       public Keepalive parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
+               if (body.readableBytes() != 0) {
                        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
                }
                return this.msg;
index 671cb70b73a14c1815dbf7126fa3b794579aa7d9..44f41f651e0bc7a6cce00b3402e3fe80db6c3321 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -71,21 +73,21 @@ public final class BGPNotificationMessageParser implements MessageParser, Messag
         * @throws BGPDocumentedException
         */
        @Override
-       public Notify parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
+       public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
                if (body == null) {
                        throw new IllegalArgumentException("Byte array cannot be null.");
                }
-               LOG.trace("Started parsing of notification message: {}", Arrays.toString(body));
+               LOG.trace("Started parsing of notification message: {}", Arrays.toString(ByteArray.getAllBytes(body)));
 
-               if (body.length < ERROR_SIZE) {
+               if (body.readableBytes() < ERROR_SIZE) {
                        throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
                }
-               final int errorCode = UnsignedBytes.toInt(body[0]);
-               final int errorSubcode = UnsignedBytes.toInt(body[1]);
+               final int errorCode = UnsignedBytes.toInt(body.readByte());
+               final int errorSubcode = UnsignedBytes.toInt(body.readByte());
 
                byte[] data = null;
-               if (body.length > ERROR_SIZE) {
-                       data = ByteArray.subByte(body, ERROR_SIZE, body.length - ERROR_SIZE);
+               if (body.readableBytes() != 0) {
+                       data = ByteArray.readAllBytes(body);
                }
                LOG.trace("Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
                                Arrays.toString(data));
index 5e1f23f408ce2474d263b5b602a4f4f586274612..9e30fa1827b29c8088222e815b90fe0da27740d2 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -132,60 +134,51 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
         * @throws BGPDocumentedException if the parsing was unsuccessful
         */
        @Override
-       public Open parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
+       public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
                if (body == null) {
                        throw new IllegalArgumentException("Byte array cannot be null.");
                }
-               LOG.trace("Started parsing of open message: {}", Arrays.toString(body));
+               LOG.trace("Started parsing of open message: {}", Arrays.toString(ByteArray.getAllBytes(body)));
 
-               if (body.length < MIN_MSG_LENGTH) {
+               if (body.readableBytes() < MIN_MSG_LENGTH) {
                        throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
                }
-               if (UnsignedBytes.toInt(body[0]) != BGP_VERSION) {
-                       throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(body[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
+               int version = UnsignedBytes.toInt(body.readByte());
+               if (version != BGP_VERSION) {
+                       throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
                }
-               int offset = VERSION_SIZE;
-               final AsNumber as = new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(body, offset, AS_SIZE)));
-               offset += AS_SIZE;
-
-               final short holdTime = ByteArray.bytesToShort(ByteArray.subByte(body, offset, HOLD_TIME_SIZE));
-               offset += HOLD_TIME_SIZE;
+               final AsNumber as = new AsNumber((long) body.readUnsignedShort());
+               final int holdTime = body.readUnsignedShort();
                if (holdTime == 1 || holdTime == 2) {
                        throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
                }
                Ipv4Address bgpId = null;
                try {
-                       bgpId = Ipv4Util.addressForBytes(ByteArray.subByte(body, offset, BGP_ID_SIZE));
+                       bgpId = Ipv4Util.addressForBytes(ByteArray.readBytes(body, BGP_ID_SIZE));
                } catch (final IllegalArgumentException e) {
                        throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
                }
-               offset += BGP_ID_SIZE;
-
-               final int optLength = UnsignedBytes.toInt(body[offset]);
+               final int optLength = UnsignedBytes.toInt(body.readByte());
 
                final List<BgpParameters> optParams = Lists.newArrayList();
                if (optLength > 0) {
-                       fillParams(ByteArray.subByte(body, MIN_MSG_LENGTH, optLength), optParams);
+                       fillParams(body.slice(body.readerIndex(), optLength), optParams);
                }
                LOG.trace("Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
-               return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer((int) holdTime).setBgpIdentifier(bgpId).setBgpParameters(
+               return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(
                                optParams).build();
        }
 
-       private void fillParams(final byte[] bytes, final List<BgpParameters> params) throws BGPDocumentedException {
-               if (bytes == null || bytes.length == 0) {
-                       throw new IllegalArgumentException("Byte array cannot be null or empty.");
-               }
-               LOG.trace("Started parsing of BGP parameter: {}", Arrays.toString(bytes));
-               int byteOffset = 0;
-               while (byteOffset < bytes.length) {
-                       if (byteOffset + 2 >= bytes.length) {
-                               throw new BGPDocumentedException("Malformed parameter encountered (" + (bytes.length - byteOffset) + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
+       private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
+               Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
+               LOG.trace("Started parsing of BGP parameter: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
+               while (buffer.readableBytes() != 0) {
+                       if (buffer.readableBytes() <= 2) {
+                               throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
                        }
-                       final int paramType = UnsignedBytes.toInt(bytes[byteOffset++]);
-                       final int paramLength = UnsignedBytes.toInt(bytes[byteOffset++]);
-                       final byte[] paramBody = ByteArray.subByte(bytes, byteOffset, paramLength);
-                       byteOffset += paramLength;
+                       final int paramType = UnsignedBytes.toInt(buffer.readByte());
+                       final int paramLength = UnsignedBytes.toInt(buffer.readByte());
+                       final ByteBuf paramBody = buffer.slice(buffer.readerIndex(), paramLength);
 
                        final BgpParameters param;
                        try {
@@ -198,6 +191,7 @@ 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 b2f99679c095ebca4659fff163bc73fa055a2a2a..a1bad72479d1cfba3ea67d6e61681d904fbbfda7 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 import java.util.List;
 
@@ -59,40 +61,26 @@ public class BGPUpdateMessageParser implements MessageParser {
        // Getters & setters --------------------------------------------------
 
        @Override
-       public Update parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
-               if (body == null || body.length == 0) {
-                       throw new IllegalArgumentException("Byte array cannot be null or empty.");
-               }
-               LOG.trace("Started parsing of update message: {}", Arrays.toString(body));
-
-               int byteOffset = 0;
-
-               final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
-               byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
+       public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
+               Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
+               LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
 
+               final int withdrawnRoutesLength = buffer.readUnsignedShort();
                final UpdateBuilder eventBuilder = new UpdateBuilder();
 
                if (withdrawnRoutesLength > 0) {
-                       final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, withdrawnRoutesLength));
-                       byteOffset += withdrawnRoutesLength;
+                       final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
                        eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
                }
-
-               final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
-               byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
-
-               if (withdrawnRoutesLength + totalPathAttrLength > body.length) {
-                       throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
-               }
+               final int totalPathAttrLength = buffer.readUnsignedShort();
 
                if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
                        return eventBuilder.build();
                }
-
                if (totalPathAttrLength > 0) {
                        try {
-                               final PathAttributes pathAttributes = reg.parseAttributes(ByteArray.subByte(body, byteOffset, totalPathAttrLength));
-                               byteOffset += totalPathAttrLength;
+                               final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
+                               buffer.skipBytes(totalPathAttrLength);
                                eventBuilder.setPathAttributes(pathAttributes);
                        } catch (final BGPDocumentedException e) {
                                // Rethrow BGPDocumentedExceptions
@@ -103,8 +91,7 @@ public class BGPUpdateMessageParser implements MessageParser {
                                throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
                        }
                }
-
-               final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, body.length - byteOffset));
+               final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
                if (nlri != null && !nlri.isEmpty()) {
                        eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
                }
index ceb50e4c7c2d1780f9e6810d04eca58535f785cb..dc55abcda7006f412a9108b4c86c00ad6488d55e 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.open;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
@@ -25,9 +27,9 @@ public final class As4CapabilityHandler implements CapabilityParser, CapabilityS
        private static final int AS4_LENGTH = 4;
 
        @Override
-       public CParameters parseCapability(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
+       public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
                return new As4BytesCaseBuilder().setAs4BytesCapability(
-                               new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(ByteArray.bytesToLong(bytes))).build()).build();
+                               new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(buffer.readUnsignedInt())).build()).build();
        }
 
        @Override
index edc12e4f7e718b7a34a48dfe460df1681e5876a3..f4ec5c52513e566c58eaa00675ed7c5787306a36 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.open;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -39,22 +41,17 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
        }
 
        @Override
-       public BgpParameters parseParameter(final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
-               if (bytes == null || bytes.length == 0) {
-                       throw new IllegalArgumentException("Byte array cannot be null or empty.");
-               }
-               LOG.trace("Started parsing of BGP Capability: {}", Arrays.toString(bytes));
-               int byteOffset = 0;
-               final int capCode = UnsignedBytes.toInt(bytes[byteOffset++]);
-               final int capLength = UnsignedBytes.toInt(bytes[byteOffset++]);
-               final byte[] paramBody = ByteArray.subByte(bytes, byteOffset, capLength);
-
-               final CParameters ret = reg.parseCapability(capCode, paramBody);
+       public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
+               Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
+               LOG.trace("Started parsing of BGP Capability: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
+               final int capCode = UnsignedBytes.toInt(buffer.readByte());
+               final int capLength = UnsignedBytes.toInt(buffer.readByte());
+               final ByteBuf paramBody = buffer.slice(buffer.readerIndex(), capLength);
+               final CParameters ret = this.reg.parseCapability(capCode, paramBody);
                if (ret == null) {
                        LOG.debug("Ignoring unsupported capability {}", capCode);
                        return null;
                }
-
                return new BgpParametersBuilder().setCParameters(ret).build();
        }
 
@@ -64,7 +61,7 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
 
                LOG.trace("Started serializing BGP Capability: {}", cap);
 
-               byte[] bytes = reg.serializeCapability(cap);
+               byte[] bytes = this.reg.serializeCapability(cap);
                if (bytes == null) {
                        throw new IllegalArgumentException("Unhandled capability class" + cap.getImplementedInterface());
                }
index 868eecf3d4cece314406e4fe0ef2c2bb269a4478..5f328d27827455894831abe22ebcd63874be7594 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.open;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -108,30 +110,30 @@ public final class GracefulCapabilityHandler implements CapabilityParser, Capabi
        }
 
        @Override
-       public CParameters parseCapability(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
+       public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
                final GracefulRestartCapabilityBuilder cb = new GracefulRestartCapabilityBuilder();
 
-               final int flagBits = (bytes[0] >> RESTART_FLAGS_SIZE);
+               final int flagBits = (buffer.getByte(0) >> RESTART_FLAGS_SIZE);
                cb.setRestartFlags(new RestartFlags((flagBits & 8) != 0));
 
-               final int timer = ((bytes[0] & TIMER_TOPBITS_MASK) << RESTART_FLAGS_SIZE) + UnsignedBytes.toInt(bytes[1]);
+               final int timer = ((buffer.readByte() & TIMER_TOPBITS_MASK) << RESTART_FLAGS_SIZE) + UnsignedBytes.toInt(buffer.readByte());
                cb.setRestartTime(timer);
 
                final List<Tables> tables = new ArrayList<>();
-               for (int offset = HEADER_SIZE; offset < bytes.length; offset += PER_AFI_SAFI_SIZE) {
-                       final int afiVal = UnsignedBytes.toInt(bytes[offset]) * 256 + UnsignedBytes.toInt(bytes[offset + 1]);
+               while (buffer.readableBytes() != 0) {
+                       final int afiVal = UnsignedBytes.toInt(buffer.readByte()) * 256 + UnsignedBytes.toInt(buffer.readByte());
                        final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal);
                        if (afi == null) {
                                LOG.debug("Ignoring GR capability for unknown address family {}", afiVal);
                                continue;
                        }
-                       final int safiVal = UnsignedBytes.toInt(bytes[offset + 2]);
+                       final int safiVal = UnsignedBytes.toInt(buffer.readByte());
                        final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
                        if (safi == null) {
                                LOG.debug("Ignoring GR capability for unknown subsequent address family {}", safiVal);
                                continue;
                        }
-                       final int flags = UnsignedBytes.toInt(bytes[offset + 3]);
+                       final int flags = UnsignedBytes.toInt(buffer.readByte());
                        tables.add(new TablesBuilder().setAfi(afi).setSafi(safi).setAfiFlags(new AfiFlags((flags & AFI_FLAG_FORWARDING_STATE) != 0)).build());
                }
                return new GracefulRestartCaseBuilder().setGracefulRestartCapability(cb.build()).build();
index 218e4eae154357aeefb0bbdcc80c0d03128cf2ef..c69ed22036da4a3e077a0ae95777fa18eab873e6 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.open;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
@@ -14,7 +16,6 @@ import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilityUtil;
 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.CParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder;
@@ -28,9 +29,6 @@ import com.google.common.primitives.UnsignedBytes;
 public final class MultiProtocolCapabilityHandler implements CapabilityParser, CapabilitySerializer {
        public static final int CODE = 1;
 
-       private static final int AFI_SIZE = 2;
-       private static final int SAFI_SIZE = 1;
-
        private final AddressFamilyRegistry afiReg;
        private final SubsequentAddressFamilyRegistry safiReg;
 
@@ -40,19 +38,19 @@ public final class MultiProtocolCapabilityHandler implements CapabilityParser, C
        }
 
        @Override
-       public MultiprotocolCase parseCapability(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
-               final int afiVal = ByteArray.bytesToInt(ByteArray.subByte(bytes, 0, AFI_SIZE));
+       public MultiprotocolCase parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+               final int afiVal = buffer.readUnsignedShort();
                final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal);
                if (afi == null) {
                        throw new BGPParsingException("Address Family Identifier: '" + afiVal + "' not supported.");
                }
-
-               final int safiVal = ByteArray.bytesToInt(ByteArray.subByte(bytes, AFI_SIZE + 1, SAFI_SIZE));
+               // skip reserved
+               buffer.skipBytes(1);
+               final int safiVal = UnsignedBytes.toInt(buffer.readByte());
                final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
                if (safi == null) {
                        throw new BGPParsingException("Subsequent Address Family Identifier: '" + safiVal + "' not supported.");
                }
-
                return new MultiprotocolCaseBuilder().setMultiprotocolCapability(
                                new MultiprotocolCapabilityBuilder().setAfi(afi).setSafi(safi).build()).build();
        }
index 40c59d48aa4a54f9db920b1e543b34fe866c50b9..24a29f0c1134866285dcd9b4bc360f28cdb018e0 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
@@ -14,7 +16,7 @@ public final class AS4AggregatorAttributeParser implements AttributeParser {
        public static final int TYPE = 17;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
                // AS4 Aggregator is ignored
        }
 }
\ No newline at end of file
index 91be8a433149d303356163f2ac082e1629b266bf..07198fbd17fee9c5edd8d9109da45c9a25ad0d05 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
@@ -14,7 +16,7 @@ public final class AS4PathAttributeParser implements AttributeParser {
        public static final int TYPE = 18;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
+       public void parseAttribute(final ByteBuf bytes, final PathAttributesBuilder builder) {
                // AS4 Path is ignored
        }
 }
\ No newline at end of file
index 022b9579d664ac52421ae78c93602c023bd59145..4065ad4fd31fa3f3ccf99f47225feaff583e337b 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.util.ByteArray;
@@ -31,17 +33,13 @@ public final class AggregatorAttributeParser implements AttributeParser {
        /**
         * Parse AGGREGATOR from bytes
         * 
-        * @param bytes byte array to be parsed
+        * @param buffer byte buffer to be parsed
         * @return {@link Aggregator} BGP Aggregator
         */
-       private Aggregator parseAggregator(final byte[] bytes) {
-               final AsNumber asNumber = refCache.getSharedReference(new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, AsPathSegmentParser.AS_NUMBER_LENGTH))));
-               final Ipv4Address address = Ipv4Util.addressForBytes(ByteArray.subByte(bytes, AsPathSegmentParser.AS_NUMBER_LENGTH, 4));
-               return new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build();
-       }
-
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
-               builder.setAggregator(parseAggregator(bytes));
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+               final AsNumber asNumber = this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()));
+               final Ipv4Address address = Ipv4Util.addressForBytes(ByteArray.readAllBytes(buffer));
+               builder.setAggregator(new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build());
        }
 }
\ No newline at end of file
index 4a8f993413dcc9207772142235ce1c1d5a0b2199..aca66a73c117f416d4d1b03d44cd41454ca2046e 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.List;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -14,7 +16,6 @@ import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
@@ -48,44 +49,38 @@ public final class AsPathAttributeParser implements AttributeParser {
         * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
         * @throws BGPParsingException
         */
-       private static AsPath parseAsPath(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
-               int byteOffset = 0;
+       private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
                final List<Segments> ases = Lists.newArrayList();
                boolean isSequence = false;
-               while (byteOffset < bytes.length) {
-                       final int type = UnsignedBytes.toInt(bytes[byteOffset]);
+               while (buffer.isReadable()) {
+                       final int type = UnsignedBytes.toInt(buffer.readByte());
                        final SegmentType segmentType = AsPathSegmentParser.parseType(type);
                        if (segmentType == null) {
                                throw new BGPParsingException("AS Path segment type unknown : " + type);
                        }
-                       byteOffset += AsPathSegmentParser.TYPE_LENGTH;
-
-                       final int count = UnsignedBytes.toInt(bytes[byteOffset]);
-                       byteOffset += AsPathSegmentParser.LENGTH_SIZE;
+                       final int count = UnsignedBytes.toInt(buffer.readByte());
 
                        if (segmentType == SegmentType.AS_SEQUENCE) {
-                               final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count,
-                                               ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
+                               final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.slice(buffer.readerIndex(), 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,
-                                               ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
+                               final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.slice(buffer.readerIndex(), count * AsPathSegmentParser.AS_NUMBER_LENGTH));
                                ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
 
                        }
-                       byteOffset += count * AsPathSegmentParser.AS_NUMBER_LENGTH;
+                       buffer.skipBytes(count * AsPathSegmentParser.AS_NUMBER_LENGTH);
                }
 
-               if (!isSequence && bytes.length != 0) {
+               if (!isSequence && buffer.readableBytes() != 0) {
                        throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
                }
                return new AsPathBuilder().setSegments(ases).build();
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
-               builder.setAsPath(parseAsPath(refCache, bytes));
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
+               builder.setAsPath(parseAsPath(this.refCache, buffer));
        }
 }
\ No newline at end of file
index b856db25a8d92e30c5953494c090cb88b44a0827..fe28ed83a12e2db66692888e265e5fc8637ed0c9 100644 (file)
@@ -8,10 +8,11 @@
 
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.ArrayList;
 import java.util.List;
 
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
@@ -54,27 +55,23 @@ public final class AsPathSegmentParser {
                }
        }
 
-       static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final byte[] bytes) {
+       static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
                final List<AsSequence> coll = new ArrayList<>();
-               int byteOffset = 0;
                for (int i = 0; i < count; i++) {
                        coll.add(
                                        refCache.getSharedReference(
                                                        new AsSequenceBuilder().setAs(
                                                                        refCache.getSharedReference(
-                                                                                       new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH))))).build()));
-                       byteOffset += AS_NUMBER_LENGTH;
+                                                                                       new AsNumber(buffer.readUnsignedInt()))).build()));
                }
                return coll;
        }
 
-       static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final byte[] bytes) {
+       static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
                final List<AsNumber> coll = new ArrayList<>();
-               int byteOffset = 0;
                for (int i = 0; i < count; i++) {
                        coll.add(refCache.getSharedReference(
-                                       new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH)))));
-                       byteOffset += AS_NUMBER_LENGTH;
+                                       new AsNumber(buffer.readUnsignedInt())));
                }
                return coll;
        }
index a833b5a9cc2c4bca427f12ac6dcfb25aa8662ee8..6d63f86d809f1a09c8d0c3e225c78c4704123231 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AtomicAggregateBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
@@ -15,7 +17,7 @@ public final class AtomicAggregateAttributeParser implements AttributeParser {
        public static final int TYPE = 6;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
                builder.setAtomicAggregate(new AtomicAggregateBuilder().build());
        }
 }
\ No newline at end of file
index b6bc009d7a8a9126d1f504f32456da0455954c72..ec342ee8a75d4dff7dfa2c3d26b9ac5382f9db22 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.List;
 
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
@@ -23,14 +25,11 @@ public final class ClusterIdAttributeParser implements AttributeParser {
        private static final int CLUSTER_LENGTH = 4;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
                final List<ClusterIdentifier> list = Lists.newArrayList();
-               int i = 0;
-               while (i < bytes.length) {
-                       list.add(new ClusterIdentifier(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, i, CLUSTER_LENGTH))));
-                       i += CLUSTER_LENGTH;
+               while (buffer.isReadable()) {
+                       list.add(new ClusterIdentifier(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, CLUSTER_LENGTH))));
                }
-
                builder.setClusterId(list);
        }
 }
\ No newline at end of file
index 9e8359094df847c7d82feafa5dc30a18bc87c80e..ff445a24cbdda403da3d0a01da4fd5c1544aac2a 100644 (file)
@@ -7,11 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.List;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
@@ -29,12 +30,11 @@ public final class CommunitiesAttributeParser implements AttributeParser {
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
                final List<Communities> set = Lists.newArrayList();
-               int i = 0;
-               while (i < bytes.length) {
-                       set.add((Communities) CommunitiesParser.parseCommunity(refCache, ByteArray.subByte(bytes, i, CommunitiesParser.COMMUNITY_LENGTH)));
-                       i += CommunitiesParser.COMMUNITY_LENGTH;
+               while (buffer.isReadable()) {
+                       set.add((Communities) CommunitiesParser.parseCommunity(this.refCache, buffer.slice(buffer.readerIndex(), CommunitiesParser.COMMUNITY_LENGTH)));
+                       buffer.skipBytes(CommunitiesParser.COMMUNITY_LENGTH);
                }
 
                builder.setCommunities(set);
index ff360410d864dc66593971a21ed23dd7a73695f6..11e338554f88170938c410bea897ee3ba2115cb8 100644 (file)
@@ -5,9 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -42,12 +43,10 @@ public final class CommunitiesParser {
 
        protected static final int COMMUNITY_LENGTH = 4;
 
-       private static final int TYPE_LENGTH = 2;
-
-       private static final int AS_NUMBER_LENGTH = 2;
-
        private static final int AS_LOCAL_ADMIN_LENGTH = 4;
 
+       private static final int INET_LOCAL_ADMIN_LENGTH = 2;
+
        protected static final short AS_TYPE_TRANS = 0;
 
        protected static final short AS_TYPE_NON_TRANS = 40;
@@ -84,19 +83,19 @@ public final class CommunitiesParser {
         * @return new Community
         * @throws BGPDocumentedException
         */
-       static Community parseCommunity(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException {
-               if (bytes.length != COMMUNITY_LENGTH) {
-                       throw new BGPDocumentedException("Community with wrong length: " + bytes.length, BGPError.OPT_ATTR_ERROR);
+       static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
+               if (buffer.readableBytes() != COMMUNITY_LENGTH) {
+                       throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
                }
-               if (Arrays.equals(bytes, NO_EXPORT)) {
+               byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
+               if (Arrays.equals(body, NO_EXPORT)) {
                        return CommunityUtil.NO_EXPORT;
-               } else if (Arrays.equals(bytes, NO_ADVERTISE)) {
+               } else if (Arrays.equals(body, NO_ADVERTISE)) {
                        return CommunityUtil.NO_ADVERTISE;
-               } else if (Arrays.equals(bytes, NO_EXPORT_SUBCONFED)) {
+               } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
                        return CommunityUtil.NO_EXPORT_SUBCONFED;
                }
-               return CommunityUtil.create(refCache, ByteArray.bytesToLong(Arrays.copyOfRange(bytes, 0, AS_NUMBER_LENGTH)),
-                               ByteArray.bytesToInt(Arrays.copyOfRange(bytes, AS_NUMBER_LENGTH, AS_NUMBER_LENGTH + AS_NUMBER_LENGTH)));
+               return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
        }
 
        /**
@@ -107,10 +106,9 @@ public final class CommunitiesParser {
         * @throws BGPDocumentedException if the type is not recognized
         */
        @VisibleForTesting
-       public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException {
-               final int type = UnsignedBytes.toInt(bytes[0]);
-               final int subType = UnsignedBytes.toInt(bytes[1]);
-               final byte[] value = ByteArray.subByte(bytes, TYPE_LENGTH, bytes.length - TYPE_LENGTH);
+       public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
+               final int type = UnsignedBytes.toInt(buffer.readByte());
+               final int subType = UnsignedBytes.toInt(buffer.readByte());
 
                ExtendedCommunities comm;
                switch (type) {
@@ -119,42 +117,42 @@ public final class CommunitiesParser {
                                comm = new ExtendedCommunitiesBuilder().setCommType(AS_TYPE_TRANS).setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
                                                new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
                                                                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
                                comm = new ExtendedCommunitiesBuilder().setCommType(AS_TYPE_TRANS).setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
                                                new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
                                                                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else {
                                comm = new ExtendedCommunitiesBuilder().setCommType(AS_TYPE_TRANS).setExtendedCommunity(
                                                new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
                                                                new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        }
                        break;
                case AS_TYPE_NON_TRANS:
                        comm = new ExtendedCommunitiesBuilder().setCommType(AS_TYPE_NON_TRANS).setExtendedCommunity(
                                        new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
                                                        new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
-                                                                       new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                       ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                       new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                       ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        break;
                case ROUTE_TYPE_ONLY:
                        if (subType == ROUTE_TARGET_SUBTYPE) {
                                comm = new ExtendedCommunitiesBuilder().setCommType(ROUTE_TYPE_ONLY).setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
                                                new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
                                                                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
                                comm = new ExtendedCommunitiesBuilder().setCommType(ROUTE_TYPE_ONLY).setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
                                                new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
                                                                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else {
                                throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subType, BGPError.OPT_ATTR_ERROR);
                        }
@@ -164,38 +162,38 @@ public final class CommunitiesParser {
                                comm = new ExtendedCommunitiesBuilder().setCommType(INET_TYPE_TRANS).setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
                                                new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
                                                                new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
                                comm = new ExtendedCommunitiesBuilder().setCommType(INET_TYPE_TRANS).setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
                                                new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
                                                                new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
-                                                                               new ShortAsNumber(ByteArray.bytesToLong(ByteArray.subByte(value, 0, AS_NUMBER_LENGTH)))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, AS_NUMBER_LENGTH, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
+                                                                               new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        } else {
                                comm = new ExtendedCommunitiesBuilder().setCommType(INET_TYPE_TRANS).setExtendedCommunity(
                                                new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
                                                                new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
-                                                                               Ipv4Util.addressForBytes(ByteArray.subByte(value, 0, 4))).setLocalAdministrator(
-                                                                                               ByteArray.subByte(value, 4, 2)).build()).build()).build();
+                                                                               Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
+                                                                                               ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        }
                        break;
                case INET_TYPE_NON_TRANS:
                        comm = new ExtendedCommunitiesBuilder().setCommType(INET_TYPE_NON_TRANS).setExtendedCommunity(
                                        new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
                                                        new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
-                                                                       Ipv4Util.addressForBytes(ByteArray.subByte(value, 0, 4))).setLocalAdministrator(
-                                                                                       ByteArray.subByte(value, 4, 2)).build()).build()).build();
+                                                                       Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
+                                                                                       ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build()).build();
                        break;
                case OPAQUE_TYPE_TRANS:
                        comm = new ExtendedCommunitiesBuilder().setCommType(OPAQUE_TYPE_TRANS).setExtendedCommunity(
                                        new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
-                                                       new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(value).build()).build()).build();
+                                                       new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build()).build();
                        break;
                case OPAQUE_TYPE_NON_TRANS:
                        comm = new ExtendedCommunitiesBuilder().setCommType(OPAQUE_TYPE_NON_TRANS).setExtendedCommunity(
                                        new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
-                                                       new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(value).build()).build()).build();
+                                                       new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build()).build();
                        break;
                default:
                        throw new BGPDocumentedException("Could not parse Extended Community type: " + type, BGPError.OPT_ATTR_ERROR);
index 26f97398b4aadced75562a638762d853c073f988..992a56f836c090933300be51488a638959125205 100644 (file)
@@ -7,11 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.List;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
@@ -29,16 +30,13 @@ public final class ExtendedCommunitiesAttributeParser implements AttributeParser
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
                final List<ExtendedCommunities> set = Lists.newArrayList();
-               int i = 0;
-               while (i < bytes.length) {
-                       final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(refCache, ByteArray.subByte(bytes, i,
-                                       CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
-                       i += CommunitiesParser.EXTENDED_COMMUNITY_LENGTH;
+               while (buffer.isReadable()) {
+                       final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
+                       buffer.skipBytes(CommunitiesParser.EXTENDED_COMMUNITY_LENGTH);
                        set.add(comm);
                }
-
                builder.setExtendedCommunities(set);
        }
 }
\ No newline at end of file
index f1e5423a80fda93d80f0dcdafa4891f7e18047c1..dcfd109252a7bbf10a485b921345ff304c8c32f1 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
 import org.opendaylight.protocol.bgp.parser.spi.NlriUtil;
@@ -17,15 +19,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
 
 abstract class IpNlriParser implements NlriParser {
-       protected abstract DestinationType parseNlri(final byte[] nlri);
+       protected abstract DestinationType parseNlri(final ByteBuf nlri);
 
        @Override
-       public final void parseNlri(final byte[] nlri, final MpUnreachNlriBuilder builder) {
+       public final void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) {
                builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(parseNlri(nlri)).build());
        }
 
        @Override
-       public final void parseNlri(final byte[] nlri, final byte[] nextHop, final MpReachNlriBuilder builder) throws BGPParsingException {
+       public final void parseNlri(final ByteBuf nlri, final byte[] nextHop, final MpReachNlriBuilder builder) throws BGPParsingException {
                builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(parseNlri(nlri)).build());
                NlriUtil.parseNextHop(nextHop, builder);
        }
index 1a896df86bd9e8d5891e65bbf29931e15e6bb088..0a9bd37a69d87145ab85af6f280d988a07bcd641 100644 (file)
@@ -7,15 +7,18 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.concepts.Ipv4Util;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4CaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.destination.ipv4._case.DestinationIpv4Builder;
 
 public final class Ipv4NlriParser extends IpNlriParser {
        @Override
-       protected DestinationIpv4Case parseNlri(final byte[] nlri) {
+       protected DestinationIpv4Case parseNlri(final ByteBuf nlri) {
                return new DestinationIpv4CaseBuilder().setDestinationIpv4(
-                               new DestinationIpv4Builder().setIpv4Prefixes(Ipv4Util.prefixListForBytes(nlri)).build()).build();
+                               new DestinationIpv4Builder().setIpv4Prefixes(Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(nlri))).build()).build();
        }
 }
\ No newline at end of file
index 489ab76513053b0497ef5ef08670c465e40240e1..62998dddf6695183c9bb3c53026ad9ed1f006ecf 100644 (file)
@@ -7,15 +7,18 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.concepts.Ipv6Util;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Case;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6CaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.destination.ipv6._case.DestinationIpv6Builder;
 
 public final class Ipv6NlriParser extends IpNlriParser {
        @Override
-       protected DestinationIpv6Case parseNlri(final byte[] nlri) {
+       protected DestinationIpv6Case parseNlri(final ByteBuf nlri) {
                return new DestinationIpv6CaseBuilder().setDestinationIpv6(
-                               new DestinationIpv6Builder().setIpv6Prefixes(Ipv6Util.prefixListForBytes(nlri)).build()).build();
+                               new DestinationIpv6Builder().setIpv6Prefixes(Ipv6Util.prefixListForBytes(ByteArray.readAllBytes(nlri))).build()).build();
        }
 }
\ No newline at end of file
index 0d4cc372201ca78c2db78caf1d585316f91c15d7..641da556d6b587158e25cc7cc02a7edd3be24d42 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPrefBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
@@ -16,7 +17,7 @@ public final class LocalPreferenceAttributeParser implements AttributeParser {
        public static final int TYPE = 5;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
-               builder.setLocalPref(new LocalPrefBuilder().setPref(ByteArray.bytesToLong(bytes)).build());
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+               builder.setLocalPref(new LocalPrefBuilder().setPref(buffer.readUnsignedInt()).build());
        }
 }
\ No newline at end of file
index 234c986732107ab9baf2c2732ae1ec51b667ea65..7cf57f040c076bf11ea71aa775614360b6f64863 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
@@ -28,9 +30,9 @@ public final class MPReachAttributeParser implements AttributeParser {
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
                try {
-                       final PathAttributes1 a = new PathAttributes1Builder().setMpReachNlri(reg.parseMpReach(bytes)).build();
+                       final PathAttributes1 a = new PathAttributes1Builder().setMpReachNlri(this.reg.parseMpReach(buffer)).build();
                        builder.addAugmentation(PathAttributes1.class, a);
                } catch (final BGPParsingException e) {
                        throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
index 566d25e79322efc8c6823aa4652383c20c7cd137..7590644b3d3020ab1a276f55d19f8ccadeb525b7 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
@@ -28,9 +30,9 @@ public final class MPUnreachAttributeParser implements AttributeParser {
        }
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
                try {
-                       final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(reg.parseMpUnreach(bytes)).build();
+                       final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
                        builder.addAugmentation(PathAttributes2.class, a);
                } catch (final BGPParsingException e) {
                        throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
index bd0db5acfd76df31f0ffd333d943a289c04372bb..4051cf45995d6e6b455600f10d08cf4b70b5925f 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDiscBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
@@ -16,7 +17,7 @@ public final class MultiExitDiscriminatorAttributeParser implements AttributePar
        public static final int TYPE = 4;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
-               builder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(ByteArray.bytesToLong(bytes)).build());
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+               builder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(buffer.readUnsignedInt()).build());
        }
 }
\ No newline at end of file
index 1b6b879109a331ec3b723198cb3f485e3fe4bf1a..38c5ef868319fb404e43eb1ff43f8108cd5d3567 100644 (file)
@@ -7,18 +7,24 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.concepts.Ipv4Util;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
 
+import com.google.common.base.Preconditions;
+
 public final class NextHopAttributeParser implements AttributeParser {
        public static final int TYPE = 3;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+               Preconditions.checkArgument(buffer.readableBytes() == Ipv4Util.IP4_LENGTH, "Length of byte array for NEXT_HOP should be %s, but is %s", buffer.readableBytes(), Ipv4Util.IP4_LENGTH);
                builder.setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(
-                               new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForBytes(bytes)).build()).build());
+                               new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForBytes(ByteArray.readAllBytes(buffer))).build()).build());
        }
 }
\ No newline at end of file
index 9846767543a41d3db4a176b52436335f4c43fdb6..5fe59760fd0a098a78c5661eee53f60e3ad656c8 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
@@ -20,12 +22,12 @@ public final class OriginAttributeParser implements AttributeParser {
        public static final int TYPE = 1;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
-               final BgpOrigin borigin = BgpOrigin.forValue(UnsignedBytes.toInt(bytes[0]));
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
+               byte rawOrigin = buffer.readByte();
+               final BgpOrigin borigin = BgpOrigin.forValue(UnsignedBytes.toInt(rawOrigin));
                if (borigin == null) {
-                       throw new BGPDocumentedException("Unknown Origin type.", BGPError.ORIGIN_ATTR_NOT_VALID, new byte[] { (byte) 0x01, (byte) 0x01, bytes[0] });
+                       throw new BGPDocumentedException("Unknown Origin type.", BGPError.ORIGIN_ATTR_NOT_VALID, new byte[] { (byte) 0x01, (byte) 0x01, rawOrigin });
                }
-
                builder.setOrigin(new OriginBuilder().setValue(borigin).build());
        }
 }
\ No newline at end of file
index 42a79277b9227bc32085d70e36e5142000c2897a..f5fad7b1995daa7bf10e63990ed2331210d264ce 100644 (file)
@@ -7,21 +7,23 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.concepts.Ipv4Util;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
+import com.google.common.base.Preconditions;
+
 public final class OriginatorIdAttributeParser implements AttributeParser {
        public static final int TYPE = 9;
 
        private static final int ORIGINATOR_LENGTH = 4;
 
        @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) {
-               if (bytes.length != ORIGINATOR_LENGTH) {
-                       throw new IllegalArgumentException("Length of byte array for ORIGINATOR_ID should be " + ORIGINATOR_LENGTH + ", but is "
-                                       + bytes.length);
-               }
-               builder.setOriginatorId(Ipv4Util.addressForBytes(bytes));
+       public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
+               Preconditions.checkArgument(buffer.readableBytes() == ORIGINATOR_LENGTH, "Length of byte array for ORIGINATOR_ID should be %s, but is %s", ORIGINATOR_LENGTH, buffer.readableBytes());
+               builder.setOriginatorId(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, ORIGINATOR_LENGTH)));
        }
 }
\ No newline at end of file
index 15d5722b3a22a022ded5073077c763ec6ead9188..79464e6cdc6c0a2afccb6660031fba91bcb7a645 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.bgp.parser.impl;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import io.netty.buffer.Unpooled;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -205,7 +206,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                // check fields
 
@@ -323,7 +324,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                // check fields
                assertNull(message.getWithdrawnRoutes());
@@ -434,7 +435,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final UpdateBuilder builder = new UpdateBuilder();
 
@@ -530,7 +531,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final UpdateBuilder builder = new UpdateBuilder();
 
@@ -601,7 +602,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                // attributes
                final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
@@ -627,7 +628,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                assertEquals(new UpdateBuilder().build(), message);
        }
@@ -651,7 +652,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
                final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
@@ -679,7 +680,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(7), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
                final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
@@ -842,7 +843,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(8), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final UpdateBuilder builder = new UpdateBuilder();
 
@@ -1044,7 +1045,7 @@ public class BGPParserTest {
                final byte[] body = ByteArray.cutBytes(inputBytes.get(9), MessageUtil.COMMON_HEADER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), MessageUtil.MARKER_LENGTH,
                                MessageUtil.LENGTH_FIELD_LENGTH));
-               final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
+               final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
 
                final UpdateBuilder builder = new UpdateBuilder();
 
@@ -1169,7 +1170,7 @@ public class BGPParserTest {
        @Test
        public void testOpenMessage() throws Exception {
                final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
-               final Open open = (Open) msgReg.parseMessage(inputBytes.get(13));
+               final Open open = (Open) msgReg.parseMessage(Unpooled.copiedBuffer(inputBytes.get(13)));
                final Set<BgpTableType> types = Sets.newHashSet();
                for (final BgpParameters param : open.getBgpParameters()) {
                        final CParameters p = param.getCParameters();
index bcb01816ed7a1f738e26052894a3c7b71e2194ca..18ca225bae7b20654da27222151a5591e5497cbd 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.fail;
+import io.netty.buffer.Unpooled;
 
 import java.util.Map;
 
@@ -128,7 +129,7 @@ public class ComplementaryTest {
        public void testCommunitiesParser() {
                ExtendedCommunities as = null;
                try {
-                       as = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
+                       as = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -144,7 +145,7 @@ public class ComplementaryTest {
                assertEquals(0, as.getCommType().intValue());
 
                try {
-                       as = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
+                       as = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -157,7 +158,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities rtc = null;
                try {
-                       rtc = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
+                       rtc = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -174,7 +175,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities roc = null;
                try {
-                       roc = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
+                       roc = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -191,7 +192,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities sec = null;
                try {
-                       sec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
+                       sec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -209,7 +210,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities oec = null;
                try {
-                       oec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
+                       oec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -221,7 +222,7 @@ public class ComplementaryTest {
                assertEquals(3, oec.getCommType().intValue());
 
                try {
-                       oec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
+                       oec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }));
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -233,7 +234,7 @@ public class ComplementaryTest {
                assertEquals(43, oec.getCommType().intValue());
 
                try {
-                       CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
+                       CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Could not parse Extended Community type: 11", e.getMessage());
@@ -244,7 +245,7 @@ public class ComplementaryTest {
        public void testBGPHeaderParser() throws Exception {
                final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
                try {
-                       msgReg.parseMessage(new byte[] { (byte) 0, (byte) 0 });
+                       msgReg.parseMessage(Unpooled.copiedBuffer(new byte[] { (byte) 0, (byte) 0 }));
                        fail("Exception should have occured.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
index 0627b77b92d5d8d94d151c1d6d0cce18386ad78c..799079c7ad23292b3ef8bae494832f80ca583d10 100644 (file)
@@ -14,6 +14,7 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.matchers.JUnitMatchers.containsString;
+import io.netty.buffer.Unpooled;
 
 import java.net.UnknownHostException;
 import java.util.Arrays;
@@ -54,31 +55,31 @@ import com.google.common.collect.Maps;
 public class ParserTest {
 
        public static final byte[] openBMsg = 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, (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
-                       (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
+               (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+               (byte) 0xff, (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
+               (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
 
        public static final byte[] keepAliveBMsg = 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, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
+               (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+               (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
 
        public static final byte[] notificationBMsg = 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, (byte) 0x00, (byte) 0x17, (byte) 0x03, (byte) 0x02, (byte) 0x04, (byte) 0x04, (byte) 0x09 };
+               (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+               (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x17, (byte) 0x03, (byte) 0x02, (byte) 0x04, (byte) 0x04, (byte) 0x09 };
 
        public static final byte[] openWithCpblt1 = 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, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
-                       (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
-                       (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x40,
-                       (byte) 0x04, (byte) 0x00, (byte) 0x47 };
+               (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+               (byte) 0xff, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
+               (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
+               (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x40,
+               (byte) 0x04, (byte) 0x00, (byte) 0x47 };
 
        public static final byte[] openWithCpblt2 = 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, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
-                       (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
-                       (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
-                       (byte) 0x01, (byte) 0x00, (byte) 0x01 };
+               (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+               (byte) 0xff, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
+               (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
+               (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
+               (byte) 0x01, (byte) 0x00, (byte) 0x01 };
 
        static MessageRegistry reg;
 
@@ -93,7 +94,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 };
                wrong = ByteArray.cutBytes(wrong, 16);
                try {
-                       ParserTest.reg.parseMessage(wrong);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(wrong));
                        fail("Exception should have occcured.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("Too few bytes in passed array. Passed: " + wrong.length + ". Expected: >= 19.", e.getMessage());
@@ -108,7 +109,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                                (byte) 0x00, (byte) 0x13, (byte) 0x08 };
                try {
-                       ParserTest.reg.parseMessage(bytes);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals(BGPError.BAD_MSG_TYPE, e.getError());
@@ -123,7 +124,7 @@ public class ParserTest {
                final byte[] bytes = ParserTest.reg.serializeMessage(keepAlive);
                assertArrayEquals(keepAliveBMsg, bytes);
 
-               final Notification m = ParserTest.reg.parseMessage(bytes);
+               final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
 
                assertTrue(m instanceof Keepalive);
        }
@@ -135,7 +136,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x14, (byte) 0x04, (byte) 0x05 };
 
                try {
-                       ParserTest.reg.parseMessage(bytes);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertThat(e.getMessage(), containsString("Message length field not within valid range."));
@@ -152,7 +153,7 @@ public class ParserTest {
                final byte[] bytes = ParserTest.reg.serializeMessage(open);
                assertArrayEquals(openBMsg, bytes);
 
-               final Notification m = ParserTest.reg.parseMessage(bytes);
+               final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
 
                assertTrue(m instanceof Open);
                assertEquals(100, ((Open) m).getMyAsNumber().intValue());
@@ -169,7 +170,7 @@ public class ParserTest {
                                (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
 
                try {
-                       ParserTest.reg.parseMessage(bMsg);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Hold time value not acceptable.", e.getMessage());
@@ -187,7 +188,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff };
 
                try {
-                       ParserTest.reg.parseMessage(bMsg);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Open message too small.", e.getMessage());
@@ -203,7 +204,7 @@ public class ParserTest {
                                (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
 
                try {
-                       ParserTest.reg.parseMessage(bMsg);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("BGP Protocol version 8 not supported.", e.getMessage());
@@ -220,7 +221,7 @@ public class ParserTest {
                byte[] bytes = ParserTest.reg.serializeMessage(notMsg);
                assertArrayEquals(notificationBMsg, bytes);
 
-               Notification m = ParserTest.reg.parseMessage(bytes);
+               Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
 
                assertTrue(m instanceof Notify);
                assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
@@ -230,7 +231,7 @@ public class ParserTest {
                                BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
                bytes = ParserTest.reg.serializeMessage(notMsg);
 
-               m = ParserTest.reg.parseMessage(bytes);
+               m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes));
 
                assertTrue(m instanceof Notify);
                assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
@@ -244,7 +245,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x14, (byte) 0x03, (byte) 0x02 };
 
                try {
-                       ParserTest.reg.parseMessage(bMsg);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg));
                        fail("Exception should have occured.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Notification message too small.", e.getMessage());
@@ -261,7 +262,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x15, (byte) 0x03, (byte) 0x02, (byte) 0xaa };
 
                try {
-                       ParserTest.reg.parseMessage(bMsg);
+                       ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg));
                        fail("Exception should have occured.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("BGP Error code 2 and subcode 170 not recognized.", e.getMessage());
index edbabd5188eb073165bd7195e823b09a3592251d..d9fcf82a2971cc70bb1bea3ebc06a4c9e83e359f 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.bgp.parser.impl;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+import io.netty.buffer.Unpooled;
 
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -23,7 +24,7 @@ public class PathAttributeParserTest {
        @Test
        public void testOriginParser() throws Exception {
                try {
-                       ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getAttributeRegistry().parseAttributes(new byte[] { 0x40, 0x01, 0x01, 0x04 });
+                       ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getAttributeRegistry().parseAttributes(Unpooled.copiedBuffer(new byte[] { 0x40, 0x01, 0x01, 0x04 }));
                        fail("This needs to fail.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Unknown Origin type.", e.getMessage());
index 7957496f134168865155f4967014e975f586b424..42527c8a69eba7520d600267c466215c7a155dc5 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.mock;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Map;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -20,18 +22,18 @@ import org.opendaylight.yangtools.yang.binding.Notification;
  * the map.
  */
 public class BGPMessageParserMock implements MessageRegistry {
-       private final Map<byte[], Notification> messages;
+       private final Map<ByteBuf, Notification> messages;
 
        /**
         * @param updateMessages Map<byte[], BGPUpdateEvent>
         */
-       public BGPMessageParserMock(final Map<byte[], Notification> messages) {
+       public BGPMessageParserMock(final Map<ByteBuf, Notification> messages) {
                this.messages = messages;
        }
 
        @Override
-       public Notification parseMessage(final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
-               final Notification ret = this.messages.get(bytes);
+       public Notification parseMessage(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
+               final Notification ret = this.messages.get(buffer);
                if (ret == null) {
                        throw new IllegalArgumentException("Undefined message encountered");
                }
index 24eac59b94aa5a35a0ade785ee897f854be0037d..ff9d6b153f0bbb930c9d87401624a08a5489c68c 100644 (file)
@@ -9,6 +9,8 @@ package org.opendaylight.protocol.bgp.parser.mock;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -90,16 +92,16 @@ public class BGPMessageParserMockTest {
         */
        @Test
        public void testGetUpdateMessage() throws BGPParsingException, BGPDocumentedException, IOException {
-               final Map<byte[], Notification> updateMap = Maps.newHashMap();
+               final Map<ByteBuf, Notification> updateMap = Maps.newHashMap();
                for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], this.messages.get(i));
+                       updateMap.put(Unpooled.copiedBuffer(this.inputBytes[i]), this.messages.get(i));
                }
                final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
 
                for (int i = 0; i < this.inputBytes.length; i++) {
-                       assertEquals(this.messages.get(i), mockParser.parseMessage(this.inputBytes[i]));
+                       assertEquals(this.messages.get(i), mockParser.parseMessage(Unpooled.copiedBuffer(this.inputBytes[i])));
                }
-               assertNotSame(this.messages.get(3), mockParser.parseMessage(this.inputBytes[8]));
+               assertNotSame(this.messages.get(3), mockParser.parseMessage(Unpooled.copiedBuffer(this.inputBytes[8])));
        }
 
        /**
@@ -111,12 +113,12 @@ public class BGPMessageParserMockTest {
         */
        @Test(expected = IllegalArgumentException.class)
        public void testGetUpdateMessageException() throws BGPParsingException, BGPDocumentedException, IOException {
-               final Map<byte[], Notification> updateMap = Maps.newHashMap();
+               final Map<ByteBuf, Notification> updateMap = Maps.newHashMap();
                for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], this.messages.get(i));
+                       updateMap.put(Unpooled.copiedBuffer(this.inputBytes[i]), this.messages.get(i));
                }
                final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
-               mockParser.parseMessage(new byte[] { 7, 4, 6 });
+               mockParser.parseMessage(Unpooled.copiedBuffer(new byte[] { 7, 4, 6 }));
        }
 
        /**
@@ -178,7 +180,7 @@ public class BGPMessageParserMockTest {
 
        @Test
        public void testGetOpenMessage() throws BGPParsingException, BGPDocumentedException, IOException {
-               final Map<byte[], Notification> openMap = Maps.newHashMap();
+               final Map<ByteBuf, Notification> openMap = Maps.newHashMap();
 
                final Set<BgpTableType> type = Sets.newHashSet();
                type.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, MplsLabeledVpnSubsequentAddressFamily.class));
@@ -192,13 +194,13 @@ public class BGPMessageParserMockTest {
                final byte[] input = new byte[] { 5, 8, 13, 21 };
 
                openMap.put(
-                               input,
+                               Unpooled.copiedBuffer(input),
                                new OpenBuilder().setMyAsNumber(30).setHoldTimer(30).setBgpParameters(params).setVersion(new ProtocolVersion((short) 4)).build());
 
                final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
 
                final Set<BgpTableType> result = Sets.newHashSet();
-               for (final BgpParameters p : ((Open) mockParser.parseMessage(input)).getBgpParameters()) {
+               for (final BgpParameters p : ((Open) mockParser.parseMessage(Unpooled.copiedBuffer(input))).getBgpParameters()) {
                        final CParameters cp = p.getCParameters();
                        final BgpTableType t = new BgpTableTypeImpl(((MultiprotocolCase) cp).getMultiprotocolCapability().getAfi(), ((MultiprotocolCase) cp).getMultiprotocolCapability().getSafi());
                        result.add(t);
index 00fac29ca0467803ad5dc15d632f0d3391b1f561..2b482dd7a09a8c6f092c6f6e4ef08f8ebf58b55c 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Arrays;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -14,17 +16,15 @@ import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yangtools.yang.binding.Notification;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 public abstract class AbstractMessageRegistry implements MessageRegistry {
-       private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageRegistry.class);
 
        private static final byte[] MARKER;
 
-       protected abstract Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException;
+       protected abstract Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException;
 
        protected abstract byte[] serializeMessageImpl(final Notification message);
 
@@ -34,40 +34,33 @@ public abstract class AbstractMessageRegistry implements MessageRegistry {
        }
 
        @Override
-       public final Notification parseMessage(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
-               if (bytes == null) {
-                       throw new IllegalArgumentException("Array of bytes is mandatory.");
-               }
-               if (bytes.length < MessageUtil.COMMON_HEADER_LENGTH) {
-                       throw new IllegalArgumentException("Too few bytes in passed array. Passed: " + bytes.length + ". Expected: >= "
-                                       + MessageUtil.COMMON_HEADER_LENGTH + ".");
-               }
-               final byte[] marker = ByteArray.subByte(bytes, 0, MessageUtil.MARKER_LENGTH);
+       public final Notification parseMessage(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
+               Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Array of bytes cannot be null or empty.");
+               Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH);
+               final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH);
 
                if (!Arrays.equals(marker, MARKER)) {
                        throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
                }
-               final byte[] bs = ByteArray.cutBytes(bytes, MessageUtil.MARKER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, MessageUtil.LENGTH_FIELD_LENGTH));
-               final int messageType = UnsignedBytes.toInt(bs[MessageUtil.LENGTH_FIELD_LENGTH]);
+               final int messageLength = buffer.readUnsignedShort();
+               // to be sent with Error message
+               final byte typeBytes = buffer.readByte();
+               final int messageType = UnsignedBytes.toInt(typeBytes);
 
-               final byte[] msgBody = ByteArray.cutBytes(bs, MessageUtil.LENGTH_FIELD_LENGTH + MessageUtil.TYPE_FIELD_LENGTH);
+               final ByteBuf msgBody = buffer.slice(buffer.readerIndex(), messageLength - MessageUtil.COMMON_HEADER_LENGTH);
 
                if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
                        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
                }
-               if (msgBody.length != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
-                       throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
+               if (msgBody.readableBytes() != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
+                       throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + "; Expected: "
                                        + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
                }
-
-               LOG.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
-
                final Notification msg = parseBody(messageType, msgBody, messageLength);
                if (msg == null) {
-                       throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
+                       throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes });
                }
-               LOG.debug("Message parsed: {}", msg);
+               buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
                return msg;
        }
 
@@ -76,15 +69,10 @@ public abstract class AbstractMessageRegistry implements MessageRegistry {
                if (message == null) {
                        throw new IllegalArgumentException("BGPMessage is mandatory.");
                }
-
-               LOG.trace("Serializing {}", message);
-
                final byte[] ret = serializeMessageImpl(message);
                if (ret == null) {
                        throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass());
                }
-
-               LOG.trace("Serialized BGP message {}.", Arrays.toString(ret));
                return ret;
        }
 }
index d0971085f905e38981de587cb93196e3967be54c..aa923da39a4ad16fe6891e15679b0bd4a07408a0 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
@@ -16,9 +18,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
  */
 public interface AttributeParser {
        /**
-        * @param body encoded attribute body
+        * @param buffer encoded attribute body in Bytebuf
         * @param builder Path attributes builder. Guaranteed to contain all valid attributes whose type
         *                is numerically lower than this attribute's type.
         */
-       void parseAttribute(final byte[] body, PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException;
+       void parseAttribute(final ByteBuf buffer, PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException;
 }
index 0cff08e207f1b403f6a462c13eecdfd9f69f09f8..e1999e634084ca1474deb67a56dca352f33802ec 100644 (file)
@@ -7,12 +7,14 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 
 public interface AttributeRegistry {
-       PathAttributes parseAttributes(byte[] bytes) throws BGPDocumentedException, BGPParsingException;
+       PathAttributes parseAttributes(ByteBuf buffer) throws BGPDocumentedException, BGPParsingException;
        byte[] serializeAttribute(DataObject attribute);
 }
index 3726a2984d0a302ad508fcbc08220281570ddec7..471fe9567a1687ea2a6b4b5955a2fcabbad4a7f4 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.CParameters;
 
 public interface CapabilityParser {
-       CParameters parseCapability(byte[] bytes) throws BGPDocumentedException, BGPParsingException;
+       CParameters parseCapability(ByteBuf buffer) throws BGPDocumentedException, BGPParsingException;
 }
index 289a19f01cac315fd6ac9d403931e9a90de86485..870647aa28eb1ae081a0a5be77b3af28c8fe4d2a 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.CParameters;
 
 public interface CapabilityRegistry {
-       CParameters parseCapability(int type, byte[] bytes) throws BGPDocumentedException, BGPParsingException;
+       CParameters parseCapability(int type, ByteBuf buffer) throws BGPDocumentedException, BGPParsingException;
        byte[] serializeCapability(CParameters capability);
 }
index e6b7f2502d2b973bfebbe5c8987aa0253a83b5cd..d8601663ab0f0b5652459f2305d1592185e83315 100644 (file)
@@ -7,9 +7,11 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public interface MessageParser {
-       Notification parseMessageBody(byte[] body, int messageLength) throws BGPDocumentedException;
+       Notification parseMessageBody(ByteBuf body, int messageLength) throws BGPDocumentedException;
 }
index adf364314d0e0f91929574a18ab2a9fc1c5c5d89..db98d714afaa760cca677ae9410a645710747611 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public interface MessageRegistry {
-       Notification parseMessage(byte[] bytes) throws BGPDocumentedException, BGPParsingException;
+       Notification parseMessage(ByteBuf bytes) throws BGPDocumentedException, BGPParsingException;
        byte[] serializeMessage(Notification message);
 }
index db0c8814cf41c3f411b68baa4679f0317d7c6a3f..fa7cd32d7a39e3edda26a4cb9b211ce0ead4bc28 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
 
 public interface NlriParser {
-       void parseNlri(byte[] nlri, MpUnreachNlriBuilder builder) throws BGPParsingException;
-       void parseNlri(byte[] nlri, byte[] nextHop, MpReachNlriBuilder builder) throws BGPParsingException;
+       void parseNlri(ByteBuf nlri, MpUnreachNlriBuilder builder) throws BGPParsingException;
+       void parseNlri(ByteBuf nlri, byte[] nextHop, MpReachNlriBuilder builder) throws BGPParsingException;
 }
index 8294bd516a5564711033ee1d79f717981923ec99..00c877295d47522a1855a4a423f8bae82d861c32 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
 
 public interface NlriRegistry {
-       MpReachNlri parseMpReach(final byte[] bytes) throws BGPParsingException;
-       MpUnreachNlri parseMpUnreach(final byte[] bytes) throws BGPParsingException;
+       MpReachNlri parseMpReach(final ByteBuf buffer) throws BGPParsingException;
+       MpUnreachNlri parseMpUnreach(final ByteBuf buffer) throws BGPParsingException;
 }
index 32f4c22da8011434818e50242b5cf1f39dcc8f7e..a07513889f8afaa136f16d7416b7ad08fcd5a870 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
 
 public interface ParameterParser {
-       BgpParameters parseParameter(byte[] bytes) throws BGPParsingException, BGPDocumentedException;
+       BgpParameters parseParameter(ByteBuf buffer) throws BGPParsingException, BGPDocumentedException;
 }
index 394767b4f89639eb280a2dc476d689343aa3db5b..4500ecb0b0226752f1e40327a25d299881fff995 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
 
 public interface ParameterRegistry {
-       BgpParameters parseParameter(int parameterType, byte[] bytes) throws BGPParsingException, BGPDocumentedException;
+       BgpParameters parseParameter(int parameterType, ByteBuf bytes) throws BGPParsingException, BGPDocumentedException;
        byte[] serializeParameter(BgpParameters parameter);
 }
index ee3d3aa5d87de114d674530e4f0d0880f1aa773e..0316ac23b4517d2df70ba5523c33ed9644bd6cc4 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.TreeMap;
@@ -33,11 +35,11 @@ import com.google.common.primitives.UnsignedBytes;
 final class SimpleAttributeRegistry implements AttributeRegistry {
        private static final class RawAttribute {
                private final AttributeParser parser;
-               private final byte[] body;
+               private final ByteBuf buffer;
 
-               public RawAttribute(final AttributeParser parser, final byte[] body) {
+               public RawAttribute(final AttributeParser parser, final ByteBuf buffer) {
                        this.parser = Preconditions.checkNotNull(parser);
-                       this.body = Preconditions.checkNotNull(body);
+                       this.buffer = Preconditions.checkNotNull(buffer);
                }
        }
 
@@ -57,19 +59,18 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                return this.handlers.registerSerializer(paramClass, serializer);
        }
 
-       private int addAttribute(final byte[] bytes, final int offset, final Map<Integer, RawAttribute> attributes) throws BGPDocumentedException {
-               final boolean[] flags = ByteArray.parseBits(bytes[offset]);
-               final Integer type = UnsignedBytes.toInt(bytes[offset + 1]);
+       private int addAttribute(final ByteBuf buffer, final Map<Integer, RawAttribute> attributes) throws BGPDocumentedException {
+               final boolean[] flags = ByteArray.parseBits(buffer.readByte());
+               final Integer type = UnsignedBytes.toInt(buffer.readByte());
                final int hdrlen;
                final int len;
                if (flags[EXTENDED_LENGTH_BIT]) {
-                       len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
+                       len = UnsignedBytes.toInt(buffer.readByte()) * 256 + UnsignedBytes.toInt(buffer.readByte());
                        hdrlen = 4;
                } else {
-                       len = UnsignedBytes.toInt(bytes[offset + 2]);
+                       len = UnsignedBytes.toInt(buffer.readByte());
                        hdrlen = 3;
                }
-
                if (!attributes.containsKey(type)) {
                        final AttributeParser parser = this.handlers.getParser(type);
                        if (parser == null) {
@@ -83,7 +84,8 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                                        LOG.debug("Ignoring unrecognized attribute type {}", type);
                                }
                        } else {
-                               attributes.put(type, new RawAttribute(parser, ByteArray.subByte(bytes, offset + hdrlen, len)));
+                               attributes.put(type, new RawAttribute(parser, buffer.slice(buffer.readerIndex(), len)));
+                               buffer.skipBytes(len);
                        }
                } else {
                        LOG.debug("Ignoring duplicate attribute type {}", type);
@@ -92,14 +94,11 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
        }
 
        @Override
-       public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
-               int byteOffset = 0;
-
+       public PathAttributes parseAttributes(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
                final TreeMap<Integer, RawAttribute> attributes = new TreeMap<>();
-               while (byteOffset < bytes.length) {
-                       byteOffset += addAttribute(bytes, byteOffset, attributes);
+               while (buffer.readableBytes() != 0) {
+                       addAttribute(buffer, attributes);
                }
-
                /*
                 * TreeMap guarantees that we will be invoking the parser in the order
                 * of increasing attribute type.
@@ -109,7 +108,7 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                        LOG.debug("Parsing attribute type {}", e.getKey());
 
                        final RawAttribute a = e.getValue();
-                       a.parser.parseAttribute(a.body, builder);
+                       a.parser.parseAttribute(a.buffer, builder);
                }
 
                return builder.build();
@@ -121,7 +120,6 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                if (serializer == null) {
                        return null;
                }
-
                return serializer.serializeAttribute(attribute);
        }
 }
index aed366e4114c2d1d4ea54e17d46793398ac3ee96..0f870b58dc95a6502cf35a403b915dea4aa55841 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
@@ -32,13 +34,12 @@ final class SimpleCapabilityRegistry implements CapabilityRegistry {
        }
 
        @Override
-       public CParameters parseCapability(final int type, final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
+       public CParameters parseCapability(final int type, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
                final CapabilityParser parser = this.handlers.getParser(type);
                if (parser == null) {
                        return null;
                }
-
-               return parser.parseCapability(bytes);
+               return parser.parseCapability(buffer);
        }
 
        @Override
index 9c6b6cfcf798e480f63ca7f1305a96991bc91750..e166d6f9f1688f932e60b752d5ddbec69241c251 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.AbstractMessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
@@ -19,8 +21,8 @@ final class SimpleMessageRegistry extends AbstractMessageRegistry {
        private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
 
        @Override
-       protected Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException {
-               final MessageParser parser = handlers.getParser(type);
+       protected Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException {
+               final MessageParser parser = this.handlers.getParser(type);
                if (parser == null) {
                        return null;
                }
@@ -30,7 +32,7 @@ final class SimpleMessageRegistry extends AbstractMessageRegistry {
 
        @Override
        protected byte[] serializeMessageImpl(final Notification message) {
-               final MessageSerializer serializer = handlers.getSerializer(message.getImplementedInterface());
+               final MessageSerializer serializer = this.handlers.getSerializer(message.getImplementedInterface());
                if (serializer == null) {
                        return null;
                }
@@ -39,10 +41,10 @@ final class SimpleMessageRegistry extends AbstractMessageRegistry {
        }
 
        AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
-               return handlers.registerParser(messageType, parser);
+               return this.handlers.registerParser(messageType, parser);
        }
 
        AutoCloseable registerMessageSerializer(final Class<? extends Notification> messageClass, final MessageSerializer serializer) {
-               return handlers.registerSerializer(messageClass, serializer);
+               return this.handlers.registerSerializer(messageClass, serializer);
        }
 }
index 5f49fc0d36d58e0eb44807e756e0e43bc8a9c663..673162d856e368e3201411485a149d9cf9f8dfd2 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -49,70 +51,66 @@ final class SimpleNlriRegistry implements NlriRegistry {
        synchronized AutoCloseable registerNlriParser(final Class<? extends AddressFamily> afi,
                        final Class<? extends SubsequentAddressFamily> safi, final NlriParser parser) {
                final BgpTableType key = createKey(afi, safi);
-               final NlriParser prev = handlers.get(key);
+               final NlriParser prev = this.handlers.get(key);
                Preconditions.checkState(prev == null, "AFI/SAFI is already bound to parser " + prev);
 
-               handlers.put(key, parser);
+               this.handlers.put(key, parser);
                final Object lock = this;
                return new AbstractRegistration() {
                        @Override
                        protected void removeRegistration() {
                                synchronized (lock) {
-                                       handlers.remove(key);
+                                       SimpleNlriRegistry.this.handlers.remove(key);
                                }
                        }
                };
        }
 
-       private Class<? extends AddressFamily> getAfi(final byte[] header) throws BGPParsingException {
-               final int afiVal = UnsignedBytes.toInt(header[0]) * 256 + UnsignedBytes.toInt(header[1]);
-               final Class<? extends AddressFamily> afi = afiReg.classForFamily(afiVal);
+       private Class<? extends AddressFamily> getAfi(final ByteBuf buffer) throws BGPParsingException {
+               final int afiVal = buffer.readUnsignedShort();
+               final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal);
                if (afi == null) {
                        throw new BGPParsingException("Address Family Identifier: '" + afiVal + "' not supported.");
                }
-
                return afi;
        }
 
-       private Class<? extends SubsequentAddressFamily> getSafi(final byte[] header) throws BGPParsingException {
-               final int safiVal = UnsignedBytes.toInt(header[2]);
-               final Class<? extends SubsequentAddressFamily> safi = safiReg.classForFamily(safiVal);
+       private Class<? extends SubsequentAddressFamily> getSafi(final ByteBuf buffer) throws BGPParsingException {
+               final int safiVal = UnsignedBytes.toInt(buffer.readByte());
+               final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
                if (safi == null) {
                        throw new BGPParsingException("Subsequent Address Family Identifier: '" + safiVal + "' not supported.");
                }
-
                return safi;
        }
 
        @Override
-       public MpUnreachNlri parseMpUnreach(final byte[] bytes) throws BGPParsingException {
+       public MpUnreachNlri parseMpUnreach(final ByteBuf buffer) throws BGPParsingException {
                final MpUnreachNlriBuilder builder = new MpUnreachNlriBuilder();
-               builder.setAfi(getAfi(bytes));
-               builder.setSafi(getSafi(bytes));
-
-               final NlriParser parser = handlers.get(createKey(builder.getAfi(), builder.getSafi()));
-               parser.parseNlri(ByteArray.subByte(bytes, 3, bytes.length - 3), builder);
+               builder.setAfi(getAfi(buffer));
+               builder.setSafi(getSafi(buffer));
 
+               final NlriParser parser = this.handlers.get(createKey(builder.getAfi(), builder.getSafi()));
+               final ByteBuf nlri = buffer.slice();
+               parser.parseNlri(nlri, builder);
                return builder.build();
        }
 
        @Override
-       public MpReachNlri parseMpReach(final byte[] bytes) throws BGPParsingException {
+       public MpReachNlri parseMpReach(final ByteBuf buffer) throws BGPParsingException {
                final MpReachNlriBuilder builder = new MpReachNlriBuilder();
-               builder.setAfi(getAfi(bytes));
-               builder.setSafi(getSafi(bytes));
-
-               final NlriParser parser = handlers.get(createKey(builder.getAfi(), builder.getSafi()));
+               builder.setAfi(getAfi(buffer));
+               builder.setSafi(getSafi(buffer));
 
-               final int nextHopLength = UnsignedBytes.toInt(bytes[3]);
-               int byteOffset = 4;
+               final NlriParser parser = this.handlers.get(createKey(builder.getAfi(), builder.getSafi()));
 
-               final byte[] nextHop = ByteArray.subByte(bytes, byteOffset, nextHopLength);
-               byteOffset += nextHopLength + 1;
+               final int nextHopLength = UnsignedBytes.toInt(buffer.readByte());
+               final byte[] nextHop = ByteArray.readBytes(buffer, nextHopLength);
+               //reserved
+               buffer.skipBytes(1);
 
-               final byte[] nlri = ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset);
+               final ByteBuf nlri = buffer.slice();
                parser.parseNlri(nlri, nextHop, builder);
-
                return builder.build();
        }
 }
index e400a9ca8e626751039773ea183394fc72bded79..ed2fe497cea220c0f2d93bd33978b740520f8bf3 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
@@ -32,13 +34,12 @@ final class SimpleParameterRegistry implements ParameterRegistry {
        }
 
        @Override
-       public BgpParameters parseParameter(final int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
+       public BgpParameters parseParameter(final int parameterType, final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
                final ParameterParser parser = this.handlers.getParser(parameterType);
                if (parser == null) {
                        return null;
                }
-
-               return parser.parseParameter(bytes);
+               return parser.parseParameter(buffer);
        }
 
        @Override
@@ -47,7 +48,6 @@ final class SimpleParameterRegistry implements ParameterRegistry {
                if (serializer == null) {
                        return null;
                }
-
                return serializer.serializeParameter(parameter);
        }
 }
index 5cb2c2bfd7aac575e3f74dbf1c4dd1a43949d514..5b781f68d6a69de0cd8c0f33b99f1e4819e15bc6 100644 (file)
@@ -39,16 +39,12 @@ final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
                        LOG.debug("No more content in incoming buffer.");
                        return;
                }
-               in.markReaderIndex();
                try {
                        LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
-                       final byte[] bytes = new byte[in.readableBytes()];
-                       in.readBytes(bytes);
-                       out.add(this.registry.parseMessage(bytes));
+                       out.add(this.registry.parseMessage(in));
                } catch (BGPParsingException | BGPDocumentedException e) {
                        LOG.debug("Failed to decode protocol message", e);
                        this.exceptionCaught(ctx, e);
                }
-               in.discardReadBytes();
        }
 }
index ea83c2edd87f930351bb7e1bb11319046b93c00e..a5236e6032d78334a4e7162d6e283b3d2153bc13 100644 (file)
@@ -169,7 +169,7 @@ public final class BGPSessionNegotiator extends AbstractSessionNegotiator<Notifi
                final List<BgpParameters> prefs = openObj.getBgpParameters();
                if (prefs != null && !prefs.isEmpty()) {
                        if (!prefs.containsAll(this.localPref.getParams())) {
-                               LOG.info("Open message recieved does not match open message sent. Session still accepted.");
+                               LOG.info("Open message session parameters differ, session still accepted.");
                        }
                        this.sendMessage(new KeepaliveBuilder().build());
                        this.session = new BGPSessionImpl(this.timer, this.listener, this.channel, openObj, this.localPref.getHoldTime());
index 06aa42765ecfadf73d4485ba2a16b47cbee040d0..b370adf7320de54febc24f6bd54e17f376c26d01 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.rib.mock;
 
+import io.netty.buffer.Unpooled;
+
 import java.io.Closeable;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -62,7 +64,7 @@ public final class BGPMock implements Closeable {
 
                                final byte[] body = ByteArray.cutBytes(b, 1);
 
-                               messages.add(registry.parseMessage(body));
+                               messages.add(registry.parseMessage(Unpooled.copiedBuffer(body)));
                        }
                } catch (final BGPDocumentedException | BGPParsingException e) {
                        LOG.warn("Failed to parse message {}", e);
index 3d89ded03c25a2b10c5c369271fa8c04fdc240c3..1d56162c81936178909e6bcfce300fc1d09e39cd 100644 (file)
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+               <dependency>
+                       <groupId>io.netty</groupId>
+                       <artifactId>netty-buffer</artifactId>
+               </dependency>
 
         <!-- Testing dependencies -->
         <dependency>
index b208c0bc9652d787d2edda9d60358fb6b209394b..536d1e0feb64452f39ee952f7196e3e845250932 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.util;
 
+import io.netty.buffer.ByteBuf;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -28,6 +30,62 @@ public final class ByteArray {
        private ByteArray() {
        }
 
+       /**
+        * Helper method missing from netty ByteBuf methods. Directly returns byte
+        * array part of the given buffer, starting at reader index, with given length.
+        * Increases reader index of the buffer by 'length'.
+        * 
+        * @param buffer ByteBuf from which the bytes are going to be taken
+        * @param length length of the returned byte array
+        * @return byte array
+        */
+       public static byte[] readBytes(final ByteBuf buffer, int length) {
+               Preconditions.checkState(buffer != null && buffer.readableBytes() >= length, "Buffer cannot be read for %s bytes. Contains only %s bytes.", length, buffer.readableBytes());
+               byte[] result = new byte[length];
+               buffer.readBytes(result);
+               return result;
+       }
+
+       /**
+        * Helper method missing from netty ByteBuf methods. Directly returns
+        * all readable bytes from buffer as byte array. Adjusts reader index
+        * of the buffer by length of readable bytes in the buffer.
+        * 
+        * @param buffer byteBuf from which the bytes are going to be taken
+        * @return byte array
+        */
+       public static byte[] readAllBytes(final ByteBuf buffer) {
+               return readBytes(buffer, buffer.readableBytes());
+       }
+
+       /**
+        * Helper method missing from netty ByteBuf methods. Directly returns byte
+        * array part of the given buffer, starting at reader index, with given length.
+        * Does not modify reader or writer index of the buffer.
+        * 
+        * @param buffer ByteBuf from which the bytes are going to be taken
+        * @param length length of the returned byte array
+        * @return byte array
+        */
+       public static byte[] getBytes(final ByteBuf buffer, int length) {
+               Preconditions.checkState(buffer != null && buffer.readableBytes() >= length, "Buffer cannot be read for %s bytes. Contains only %s bytes.", length, buffer.readableBytes());
+               byte[] result = new byte[length];
+               buffer.getBytes(buffer.readerIndex(), result);
+               return result;
+       }
+
+       /**
+        * Helper method missing from netty ByteBuf methods. Directly returns
+        * all readable bytes from buffer as byte array. Does not modify writer
+        * or reader index of the buffer.
+        * 
+        * @param buffer byteBuf from which the bytes are going to be taken
+        * @return byte array
+        */
+       public static byte[] getAllBytes(final ByteBuf buffer) {
+               return getBytes(buffer, buffer.readableBytes());
+       }
+
        /**
         * Returns a new byte array from given byte array, starting at start index with the size of the length parameter.
         * Byte array given as parameter stays untouched.
index 06d2ea9448a7ac13e30b04d4cfde619cf645d3db..9c4d841d9f0c4a0e8ca7cd0fd1ea3d22a7c6f43a 100644 (file)
@@ -10,7 +10,10 @@ package org.opendaylight.protocol.util;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertTrue;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -26,6 +29,29 @@ public class ByteArrayTest {
 
        byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
 
+       @Test
+       public void testReadBytes() {
+               ByteBuf buffer = Unpooled.copiedBuffer(this.before);
+               buffer.readerIndex(1);
+               assertArrayEquals(new byte[]{28,4,6}, ByteArray.readBytes(buffer, 3));
+               assertEquals(4, buffer.readerIndex());
+
+               assertArrayEquals(new byte[]{9,10}, ByteArray.readAllBytes(buffer));
+               assertEquals(buffer.readerIndex(), buffer.writerIndex());
+       }
+
+       @Test
+       public void testGetBytes() {
+               ByteBuf buffer = Unpooled.copiedBuffer(this.before);
+               buffer.readerIndex(1);
+               assertArrayEquals(new byte[]{28,4,6}, ByteArray.getBytes(buffer, 3));
+               assertEquals(1, buffer.readerIndex());
+
+               assertArrayEquals(new byte[]{28,4,6,9,10}, ByteArray.getAllBytes(buffer));
+               assertNotSame(buffer.readerIndex(), buffer.writerIndex());
+       }
+
+
        @Test
        public void testBytesToFloat() {
                final float expected = 8581;