Example usage for io.netty.buffer ByteBuf isReadable

List of usage examples for io.netty.buffer ByteBuf isReadable

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isReadable.

Prototype

public abstract boolean isReadable();

Source Link

Document

Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0 .

Usage

From source file:org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser.java

License:Open Source License

/**
 * Parse Update message from buffer./*  w  ww. j  a v  a2 s . co m*/
 * Calls {@link #checkMandatoryAttributesPresence(Update)} to check for presence of mandatory attributes.
 *
 * @param buffer Encoded BGP message in ByteBuf
 * @param messageLength Length of the BGP message
 * @param constraint Peer specific constraints
 * @return Parsed Update message body
 * @throws BGPDocumentedException
 */
@Override
public Update parseMessageBody(final ByteBuf buffer, final int messageLength,
        final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");

    final UpdateBuilder builder = new UpdateBuilder();

    final int withdrawnRoutesLength = buffer.readUnsignedShort();
    if (withdrawnRoutesLength > 0) {
        // TODO handle NLRI with multiple paths - requires modified yang data model
        final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util
                .prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
        builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
    }
    final int totalPathAttrLength = buffer.readUnsignedShort();

    if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
        return builder.build();
    }
    if (totalPathAttrLength > 0) {
        try {
            final Attributes attributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength),
                    constraint);
            builder.setAttributes(attributes);
        } catch (final RuntimeException | BGPParsingException e) {
            // Catch everything else and turn it into a BGPDocumentedException
            throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST,
                    e);
        }
    }
    final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
    if (!nlri.isEmpty()) {
        // TODO handle NLRI with multiple paths - requires modified yang data model
        builder.setNlri(new NlriBuilder().setNlri(nlri).build());
    }
    final Update msg = builder.build();
    checkMandatoryAttributesPresence(msg);
    LOG.debug("BGP Update message was parsed {}.", msg);
    return msg;
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.open.AddPathCapabilityHandler.java

License:Open Source License

@Override
public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
    final List<AddressFamilies> families = new ArrayList<>();
    while (buffer.isReadable()) {
        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.");
        }//from  www  . j a  va2s  . co m
        final int safiVal = buffer.readUnsignedByte();
        final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
        if (safi == null) {
            throw new BGPParsingException(
                    "Subsequent Address Family Identifier: '" + safiVal + "' not supported.");
        }
        final SendReceive sendReceive = SendReceive.forValue(buffer.readUnsignedByte());
        if (sendReceive != null) {
            families.add(
                    new AddressFamiliesBuilder().setAfi(afi).setSafi(safi).setSendReceive(sendReceive).build());
        }
    }
    return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder()
            .setAddPathCapability(new AddPathCapabilityBuilder().setAddressFamilies(families).build()).build())
            .build();
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.open.CapabilityParameterParser.java

License:Open Source License

@Override
public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
    Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0,
            "Byte array cannot be null or empty.");

    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
    }/*from   w  w w. j  av  a  2 s .c o  m*/

    final List<OptionalCapabilities> optionalCapas = Lists.newArrayList();
    while (buffer.isReadable()) {
        final OptionalCapabilities optionalCapa = parseOptionalCapability(buffer);
        if (optionalCapa != null) {
            optionalCapas.add(optionalCapa);
        }
    }
    return new BgpParametersBuilder().setOptionalCapabilities(optionalCapas).build();
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.AigpAttributeParser.java

License:Open Source License

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
        throws BGPDocumentedException, BGPParsingException {
    if (!buffer.isReadable()) {
        return;/*from  w ww  .  j  a v  a 2s .co  m*/
    }
    builder.setAigp(new AigpBuilder().setAigpTlv(parseAigpTLV(buffer)).build());
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser.java

License:Open Source License

/**
 * Parses AS_PATH from bytes./*from  w w  w  .  ja  v  a 2 s .com*/
 *
 * @param refCache ReferenceCache shared reference of object
 * @param buffer bytes to be parsed
 * @return new ASPath object
 * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
 * @throws BGPParsingException
 */
private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    if (!buffer.isReadable()) {
        return EMPTY;
    }
    final ArrayList<Segments> ases = new ArrayList<>();
    boolean isSequence = false;
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedByte();
        final SegmentType segmentType = AsPathSegmentParser.parseType(type);
        if (segmentType == null) {
            throw new BGPParsingException("AS Path segment type unknown : " + type);
        }
        final int count = buffer.readUnsignedByte();

        final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count,
                buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
        if (segmentType == SegmentType.AS_SEQUENCE) {
            ases.add(new SegmentsBuilder().setAsSequence(asList).build());
            isSequence = true;
        } else {
            ases.add(new SegmentsBuilder().setAsSet(asList).build());
        }
    }
    if (!isSequence) {
        throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.",
                BGPError.AS_PATH_MALFORMED);
    }

    ases.trimToSize();
    return new AsPathBuilder().setSegments(ases).build();
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.BgpPrefixSidAttributeParser.java

License:Open Source License

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
        throws BGPDocumentedException, BGPParsingException {
    final BgpPrefixSidBuilder sid = new BgpPrefixSidBuilder();
    final List<BgpPrefixSidTlvs> tlvList = new ArrayList<BgpPrefixSidTlvs>();
    while (buffer.isReadable()) {
        final BgpPrefixSidTlv tlv = this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer);
        tlvList.add(new BgpPrefixSidTlvsBuilder().setBgpPrefixSidTlv(tlv).build());
    }/*from www  . j  av a2 s . c  o m*/
    builder.setBgpPrefixSid(sid.setBgpPrefixSidTlvs(tlvList).build());
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.ClusterIdAttributeParser.java

License:Open Source License

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
    final List<ClusterIdentifier> list = Lists.newArrayList();
    while (buffer.isReadable()) {
        list.add(new ClusterIdentifier(Ipv4Util.addressForByteBuf(buffer)));
    }/*from w w  w.  j  a  va  2  s.  c o  m*/
    builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesAttributeParser.java

License:Open Source License

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
        throws BGPDocumentedException {
    final List<Communities> set = Lists.newArrayList();
    while (buffer.isReadable()) {
        set.add((Communities) parseCommunity(this.refCache, buffer.readSlice(COMMUNITY_LENGTH)));
    }// w ww  . ja  v  a  2s .  c o  m
    builder.setCommunities(set);
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities.EncapsulationEC.java

License:Open Source License

@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    Preconditions.checkArgument(buffer.readableBytes() == CONTENT_SIZE,
            "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
    buffer.skipBytes(RESERVED_SIZE);/*  w  ww .  j  a  v  a 2s .c o  m*/
    final EncapsulationExtendedCommunity encap = new EncapsulationExtendedCommunityBuilder()
            .setTunnelType(EncapsulationTunnelType.forValue(buffer.readUnsignedShort())).build();
    return new EncapsulationCaseBuilder().setEncapsulationExtendedCommunity(encap).build();
}

From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser.java

License:Open Source License

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
        throws BGPDocumentedException, BGPParsingException {
    final List<ExtendedCommunities> set = new ArrayList<>();
    while (buffer.isReadable()) {
        final ExtendedCommunities exComm = ecReg.parseExtendedCommunity(buffer);
        if (exComm != null) {
            set.add(exComm);/* w  ww  . j a v a2s  .c  o m*/
        }
    }
    builder.setExtendedCommunities(set);
}