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.linkstate.nlri.LinkstateNlriParser.java

License:Open Source License

@Override
public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;//from   ww w  .  java2s  .  c  o  m
    }
    final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);

    builder.setAdvertizedRoutes(
            new AdvertizedRoutesBuilder()
                    .setDestinationType(new DestinationLinkstateCaseBuilder()
                            .setDestinationLinkstate(
                                    new DestinationLinkstateBuilder().setCLinkstateDestination(dst).build())
                            .build())
                    .build());
}

From source file:org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser.java

License:Open Source License

static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier parseNodeDescriptors(
        final ByteBuf buffer, final NlriType nlriType, final boolean local) throws BGPParsingException {
    AsNumber asnumber = null;//w w  w .  j av  a  2 s .  c  o  m
    DomainIdentifier bgpId = null;
    AreaIdentifier ai = null;
    CRouterIdentifier routerId = null;
    AsNumber memberAsn = null;
    Ipv4Address bgpRouterId = null;
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedShort();
        final int length = buffer.readUnsignedShort();
        final ByteBuf value = buffer.readSlice(length);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Parsing Node Descriptor: {}", ByteBufUtil.hexDump(value));
        }
        switch (type) {
        case AS_NUMBER:
            asnumber = new AsNumber(value.readUnsignedInt());
            LOG.debug("Parsed {}", asnumber);
            break;
        case BGP_LS_ID:
            bgpId = new DomainIdentifier(value.readUnsignedInt());
            LOG.debug("Parsed {}", bgpId);
            break;
        case AREA_ID:
            ai = new AreaIdentifier(value.readUnsignedInt());
            LOG.debug("Parsed area identifier {}", ai);
            break;
        case IGP_ROUTER_ID:
            routerId = parseRouterId(value);
            LOG.debug("Parsed Router Identifier {}", routerId);
            break;
        case BGP_ROUTER_ID:
            bgpRouterId = Ipv4Util.addressForByteBuf(value);
            LOG.debug("Parsed BGP Router Identifier {}", bgpRouterId);
            break;
        case MEMBER_AS_NUMBER:
            memberAsn = new AsNumber(value.readUnsignedInt());
            LOG.debug("Parsed Member AsNumber {}", memberAsn);
            break;
        default:
            throw new BGPParsingException("Node Descriptor not recognized, type: " + type);
        }
    }
    LOG.trace("Finished parsing Node descriptors.");
    return correctType(nlriType, local, asnumber, ai, routerId, bgpId, bgpRouterId, memberAsn);
}

From source file:org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriParser.java

License:Open Source License

static PrefixDescriptors parsePrefixDescriptors(final ByteBuf buffer, final boolean ipv4)
        throws BGPParsingException {
    final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedShort();
        final int length = buffer.readUnsignedShort();
        final ByteBuf value = buffer.readSlice(length);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Parsing Prefix Descriptor: {}", ByteBufUtil.hexDump(value));
        }//from   www  .jav a 2 s .  com
        switch (type) {
        case TlvUtil.MULTI_TOPOLOGY_ID:
            final TopologyIdentifier topologyId = new TopologyIdentifier(
                    value.readShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
            builder.setMultiTopologyId(topologyId);
            LOG.trace("Parsed Topology Identifier: {}", topologyId);
            break;
        case OSPF_ROUTE_TYPE:
            final int rt = value.readByte();
            final OspfRouteType routeType = OspfRouteType.forValue(rt);
            if (routeType == null) {
                throw new BGPParsingException("Unknown OSPF Route Type: " + rt);
            }
            builder.setOspfRouteType(routeType);
            LOG.trace("Parser RouteType: {}", routeType);
            break;
        case IP_REACHABILITY:
            final IpPrefix prefix = (ipv4) ? new IpPrefix(Ipv4Util.prefixForByteBuf(value))
                    : new IpPrefix(Ipv6Util.prefixForByteBuf(value));
            builder.setIpReachabilityInformation(prefix);
            LOG.trace("Parsed IP reachability info: {}", prefix);
            break;
        default:
            throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
        }
    }
    LOG.debug("Finished parsing Prefix descriptors.");
    return builder.build();
}

From source file:org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleBindingSubTlvsRegistry.java

License:Open Source License

