List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:org.opendaylight.protocol.bmp.spi.parser.TlvUtilTest.java
License:Open Source License
@Test public void testFormatTlvASCII() throws Exception { ByteBuf out = Unpooled.buffer(TLV_ASCII_OUT.length); TlvUtil.formatTlvAscii(1, "Name", out); Assert.assertArrayEquals(TLV_ASCII_OUT, ByteArray.getAllBytes(out)); out = Unpooled.EMPTY_BUFFER;//from w ww. java 2 s . c om TlvUtil.formatTlvAscii(1, null, out); Assert.assertFalse(out.isReadable()); }
From source file:org.opendaylight.protocol.bmp.spi.registry.SimpleBmpMessageRegistry.java
License:Open Source License
private int parseMessageHeader(final ByteBuf buffer) throws BmpDeserializationException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty."); Preconditions.checkArgument(buffer.readableBytes() >= COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), COMMON_HEADER_LENGTH);//from w ww . j a v a2s .com final short messageVersion = buffer.readUnsignedByte(); if (messageVersion != BMP_VERSION) { throw new BmpDeserializationException( "Unsuppoted BMP version. Passed: " + messageVersion + "; Expected: " + BMP_VERSION + "."); } final long messageLength = buffer.readUnsignedInt(); final short msgType = buffer.readUnsignedByte(); if (messageLength - COMMON_HEADER_LENGTH != buffer.readableBytes()) { throw new BmpDeserializationException("Size doesn't match size specified in header. Passed: " + buffer.readableBytes() + COMMON_HEADER_LENGTH + "; Expected: " + messageLength + "."); } return msgType; }
From source file:org.opendaylight.protocol.pcep.auto.bandwidth.extension.BandwidthUsageObjectCodec.java
License:Open Source License
@Override public BandwidthUsage parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (bytes.readableBytes() % BW_LENGTH != 0) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected multiple of " + BW_LENGTH + "."); }//from w ww . j a va2 s . c om final BandwidthUsageBuilder builder = new BandwidthUsageBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); final List<Bandwidth> bwSamples = new ArrayList<>(bytes.readableBytes() / BW_LENGTH); while (bytes.isReadable()) { bwSamples.add(new Bandwidth(ByteArray.readBytes(bytes, BW_LENGTH))); } builder.setBwSample(bwSamples); return builder.build(); }
From source file:org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LspObjectParser.java
License:Open Source License
@Override public Lsp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final LspBuilder builder = new LspBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); final int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), }; builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << FLAGS_SIZE) | (plspIdRaw[1] << FOUR_BITS_SHIFT) | (plspIdRaw[2] >> FOUR_BITS_SHIFT)))); parseFlags(builder, bytes);/*from ww w .j av a 2 s.c o m*/ final TlvsBuilder b = new TlvsBuilder(); parseTlvs(b, bytes.slice()); builder.setTlvs(b.build()); return builder.build(); }
From source file:org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07SrpObjectParser.java
License:Open Source License
@Override public Srp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (bytes.readableBytes() < MIN_SIZE) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: >=" + MIN_SIZE + "."); }// w w w . j a v a 2 s .co m final SrpBuilder builder = new SrpBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); parseFlags(builder, bytes); builder.setOperationId(new SrpIdNumber(bytes.readUnsignedInt())); final TlvsBuilder tlvsBuilder = new TlvsBuilder(); parseTlvs(tlvsBuilder, bytes.slice()); builder.setTlvs(tlvsBuilder.build()); return builder.build(); }
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()); }// www .j a v a2 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), 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()); }//from w w w. ja v 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.AbstractVendorInformationObjectParser.java
License:Open Source License
@Override public final Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final VendorInformationObjectBuilder builder = new VendorInformationObjectBuilder(); builder.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber())); builder.setEnterpriseSpecificInformation(parseEnterpriseSpecificInformation(buffer)); return builder.build(); }
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()); }//from ww w . ja va 2 s.co 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.object.PCEPBandwidthObjectParser.java
License:Open Source License
@Override public Bandwidth parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (bytes.readableBytes() != BANDWIDTH_F_LENGTH) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + BANDWIDTH_F_LENGTH + "."); }//from ww w.jav a2s . co m final BandwidthBuilder builder = new BandwidthBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); builder.setBandwidth( new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth( ByteArray.getAllBytes(bytes))); return builder.build(); }