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.impl.attribute.sr.SrLinkAttributesParser.java

License:Open Source License

public static SrAdjIds parseAdjacencySegmentIdentifier(final ByteBuf buffer, final ProtocolId protocolId) {
    final Flags adjFlags;
    final Weight weight;
    final SidLabelIndex sidValue;
    if (buffer.isReadable()) {
        final BitArray flags = BitArray.valueOf(buffer, FLAGS_BITS_SIZE);
        adjFlags = parseFlags(flags, protocolId);
        weight = new Weight(buffer.readUnsignedByte());
        buffer.skipBytes(RESERVED);//from   www  .j  a  v a2  s.c om
        sidValue = SidLabelIndexParser.parseSidLabelIndex(Size.forValue(buffer.readableBytes()), buffer);
    } else {
        adjFlags = null;
        weight = null;
        sidValue = null;
    }
    return new SrAdjIdsBuilder().setFlags(adjFlags).setSidLabelIndex(sidValue).setWeight(weight).build();
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.SrLinkAttributesParser.java

License:Open Source License

public static EpeAdjSidTlv parseEpeAdjacencySegmentIdentifier(final ByteBuf buffer) {
    final Weight weight;
    final SidLabelIndex sidValue;
    if (buffer.isReadable()) {
        buffer.skipBytes(FLAGS_BYTE_SIZE);
        weight = new Weight(buffer.readUnsignedByte());
        buffer.skipBytes(RESERVED);//from w  w  w. j a va  2s . c  om
        sidValue = SidLabelIndexParser.parseSidLabelIndex(Size.forValue(buffer.readableBytes()), buffer);
    } else {
        weight = null;
        sidValue = null;
    }
    return new EpeAdjSidTlv() {
        @Override
        public Class<? extends DataContainer> getImplementedInterface() {
            return EpeAdjSidTlv.class;
        }

        @Override
        public Weight getWeight() {
            return weight;
        }

        @Override
        public SidLabelIndex getSidLabelIndex() {
            return sidValue;
        }
    };
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.SrLinkAttributesParser.java

License:Open Source License

public static SrLanAdjIds parseLanAdjacencySegmentIdentifier(final ByteBuf buffer,
        final ProtocolId protocolId) {
    if (!buffer.isReadable()) {
        return new SrLanAdjIdsBuilder().build();
    }/*from w  w  w.  j  a  v  a2 s. c o  m*/
    final SrLanAdjIdsBuilder srLanAdjIdBuilder = new SrLanAdjIdsBuilder();
    final BitArray flags = BitArray.valueOf(buffer, FLAGS_BITS_SIZE);
    srLanAdjIdBuilder.setFlags(parseFlags(flags, protocolId));
    srLanAdjIdBuilder.setWeight(new Weight(buffer.readUnsignedByte()));
    buffer.skipBytes(RESERVED);
    switch (protocolId) {
    case IsisLevel1:
    case IsisLevel2:
        srLanAdjIdBuilder
                .setIsoSystemId(new IsoSystemIdentifier(ByteArray.readBytes(buffer, ISO_SYSTEM_ID_SIZE)));
        break;
    case Ospf:
    case OspfV3:
        srLanAdjIdBuilder.setNeighborId(Ipv4Util.addressForByteBuf(buffer));
        break;
    default:
        return null;
    }
    // length determines a type of next field, which is used for parsing
    srLanAdjIdBuilder.setSidLabelIndex(
            SidLabelIndexParser.parseSidLabelIndex(Size.forValue(buffer.readableBytes()), buffer));
    return srLanAdjIdBuilder.build();
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.TeLspAttributesParser.java

License:Open Source License

static LinkStateAttribute parseTeLspAttributes(final RSVPTeObjectRegistry registry, final ByteBuf attributes)
        throws BGPParsingException {

    final TeLspAttributesBuilder builder = new TeLspAttributesBuilder();
    LOG.trace("Initiated parsing TE LSP Objects.");
    while (attributes.isReadable()) {
        final int length = attributes.readUnsignedShort();
        final int classNum = attributes.readUnsignedByte();
        final int cType = attributes.readUnsignedByte();
        final ByteBuf value = attributes.readSlice(length);
        try {//from  w ww.  java2 s  .  c o m
            addObject(builder, registry.parseRSPVTe(classNum, cType, value));
        } catch (final RSVPParsingException e) {
            LOG.debug("Parsering TE LSP Object error. class number: {} cType: {} value: {}", classNum, cType,
                    value, e);
            throw new BGPParsingException(e.getMessage());
        }
    }
    LOG.trace("Finished parsing TE LSP Objects.");
    return new TeLspAttributesCaseBuilder().setTeLspAttributes(builder.build()).build();
}

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

License:Open Source License

/**
 * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
 *
 * @param nlri as byte array//from w ww.jav  a2s. c  om
 * @return {@link CLinkstateDestination}
 */
private List<CLinkstateDestination> parseNlri(final ByteBuf nlri) {
    final List<CLinkstateDestination> dests = new ArrayList<>();
    while (nlri.isReadable()) {
        final CLinkstateDestination destination = this.nlriTypeReg.parseNlriType(nlri);
        if (destination == null) {
            continue;
        }
        dests.add(destination);
    }
    return dests;
}

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

License:Open Source License

@Override
public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;//from  w  w w.j  a  v  a  2  s.c om
    }
    final List<CLinkstateDestination> dst = parseNlri(nlri);

    builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder()
                    .setDestinationLinkstate(
                            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder()
                                    .setCLinkstateDestination(dst).build())
                    .build())
            .build());
}

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

