List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:org.opendaylight.protocol.bgp.parser.spi.AbstractMessageRegistry.java
License:Open Source License
@Override public Notification parseMessage(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty."); Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH); final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH); if (!Arrays.equals(marker, MARKER)) { throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC); }/*from w ww . j av a2s . com*/ final int messageLength = buffer.readUnsignedShort(); // to be sent with Error message final byte typeBytes = buffer.readByte(); final int messageType = UnsignedBytes.toInt(typeBytes); if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) { throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength); } if (messageLength - MessageUtil.COMMON_HEADER_LENGTH != buffer.readableBytes()) { throw new BGPParsingException( "Size doesn't match size specified in header. Passed: " + buffer.readableBytes() + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); } final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH); final Notification msg = parseBody(messageType, msgBody, messageLength, constraint); if (msg == null) { throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes }); } return msg; }
From source file:org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleAttributeRegistry.java
License:Open Source License
@Override public Attributes parseAttributes(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException { final Map<Integer, RawAttribute> attributes = new TreeMap<>(); while (buffer.isReadable()) { addAttribute(buffer, attributes); }//from w w w . j a v a 2 s . c o m /* * TreeMap guarantees that we will be invoking the parser in the order * of increasing attribute type. */ final AttributesBuilder builder = new AttributesBuilder(); for (final Entry<Integer, RawAttribute> e : attributes.entrySet()) { LOG.debug("Parsing attribute type {}", e.getKey()); final RawAttribute a = e.getValue(); a.parser.parseAttribute(a.buffer, builder, constraint); } builder.setUnrecognizedAttributes(this.unrecognizedAttributes); return builder.build(); }
From source file:org.opendaylight.protocol.bgp.rib.impl.BGPByteToMessageDecoder.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BGPDocumentedException, BGPParsingException { if (in.isReadable()) { if (LOG.isTraceEnabled()) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); }//from w ww . java 2 s. c om out.add(this.registry.parseMessage(in, this.constraints)); } else { LOG.trace("No more content in incoming buffer."); } }
From source file:org.opendaylight.protocol.bmp.impl.BmpByteToMessageDecoder.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BmpDeserializationException { if (in.isReadable()) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); out.add(this.registry.parseMessage(in)); } else {//from w w w . j av a 2s. com LOG.trace("No more content in incoming buffer."); } }
From source file:org.opendaylight.protocol.bmp.impl.message.PeerUpHandler.java
License:Open Source License
@Override public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException { final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder() .setPeerHeader(parsePerPeerHeader(bytes)); if (peerUpNot.getPeerHeader().isIpv4()) { bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH); peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes))); } else {/*from w ww .j a va 2 s .c om*/ peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes))); } peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort())); peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort())); try { final Notification opSent = getBgpMessageRegistry() .parseMessage(bytes.readSlice(getBgpMessageLength(bytes))); Preconditions.checkNotNull(opSent, "Error on parse Sent OPEN Message, Sent OPEN Message is null"); Preconditions.checkArgument(opSent instanceof OpenMessage, "An instance of OpenMessage notification is required"); final OpenMessage sent = (OpenMessage) opSent; final Notification opRec = getBgpMessageRegistry() .parseMessage(bytes.readSlice(getBgpMessageLength(bytes))); Preconditions.checkNotNull(opRec, "Error on parse Received OPEN Message, Received OPEN Message is null"); Preconditions.checkArgument(opRec instanceof OpenMessage, "An instance of OpenMessage notification is required"); final OpenMessage received = (OpenMessage) opRec; peerUpNot.setSentOpen(new SentOpenBuilder(sent).build()); peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build()); final InformationBuilder infos = new InformationBuilder(); if (bytes.isReadable()) { parseTlvs(infos, bytes); peerUpNot.setInformation(infos.build()); } } catch (final BGPDocumentedException | BGPParsingException e) { throw new BmpDeserializationException("Error while parsing BGP Open Message.", e); } return peerUpNot.build(); }
From source file:org.opendaylight.protocol.bmp.spi.parser.AbstractBmpMessageParser.java
License:Open Source License
@Override public final Notification parseMessage(final ByteBuf bytes) throws BmpDeserializationException { Preconditions.checkArgument(bytes != null && bytes.isReadable()); final Notification parsedMessage = parseMessageBody(bytes); LOG.trace("Parsed BMP message: {}", parsedMessage); return parsedMessage; }
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;/* w w w.j av a2 s .c o m*/ } 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.bmp.spi.parser.TlvUtilTest.java
License:Open Source License
@Test public void testFormatTlvCounter32() throws Exception { ByteBuf out = Unpooled.buffer(TLV_COUNTER32_OUT.length); TlvUtil.formatTlvCounter32(1, new Counter32(5L), out); Assert.assertArrayEquals(TLV_COUNTER32_OUT, ByteArray.getAllBytes(out)); out = Unpooled.EMPTY_BUFFER;//from w w w . ja v a 2 s.c o m TlvUtil.formatTlvCounter32(1, null, out); Assert.assertFalse(out.isReadable()); }
From source file:org.opendaylight.protocol.bmp.spi.parser.TlvUtilTest.java
License:Open Source License
@Test public void testFormatTlvGauge64() throws Exception { ByteBuf out = Unpooled.buffer(TLV_GAUGE64_OUT.length); TlvUtil.formatTlvGauge64(1, new Gauge64(BigInteger.valueOf(5)), out); Assert.assertArrayEquals(TLV_GAUGE64_OUT, ByteArray.getAllBytes(out)); out = Unpooled.EMPTY_BUFFER;/*ww w .j a v a 2 s . co m*/ TlvUtil.formatTlvGauge64(1, null, out); Assert.assertFalse(out.isReadable()); }
From source file:org.opendaylight.protocol.bmp.spi.parser.TlvUtilTest.java
License:Open Source License
@Test public void testFormatTlvUtf8() throws Exception { ByteBuf out = Unpooled.buffer(TLV_UTF8_OUT.length); TlvUtil.formatTlvUtf8(1, "info1", out); Assert.assertArrayEquals(TLV_UTF8_OUT, ByteArray.getAllBytes(out)); out = Unpooled.EMPTY_BUFFER;//from w w w. j a v a2 s. c o m TlvUtil.formatTlvUtf8(1, null, out); Assert.assertFalse(out.isReadable()); }