X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Fparser-spi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Fparser%2Fspi%2FAbstractMessageRegistry.java;h=f7afa9a7b9a09a8c8c808c2bb2fa27f6a99d73bf;hb=a397aa4828919cc643647c96f5c916ee211f597f;hp=2b482dd7a09a8c6f092c6f6e4ef08f8ebf58b55c;hpb=d4811da4b6bf398a5364a3f96c80387ac28624a5;p=bgpcep.git diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java index 2b482dd7a0..f7afa9a7b9 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java @@ -7,6 +7,9 @@ */ package org.opendaylight.protocol.bgp.parser.spi; +import com.google.common.base.Preconditions; +import com.google.common.primitives.UnsignedBytes; + import io.netty.buffer.ByteBuf; import java.util.Arrays; @@ -17,62 +20,56 @@ import org.opendaylight.protocol.bgp.parser.BGPParsingException; import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yangtools.yang.binding.Notification; -import com.google.common.base.Preconditions; -import com.google.common.primitives.UnsignedBytes; - public abstract class AbstractMessageRegistry implements MessageRegistry { - private static final byte[] MARKER; + private static final byte[] MARKER; - protected abstract Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException; + protected abstract Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException; - protected abstract byte[] serializeMessageImpl(final Notification message); + protected abstract byte[] serializeMessageImpl(final Notification message); - static { - MARKER = new byte[MessageUtil.MARKER_LENGTH]; - Arrays.fill(MARKER, UnsignedBytes.MAX_VALUE); - } + static { + MARKER = new byte[MessageUtil.MARKER_LENGTH]; + Arrays.fill(MARKER, UnsignedBytes.MAX_VALUE); + } - @Override - public final Notification parseMessage(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { - Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "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); + @Override + public final Notification parseMessage(final ByteBuf buffer) 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); - } - final int messageLength = buffer.readUnsignedShort(); - // to be sent with Error message - final byte typeBytes = buffer.readByte(); - final int messageType = UnsignedBytes.toInt(typeBytes); + if (!Arrays.equals(marker, MARKER)) { + throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC); + } + final int messageLength = buffer.readUnsignedShort(); + // to be sent with Error message + final byte typeBytes = buffer.readByte(); + final int messageType = UnsignedBytes.toInt(typeBytes); - final ByteBuf msgBody = buffer.slice(buffer.readerIndex(), messageLength - MessageUtil.COMMON_HEADER_LENGTH); + final ByteBuf msgBody = buffer.slice(buffer.readerIndex(), messageLength - MessageUtil.COMMON_HEADER_LENGTH); - if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) { - throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength); - } - if (msgBody.readableBytes() != messageLength - MessageUtil.COMMON_HEADER_LENGTH) { - throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + "; Expected: " - + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); - } - final Notification msg = parseBody(messageType, msgBody, messageLength); - if (msg == null) { - throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes }); - } - buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH); - return msg; - } + if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) { + throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength); + } + if (msgBody.readableBytes() != messageLength - MessageUtil.COMMON_HEADER_LENGTH) { + throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); + } + final Notification msg = parseBody(messageType, msgBody, messageLength); + if (msg == null) { + throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes }); + } + buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH); + return msg; + } - @Override - public final byte[] serializeMessage(final Notification message) { - if (message == null) { - throw new IllegalArgumentException("BGPMessage is mandatory."); - } - final byte[] ret = serializeMessageImpl(message); - if (ret == null) { - throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass()); - } - return ret; - } + @Override + public final byte[] serializeMessage(final Notification message) { + Preconditions.checkNotNull(message, "BGPMessage is mandatory."); + final byte[] ret = serializeMessageImpl(message); + Preconditions.checkNotNull(ret, "Unknown instance of BGPMessage. Passed ", message.getClass()); + return ret; + } }