public List<BindingSubTlvs> parseBindingSubTlvs(final ByteBuf buffer, final ProtocolId protocolId) {
    final List<BindingSubTlvs> subTlvs = new ArrayList<>();
    if (buffer != null) {
        while (buffer.isReadable()) {
            final int type = buffer.readUnsignedShort();
            final int length = buffer.readUnsignedShort();
            final ByteBuf slice = buffer.readSlice(length);
            final BindingSubTlvsParser parser = this.handlers.getParser(type);
            if (parser == null) {
                return null;
            }//from   w  w  w . ja  v a 2s .c o  m
            subTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(parser.parseSubTlv(slice, protocolId))
                    .build());
        }
    }
    return subTlvs;
}

From source file:org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry.java

License:Open Source License

public Map<QName, Object> parseSubTlvs(final ByteBuf buffer) {
    final Map<QName, Object> tlvs = new HashMap<>();
    while (buffer.isReadable()) {
        final LinkstateTlvParser<?> tlvParser = getParser(buffer);
        final Object tlvBody = parseTlv(buffer, tlvParser);
        if (tlvBody != null) {
            tlvs.put(tlvParser.getTlvQName(), tlvBody);
        }//from  w w  w  . j a va 2  s. co  m
    }
    return tlvs;
}

From source file:org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry.java

License:Open Source License

private <T> LinkstateTlvParser<T> getParser(final ByteBuf buffer) {
    Preconditions.checkArgument(buffer != null && buffer.isReadable());
    final int type = buffer.readUnsignedShort();
    final LinkstateTlvParser<T> parser = (LinkstateTlvParser<T>) this.tlvParsers.get(type);
    if (parser == null) {
        LOG.warn("Linkstate TLV parser for Type: {} was not found.", type);
    }//from   w w  w .  j a va  2 s  . co  m
    return parser;
}

From source file:org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry.java

License:Open Source License

private static <T> T parseTlv(final ByteBuf buffer, final LinkstateTlvParser<T> parser) {
    if (parser == null) {
        return null;
    }//  w  w w  .j a  v  a 2  s  . c om
    Preconditions.checkArgument(buffer != null && buffer.isReadable());
    final int length = buffer.readUnsignedShort();
    return parser.parseTlvBody(buffer.readSlice(length));
}

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

License:Open Source License

@Override
public Keepalive parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    if (body.isReadable()) {
        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.",
                messageLength);/*from w  ww  .  j  a v  a  2s  . c o m*/
    }
    return KEEPALIVE_MSG;
}

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

License:Open Source License

/**
 * Parses BGP Notification message to bytes.
 *
 * @param body ByteBuf to be parsed//from w  w  w  .j  av  a2s  .  c om
 * @param messageLength the length of the message
 * @return {@link Notify} which represents BGP notification message
 * @throws BGPDocumentedException if parsing goes wrong
 */
@Override
public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    Preconditions.checkArgument(body != null, "Buffer cannot be null.");
    if (body.readableBytes() < ERROR_SIZE) {
        throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
    }
    final int errorCode = body.readUnsignedByte();
    final int errorSubcode = body.readUnsignedByte();
    final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode)
            .setErrorSubcode((short) errorSubcode);
    if (body.isReadable()) {
        builder.setData(ByteArray.readAllBytes(body));
    }
    LOG.debug("BGP Notification message was parsed: err = {}, data = {}.",
            BGPError.forValue(errorCode, errorSubcode), Arrays.toString(builder.getData()));
    return builder.build();
}

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

License:Open Source License

private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(), "BUffer cannot be null or empty.");
    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
    }/*  w ww.j  a v  a2s . c o m*/
    while (buffer.isReadable()) {
        if (buffer.readableBytes() <= 2) {
            throw new BGPDocumentedException(
                    "Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)",
                    BGPError.OPT_PARAM_NOT_SUPPORTED);
        }
        final int paramType = buffer.readUnsignedByte();
        final int paramLength = buffer.readUnsignedByte();
        final ByteBuf paramBody = buffer.readSlice(paramLength);

        final BgpParameters param;
        try {
            param = this.reg.parseParameter(paramType, paramBody);
        } catch (final BGPParsingException e) {
            throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR,
                    e);
        }
        if (param != null) {
            params.add(param);
        } else {
            LOG.debug("Ignoring BGP Parameter type: {}", paramType);
        }
    }
    LOG.trace("Parsed BGP parameters: {}", params);
}