Example usage for io.netty.buffer ByteBuf readUnsignedByte

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

Introduction

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

Prototype

public abstract short readUnsignedByte();

Source Link

Document

Gets an unsigned byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser.java

License:Open Source License

@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory)
        throws PCEPDeserializerException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() != CONTENT_LENGTH) {
        throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
                + "; Expected: " + CONTENT_LENGTH + ".");
    }/*from ww w .  jav  a2s . c  om*/
    buffer.readerIndex(buffer.readerIndex() + RESERVED);
    final SubobjectBuilder builder = new SubobjectBuilder();
    builder.setMandatory(mandatory);
    builder.setAttribute(Attribute.forValue(buffer.readUnsignedByte()));
    final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
    ubuilder.setRouterId(buffer.readUnsignedInt());
    ubuilder.setInterfaceId(buffer.readUnsignedInt());
    builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
    return builder.build();
}

From source file:org.opendaylight.protocol.pcep.spi.AbstractMessageParser.java

License:Open Source License

private List<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
    final List<Object> objs = new ArrayList<>();
    while (bytes.isReadable()) {
        if (bytes.readableBytes() < COMMON_OBJECT_HEADER_LENGTH) {
            throw new PCEPDeserializerException("Too few bytes in passed array. Passed: "
                    + bytes.readableBytes() + " Expected: >= " + COMMON_OBJECT_HEADER_LENGTH + ".");
        }//from   ww  w. ja va 2 s .  co  m
        final int objClass = bytes.readUnsignedByte();

        final byte flagsByte = bytes.readByte();
        final BitArray flags = BitArray.valueOf(flagsByte);
        final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(flagsByte, OT_SF_OFFSET, OT_SF_LENGTH));
        final int objLength = bytes.readUnsignedShort();

        if (bytes.readableBytes() < objLength - COMMON_OBJECT_HEADER_LENGTH) {
            throw new PCEPDeserializerException("Too few bytes in passed array. Passed: "
                    + bytes.readableBytes() + " Expected: >= " + objLength + ".");
        }
        // copy bytes for deeper parsing
        final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);

        final ObjectHeader header = new ObjectHeaderImpl(flags.get(PROCESSED), flags.get(IGNORED));

        if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
            final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(bytesToPass.readUnsignedInt());
            final Optional<? extends Object> obj = this.registry.parseVendorInformationObject(enterpriseNumber,
                    header, bytesToPass);
            if (obj.isPresent()) {
                objs.add(obj.get());
            }
        } else {
            // parseObject is required to return null for P=0 errored objects
            final Object o = this.registry.parseObject(objClass, objType, header, bytesToPass);
            if (o != null) {
                objs.add(o);
            }
        }
    }

    return objs;
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROExplicitExclusionRouteSubobjectParser.java

License:Open Source License

private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer> parseSubobject(
        final ByteBuf buffer) throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer> subs = new ArrayList<>();
    while (buffer.isReadable()) {
        final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0)
                ? true/*from   w w w. j a va2s .c  o  m*/
                : false;
        final int type = (buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES)
                & ~(1 << Values.FIRST_BIT_OFFSET);
        final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
        if (length > buffer.readableBytes()) {
            throw new RSVPParsingException(
                    "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
        }
        subs.add(this.registry.parseSubobject(type, buffer.readSlice(length), mandatory));
    }
    return subs;
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROLabelSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose)
        throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() < HEADER_LENGTH) {
        throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
                + "; Expected: >" + HEADER_LENGTH + ".");
    }//from  ww w .  ja v  a2  s.c om
    final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
    final short cType = buffer.readUnsignedByte();

    final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
    if (labelType == null) {
        throw new RSVPParsingException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
    }
    final LabelBuilder builder = new LabelBuilder();
    builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
    builder.setLabelType(labelType);
    return new SubobjectContainerBuilder().setLoose(loose)
            .setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.SEROBasicProtectionSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose)
        throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
    builder.setLoose(loose);//from www.  ja va 2  s  . c o m
    //skip reserved
    buffer.readByte();
    final short cType = buffer.readUnsignedByte();
    final ProtectionSubobject prot = parseCommonProtectionBody(cType, buffer);
    if (cType == CTYPE) {
        builder.setSubobjectType(new BasicProtectionCaseBuilder()
                .setBasicProtection(new BasicProtectionBuilder().setProtectionSubobject(prot).build()).build());

    } else if (cType == SERODynamicProtectionSubobjectParser.CTYPE) {
        builder.setSubobjectType(
                new DynamicControlProtectionCaseBuilder()
                        .setDynamicControlProtection(
                                new DynamicControlProtectionBuilder().setProtectionSubobject(prot).build())
                        .build());
    }
    return builder.build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROLabelSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() < HEADER_LENGTH) {
        throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
                + "; Expected: >" + HEADER_LENGTH + ".");
    }//from  ww w  .j a  v a  2s  .  com
    final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);

    final short cType = buffer.readUnsignedByte();

    final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
    if (labelType == null) {
        throw new RSVPParsingException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
    }
    final LabelBuilder builder = new LabelBuilder();
    builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
    builder.setGlobal(reserved.get(G_FLAG_OFFSET));
    builder.setLabelType(labelType);
    return new SubobjectContainerBuilder()
            .setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.SRROBasicProtectionSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
    //skip reserved
    buffer.readByte();//from  ww  w .j  av  a  2  s  .  com
    final short cType = buffer.readUnsignedByte();
    final ProtectionSubobject prot = parseCommonProtectionBody(cType, buffer);
    if (cType == CTYPE) {
        builder.setSubobjectType(new BasicProtectionCaseBuilder()
                .setBasicProtection(new BasicProtectionBuilder().setProtectionSubobject(prot).build()).build());
    } else if (cType == SRRODynamicProtectionSubobjectParser.CTYPE) {
        builder.setSubobjectType(
                new DynamicControlProtectionCaseBuilder()
                        .setDynamicControlProtection(
                                new DynamicControlProtectionBuilder().setProtectionSubobject(prot).build())
                        .build());
    }
    return builder.build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv4PrefixSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory)
        throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
    builder.setMandatory(mandatory);/* w  w w.j  a v  a 2 s .c  o  m*/
    if (buffer.readableBytes() != CONTENT4_LENGTH) {
        throw new RSVPParsingException(
                "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
    }
    final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
    final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
            new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length)));
    builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
    buffer.skipBytes(PREFIX_F_LENGTH);
    builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
    return builder.build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv6PrefixSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory)
        throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
    builder.setMandatory(mandatory);/*from www  . j a v a 2  s  .co m*/
    if (buffer.readableBytes() != CONTENT6_LENGTH) {
        throw new RSVPParsingException(
                "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
    }
    final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET);
    final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
            new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
    builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
    buffer.skipBytes(PREFIX_F_LENGTH);
    builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
    return builder.build();
}

From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROSRLGSubobjectParser.java

License:Open Source License

@Override
public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory)
        throws RSVPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() != CONTENT_LENGTH) {
        throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
                + "; Expected: " + CONTENT_LENGTH + ".");
    }/*from   www  .j ava2 s .  c  o  m*/
    final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
    builder.setMandatory(mandatory);
    builder.setSubobjectType(new SrlgCaseBuilder()
            .setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer.readUnsignedInt())).build()).build());
    buffer.readByte();
    builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
    return builder.build();
}