Fixed BGP synchronization.
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / AbstractMessageRegistry.java
index 6b6f0e7fc2ddaf99967aaebb9dbae70f06e3d22b..066caff44be814cee13ed0fc5a6d898a20169091 100644 (file)
@@ -11,9 +11,9 @@ import java.util.Arrays;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
-import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
@@ -22,14 +22,21 @@ import org.slf4j.LoggerFactory;
 import com.google.common.primitives.UnsignedBytes;
 
 abstract class AbstractMessageRegistry implements MessageRegistry {
-       private final static Logger logger = LoggerFactory.getLogger(AbstractMessageRegistry.class);
+       private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageRegistry.class);
+
+       private static final byte[] MARKER;
 
        protected abstract Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException;
 
        protected abstract byte[] serializeMessageImpl(final Notification message);
 
+       static {
+               MARKER = new byte[MessageUtil.MARKER_LENGTH];
+               Arrays.fill(MARKER, UnsignedBytes.MAX_VALUE);
+       }
+
        @Override
-       public final Notification parseMessage(final byte[] bytes) throws BGPDocumentedException, DeserializerException {
+       public final Notification parseMessage(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
                if (bytes == null) {
                        throw new IllegalArgumentException("Array of bytes is mandatory.");
                }
@@ -38,12 +45,10 @@ abstract class AbstractMessageRegistry implements MessageRegistry {
                                        + MessageUtil.COMMON_HEADER_LENGTH + ".");
                }
                final byte[] marker = ByteArray.subByte(bytes, 0, MessageUtil.MARKER_LENGTH);
-               final byte[] ones = new byte[MessageUtil.MARKER_LENGTH];
-               Arrays.fill(ones, (byte) 0xff);
-               // TODO: possible refactor
-               // if (Arrays.equals(marker, ones)) {
-               // throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
-               // }
+
+               if (!Arrays.equals(marker, MARKER)) {
+                       throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
+               }
                final byte[] bs = ByteArray.cutBytes(bytes, MessageUtil.MARKER_LENGTH);
                final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, MessageUtil.LENGTH_FIELD_LENGTH));
                final int messageType = UnsignedBytes.toInt(bs[MessageUtil.LENGTH_FIELD_LENGTH]);
@@ -54,17 +59,17 @@ abstract class AbstractMessageRegistry implements MessageRegistry {
                        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
                }
                if (msgBody.length != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
-                       throw new DeserializerException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
+                       throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
                                        + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
                }
 
-               logger.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
+               LOG.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
 
                final Notification msg = parseBody(messageType, msgBody, messageLength);
                if (msg == null) {
                        throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
                }
-
+               LOG.debug("Message parsed: {}", msg);
                return msg;
        }
 
@@ -74,14 +79,14 @@ abstract class AbstractMessageRegistry implements MessageRegistry {
                        throw new IllegalArgumentException("BGPMessage is mandatory.");
                }
 
-               logger.trace("Serializing {}", message);
+               LOG.trace("Serializing {}", message);
 
                final byte[] ret = serializeMessageImpl(message);
                if (ret == null) {
                        throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass());
                }
 
-               logger.trace("Serialized BGP message {}.", Arrays.toString(ret));
+               LOG.trace("Serialized BGP message {}.", Arrays.toString(ret));
                return ret;
        }
 }