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;
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.");
}
+ 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]);
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;
}
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;
}
}