@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);
+ 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 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) + ". ");
+ 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) {
@Override
public final byte[] serializeMessage(final Notification message) {
- if (message == null) {
- throw new IllegalArgumentException("BGPMessage is mandatory.");
- }
+ Preconditions.checkNotNull(message, "BGPMessage is mandatory.");
final byte[] ret = serializeMessageImpl(message);
- if (ret == null) {
- throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass());
- }
+ Preconditions.checkNotNull(ret, "Unknown instance of BGPMessage. Passed ", message.getClass());
return ret;
}
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+public class AbstractMessageRegistryTest {
+
+ public static final byte[] keepAliveBMsg = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
+
+ private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
+
+ @Override
+ protected byte[] serializeMessageImpl(Notification message) {
+ return keepAliveBMsg;
+ }
+
+ @Override
+ protected Notification parseBody(int type, ByteBuf body, int messageLength) throws BGPDocumentedException {
+ return new KeepaliveBuilder().build();
+ }
+ };
+
+ @Test
+ public void testRegistry() throws BGPDocumentedException, BGPParsingException {
+ final Notification keepAlive = new KeepaliveBuilder().build();
+ final byte[] serialized = this.registry.serializeMessage(keepAlive);
+ assertArrayEquals(keepAliveBMsg, serialized);
+
+ final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg));
+ assertTrue(not instanceof Keepalive);
+ }
+}