Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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

License:Open Source License

/**
 * Parses given byte array to BGP Open message
 *
 * @param body byte array representing BGP Open message, without header
 * @param messageLength the length of the message
 * @return {@link Open} BGP Open Message
 * @throws BGPDocumentedException if the parsing was unsuccessful
 *//*  ww w. j  a va  2 s . c  o  m*/
@Override
public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    Preconditions.checkArgument(body != null, "Buffer cannot be null.");

    if (body.readableBytes() < MIN_MSG_LENGTH) {
        throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
    }
    final int version = body.readUnsignedByte();
    if (version != BGP_VERSION) {
        throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.",
                BGPError.VERSION_NOT_SUPPORTED);
    }
    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.addressForByteBuf(body);
    } catch (final IllegalArgumentException e) {
        throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
    }
    final int optLength = body.readUnsignedByte();

    final List<BgpParameters> optParams = new ArrayList<>();
    if (optLength > 0) {
        fillParams(body.slice(), optParams);
    }
    LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime,
            bgpId, optParams);
    return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime)
            .setBgpIdentifier(bgpId).setBgpParameters(optParams).build();
}

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

License:Open Source License

/**
 * Parse Update message from buffer.//from  w w w.  j a  va 2  s  .c o  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  w ww  . j av a2s .  com*/
        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.update.CommunitiesAttributeParser.java

License:Open Source License

/**
 * Parse known Community, if unknown, a new one will be created.
 *
 * @param refCache/*from w ww.j  av  a2 s . c  o m*/
 * @param buffer byte array to be parsed
 * @return new Community
 * @throws BGPDocumentedException
 */
@VisibleForTesting
public 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);
    }
    final byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
    if (Arrays.equals(body, NO_EXPORT)) {
        return CommunityUtil.NO_EXPORT;
    } else if (Arrays.equals(body, NO_ADVERTISE)) {
        return CommunityUtil.NO_ADVERTISE;
    } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
        return CommunityUtil.NO_EXPORT_SUBCONFED;
    }
    return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
}

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

License:Open Source License

@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    final AsSpecificExtendedCommunity asSpecific = new AsSpecificExtendedCommunityBuilder()
            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build();
    return new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(asSpecific).build();
}

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);/*from   www. j ava  2  s.  co  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.extended.communities.RouteOriginAsTwoOctetEcHandler.java

License:Open Source License

@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    final RouteOriginExtendedCommunity targetOrigin = new RouteOriginExtendedCommunityBuilder()
            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build();
    return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(targetOrigin).build();
}

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

License:Open Source License

@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    final RouteOriginIpv4 routeTarget = new RouteOriginIpv4Builder()
            .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
            .setLocalAdministrator(buffer.readUnsignedShort()).build();
    return new RouteOriginIpv4CaseBuilder().setRouteOriginIpv4(routeTarget).build();
}

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

License:Open Source License

@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
        throws BGPDocumentedException, BGPParsingException {
    final RouteTargetExtendedCommunity targetTarget = new RouteTargetExtendedCommunityBuilder()
            .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
            .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build();
    return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(targetTarget).build();
}