Example usage for io.netty.buffer ByteBuf readSlice

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

Introduction

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

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

From source file:org.opendaylight.protocol.bmp.spi.parser.AbstractBmpMessageWithTlvParser.java

License:Open Source License

protected final void parseTlvs(final T builder, final ByteBuf bytes) throws BmpDeserializationException {
    Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
    if (!bytes.isReadable()) {
        return;/*from   w ww .j a va 2s. c  om*/
    }
    while (bytes.isReadable()) {
        final int type = bytes.readUnsignedShort();
        final int length = bytes.readUnsignedShort();
        if (length > bytes.readableBytes()) {
            throw new BmpDeserializationException("Wrong length specified. Passed: " + length
                    + "; Expected: <= " + bytes.readableBytes() + ".");
        }
        final ByteBuf tlvBytes = bytes.readSlice(length);
        LOG.trace("Parsing BMP TLV : {}", ByteBufUtil.hexDump(tlvBytes));

        final Tlv tlv = this.tlvRegistry.parseTlv(type, tlvBytes);
        if (tlv != null) {
            LOG.trace("Parsed BMP TLV {}.", tlv);
            addTlv(builder, tlv);
        }
    }
}

From source file:org.opendaylight.protocol.pcep.impl.object.AbstractEROWithSubobjectsParser.java

License:Open Source License

protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
    // Explicit approval of empty ERO
    Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
    final List<Subobject> subs = new ArrayList<>();
    while (buffer.isReadable()) {
        final boolean loose = ((buffer.getUnsignedByte(buffer.readerIndex())
                & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : 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 PCEPDeserializerException(
                    "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
        }// w  w  w  .  j  av  a  2s.  co m
        LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
        final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), loose);
        if (sub == null) {
            LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
        } else {
            LOG.debug("Subobject was parsed. {}", sub);
            subs.add(sub);
        }
    }
    return subs;
}

From source file:org.opendaylight.protocol.pcep.impl.object.AbstractRROWithSubobjectsParser.java

License:Open Source License

protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final List<Subobject> subs = new ArrayList<>();
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedByte();
        final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
        if (length > buffer.readableBytes()) {
            throw new PCEPDeserializerException(
                    "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
        }/* w  w  w. j av a 2 s. c o m*/
        LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
        final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length));
        if (sub == null) {
            LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
        } else {
            LOG.debug("Subobject was parsed. {}", sub);
            subs.add(sub);
        }
    }
    return subs;
}

From source file:org.opendaylight.protocol.pcep.impl.object.AbstractXROWithSubobjectsParser.java

License:Open Source License

protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    final List<Subobject> subs = new ArrayList<>();
    while (buffer.isReadable()) {
        final boolean mandatory = ((buffer.getUnsignedByte(buffer.readerIndex())
                & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false;
        final int type = UnsignedBytes.checkedCast(
                (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 PCEPDeserializerException(
                    "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
        }/*  ww w  .  j a va2  s  . c  o m*/
        LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
        final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), mandatory);
        if (sub == null) {
            LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
        } else {
            LOG.debug("Subobject was parsed. {}", sub);
            subs.add(sub);
        }
    }
    return subs;
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPObjectParserTest.java

License:Open Source License

@Test
public void testVendorInformationObject() throws PCEPDeserializerException {
    final byte[] viObjBytes = {
            /* vendor-information object */
            0x22, 0x10, 0x00, 0x0C,/* w  w  w  . j  a  v a 2 s.c o m*/
            /* enterprise number */
            0x00, 0x00, 0x00, 0x00,
            /* enterprise specific information */
            0x00, 0x00, 0x00, 0x05 };
    final TestVendorInformationObjectParser parser = new TestVendorInformationObjectParser();
    final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
    final VendorInformationObject viObj = new VendorInformationObjectBuilder()
            .setEnterpriseNumber(new EnterpriseNumber(0L)).setEnterpriseSpecificInformation(esInfo).build();
    final ByteBuf result = Unpooled.wrappedBuffer(viObjBytes);
    result.readerIndex(8);
    final VendorInformationObject o = (VendorInformationObject) parser
            .parseObject(new ObjectHeaderImpl(false, false), result.readSlice(result.readableBytes()));
    assertEquals(viObj, o);

    final ByteBuf buf = Unpooled.buffer(viObjBytes.length);
    parser.serializeObject(viObj, buf);
    assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
}

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

License:Open Source License

private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> parseSubobject(
        final ByteBuf buffer) throws PCEPDeserializerException {
    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.pcep.types.rev131005.exclude.route.object.xro.Subobject> subs = new ArrayList<>();
    while (buffer.isReadable()) {
        final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0)
                ? true//ww  w .ja v  a  2s  . co 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 PCEPDeserializerException(
                    "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.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 + ".");
        }//ww  w .  ja v a 2 s .c o  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.pcep.spi.AbstractObjectWithTlvsParser.java

License:Open Source License

protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
    Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
    if (!bytes.isReadable()) {
        return;/*from  w ww.ja  v a 2 s .com*/
    }
    final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
    while (bytes.isReadable()) {
        final int type = bytes.readUnsignedShort();
        final int length = bytes.readUnsignedShort();
        if (length > bytes.readableBytes()) {
            throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
                    + bytes.readableBytes() + ".");
        }
        final ByteBuf tlvBytes = bytes.readSlice(length);
        LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));

        if (VendorInformationUtil.isVendorInformationTlv(type)) {
            final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(tlvBytes.readUnsignedInt());
            final Optional<VendorInformationTlv> viTlv = this.viTlvReg
                    .parseVendorInformationTlv(enterpriseNumber, tlvBytes);
            if (viTlv.isPresent()) {
                LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
                viTlvs.add(viTlv.get());
            }
        } else {
            final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
            if (tlv != null) {
                LOG.trace("Parsed PCEP TLV {}.", tlv);
                addTlv(builder, tlv);
            }
        }
        bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
    }
    addVendorInformationTlvs(builder, viTlvs);
}

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 ww.java 2s .  co  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.te.AttributesObjectParser.java

License:Open Source License

@Override
protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
    final LspAttributesObjectBuilder builder = new LspAttributesObjectBuilder();

    final List<SubobjectContainer> subObjectList = new ArrayList<>();
    while (byteBuf.isReadable()) {
        final int type = byteBuf.readUnsignedShort();
        final int length = byteBuf.readUnsignedShort();
        final ByteBuf value = byteBuf.readSlice(length);
        final SubobjectContainerBuilder subObj = new SubobjectContainerBuilder();
        if (type == FLAG_TLV_TYPE) {
            subObj.setLspSubobject(new FlagsTlvBuilder().setFlagContainer(parseFlag(value)).build());
        } else {//from  www  . j  av  a  2 s  .c  o  m
            LOG.warn("Lsp Attributes Subobject type {} not supported", type);
        }
        subObjectList.add(subObj.build());
    }

    return builder.setSubobjectContainer(subObjectList).build();
}