*/
package org.opendaylight.protocol.bgp.linkstate;
+import io.netty.buffer.ByteBuf;
+
import java.util.Arrays;
import java.util.List;
import java.util.Map;
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;
}
@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.
values.add(value);
map.put(type, values);
- byteOffset += length;
}
final LinkstatePathAttributeBuilder builder = new LinkstatePathAttributeBuilder();
* @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()) {
* @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()) {
* @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()) {
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:
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:
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(
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:
default:
throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
}
- byteOffset += length;
}
LOG.debug("Finished parsing Prefix descriptors.");
return builder.build();
* @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);
}
@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);
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;
}
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;
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();
@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);
@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();
@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();
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;
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());
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());
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());
*/
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;
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;
*/
package org.opendaylight.protocol.bgp.parser.impl.message;
+import io.netty.buffer.ByteBuf;
+
import java.util.Arrays;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
* @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));
*/
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;
* @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 {
} else {
LOG.debug("Ignoring BGP Parameter type: {}", paramType);
}
+ buffer.skipBytes(paramLength);
}
LOG.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
}
package org.opendaylight.protocol.bgp.parser.impl.message;
+import io.netty.buffer.ByteBuf;
+
import java.util.Arrays;
import java.util.List;
// 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
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());
}
*/
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;
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
*/
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;
}
@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();
}
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());
}
*/
package org.opendaylight.protocol.bgp.parser.impl.message.open;
+import io.netty.buffer.ByteBuf;
+
import java.util.ArrayList;
import java.util.List;
}
@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();
*/
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;
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;
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;
}
@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();
}
*/
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;
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
*/
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;
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
*/
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;
/**
* 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
*/
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.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;
* @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
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;
}
}
- 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;
}
*/
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;
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
*/
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;
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
*/
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;
}
@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);
* 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;
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;
* @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());
}
/**
* @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) {
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);
}
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);
*/
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;
}
@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
*/
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;
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);
}
*/
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
*/
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
*/
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;
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
*/
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;
}
@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);
*/
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;
}
@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);
*/
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;
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
*/
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
*/
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;
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
*/
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
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;
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
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());
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();
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();
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"));
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);
}
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();
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();
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();
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();
@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();
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;
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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());
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());
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;
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;
(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());
(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());
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);
}
(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."));
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());
(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());
(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());
(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());
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()));
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()));
(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());
(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());
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;
@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());
*/
package org.opendaylight.protocol.bgp.parser.mock;
+import io.netty.buffer.ByteBuf;
+
import java.util.Map;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
* 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");
}
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;
*/
@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])));
}
/**
*/
@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 }));
}
/**
@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));
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);
*/
package org.opendaylight.protocol.bgp.parser.spi;
+import io.netty.buffer.ByteBuf;
+
import java.util.Arrays;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
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);
}
@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;
}
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;
}
}
*/
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;
*/
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;
}
*/
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);
}
*/
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;
}
*/
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);
}
*/
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;
}
*/
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);
}
*/
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;
}
*/
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;
}
*/
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;
}
*/
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);
}
*/
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;
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);
}
}
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) {
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);
}
@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.
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();
if (serializer == null) {
return null;
}
-
return serializer.serializeAttribute(attribute);
}
}
*/
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;
}
@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
*/
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;
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;
}
@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;
}
}
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);
}
}
*/
package org.opendaylight.protocol.bgp.parser.spi.pojo;
+import io.netty.buffer.ByteBuf;
+
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
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();
}
}
*/
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;
}
@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
if (serializer == null) {
return null;
}
-
return serializer.serializeParameter(parameter);
}
}
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();
}
}
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());
*/
package org.opendaylight.protocol.bgp.rib.mock;
+import io.netty.buffer.Unpooled;
+
import java.io.Closeable;
import java.util.Arrays;
import java.util.Iterator;
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);
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>io.netty</groupId>
+ <artifactId>netty-buffer</artifactId>
+ </dependency>
<!-- Testing dependencies -->
<dependency>
*/
package org.opendaylight.protocol.util;
+import io.netty.buffer.ByteBuf;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.
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;
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;