List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:org.opendaylight.protocol.pcep.spi.AbstractEROPathKeySubobjectParser.java
License:Open Source License
@Override public final Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); checkContentLenght(buffer.readableBytes()); final int pathKey = buffer.readUnsignedShort(); final byte[] pceId = readPceId(buffer); final PathKeyBuilder pBuilder = new PathKeyBuilder(); pBuilder.setPceId(new PceId(pceId)); pBuilder.setPathKey(new PathKey(pathKey)); final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose);//from w ww. j av a2 s . com builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.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 + "."); }/*w ww . j ava2 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;// w w w . j av a 2s.c o m } 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.AsNumberCaseParser.java
License:Open Source License
public static AsNumberCase 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() != CONTENT_LENGTH) { throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + CONTENT_LENGTH + "."); }/*from ww w.j a v a 2 s . com*/ return new AsNumberCaseBuilder() .setAsNumber( new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()) .build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROExplicitExclusionRouteSubobjectParser.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);// www .ja v a2s . c o m final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer> list = parseSubobject( buffer); final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> exrss = Lists .newArrayList(); for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer s : list) { final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder(); b.setAttribute(s.getAttribute()); b.setMandatory(s.isMandatory()); b.setSubobjectType(s.getSubobjectType()); exrss.add(b.build()); } builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build()); return builder.build(); }
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/*w w w .j a v a 2 s . 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.EROIpv4PrefixSubobjectParser.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 w w w . j av a2 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()); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv6PrefixSubobjectParser.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 w ww .ja v a2 s . c o m*/ if (buffer.readableBytes() != CONTENT_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX_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()); return builder.build(); }
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 + "."); }/* w ww . ja va2s . c o m*/ 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.EROPathKey128SubobjectParser.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() != CONTENT128_LENGTH) { throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + CONTENT128_LENGTH + "."); }//w w w . j a v a 2 s . co m final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); builder.setLoose(loose); builder.setSubobjectType( new PathKeyCaseBuilder().setPathKey(parsePathKey(PCE128_ID_F_LENGTH, buffer)).build()); return builder.build(); }