License:Open Source License

@Override
public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;//from   w  w w  .  j  a  va  2 s  .  c  om
    }
    final List<CLinkstateDestination> dst = parseNlri(nlri);

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

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

License:Open Source License

static LinkDescriptors parseLinkDescriptors(final ByteBuf buffer) throws BGPParsingException {
    final LinkDescriptorsBuilder builder = new LinkDescriptorsBuilder();
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedShort();
        final int length = buffer.readUnsignedShort();
        final ByteBuf value = buffer.readSlice(length);
        LOG.trace("Parsing Link Descriptor: {}", ByteBufUtil.hexDump(value));
        switch (type) {
        case LINK_LR_IDENTIFIERS:
            builder.setLinkLocalIdentifier(value.readUnsignedInt());
            builder.setLinkRemoteIdentifier(value.readUnsignedInt());
            LOG.debug("Parsed link local {} remote {} Identifiers.", builder.getLinkLocalIdentifier(),
                    builder.getLinkRemoteIdentifier());
            break;
        case IPV4_IFACE_ADDRESS:
            final Ipv4InterfaceIdentifier lipv4 = new Ipv4InterfaceIdentifier(
                    Ipv4Util.addressForByteBuf(value));
            builder.setIpv4InterfaceAddress(lipv4);
            LOG.debug("Parsed IPv4 interface address {}.", lipv4);
            break;
        case IPV4_NEIGHBOR_ADDRESS:
            final Ipv4InterfaceIdentifier ripv4 = new Ipv4InterfaceIdentifier(
                    Ipv4Util.addressForByteBuf(value));
            builder.setIpv4NeighborAddress(ripv4);
            LOG.debug("Parsed IPv4 neighbor address {}.", ripv4);
            break;
        case IPV6_IFACE_ADDRESS:
            final Ipv6InterfaceIdentifier lipv6 = new Ipv6InterfaceIdentifier(
                    Ipv6Util.addressForByteBuf(value));
            builder.setIpv6InterfaceAddress(lipv6);
            LOG.debug("Parsed IPv6 interface address {}.", lipv6);
            break;
        case IPV6_NEIGHBOR_ADDRESS:
            final Ipv6InterfaceIdentifier ripv6 = new Ipv6InterfaceIdentifier(
                    Ipv6Util.addressForByteBuf(value));
            builder.setIpv6NeighborAddress(ripv6);
            LOG.debug("Parsed IPv6 neighbor address {}.", ripv6);
            break;
        case TlvUtil.MULTI_TOPOLOGY_ID:
            final TopologyIdentifier topId = new TopologyIdentifier(
                    value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
            builder.setMultiTopologyId(topId);
            LOG.debug("Parsed topology identifier {}.", topId);
            break;
        default://from   w  ww . ja v a2s  .  c  o  m
            throw new BGPParsingException("Link Descriptor not recognized, type: " + type);
        }
    }
    LOG.trace("Finished parsing Link descriptors.");
    return builder.build();
}

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

License:Open Source License

/**
 * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
 *
 * @param nlri as byte array/*from   ww w.  j  a  va2  s. c  om*/
 * @param isVpn flag which determines that destination has route distinguisher
 * @return {@link CLinkstateDestination}
 * @throws BGPParsingException if parsing was unsuccessful
 */
public static List<CLinkstateDestination> parseNlri(final ByteBuf nlri, final boolean isVpn)
        throws BGPParsingException {
    final List<CLinkstateDestination> dests = new ArrayList<>();
    while (nlri.isReadable()) {
        final CLinkstateDestinationBuilder builder = new CLinkstateDestinationBuilder();
        final NlriType type = NlriType.forValue(nlri.readUnsignedShort());

        // length means total length of the tlvs including route distinguisher not including the type field
        final int length = nlri.readUnsignedShort();
        RouteDistinguisher distinguisher = null;
        if (isVpn) {
            // this parses route distinguisher
            distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
            builder.setDistinguisher(distinguisher);
        }
        // parse source protocol
        final ProtocolId sp = ProtocolId.forValue(nlri.readByte());
        builder.setProtocolId(sp);

        // parse identifier
        final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
        builder.setIdentifier(identifier);

        // if we are dealing with linkstate nodes/links, parse local node descriptor
        org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localDescriptor = null;
        ByteBuf rest = null;
        if (!type.equals(NlriType.Ipv4TeLsp) && !type.equals(NlriType.Ipv6TeLsp)) {
            final int localtype = nlri.readUnsignedShort();
            final int locallength = nlri.readUnsignedShort();
            if (localtype == LOCAL_NODE_DESCRIPTORS_TYPE) {
                localDescriptor = NodeNlriParser.parseNodeDescriptors(nlri.readSlice(locallength), type, true);
            }
            final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH
                    - IDENTIFIER_LENGTH - TYPE_LENGTH - LENGTH_SIZE - locallength;
            LOG.trace("Restlength {}", restLength);
            rest = nlri.readSlice(restLength);
        }
        setObjectType(nlri, builder, type, localDescriptor, rest);

        dests.add(builder.build());
    }
    return dests;
}

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

License:Open Source License

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

    builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder()
                    .setDestinationLinkstate(
                            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder()
                                    .setCLinkstateDestination(dst).build())
                    .build())
            .build());
}