Move constants and message header utility into SPI 06/1806/1
authorRobert Varga <rovarga@cisco.com>
Thu, 10 Oct 2013 17:09:03 +0000 (19:09 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 10 Oct 2013 17:09:03 +0000 (19:09 +0200)
Change-Id: If9928400945a6c72e9cdf342f0f34db9eb72f87f
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java [new file with mode: 0644]

index 24016f9ae37183b554c4b176834c206a891b8453..209908c0528093d8a693d8fbc7e9ac94badfe736 100644 (file)
@@ -19,6 +19,7 @@ import org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser;
 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -29,7 +30,6 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
@@ -58,17 +58,6 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
 
        private final static Logger logger = LoggerFactory.getLogger(BGPMessageFactoryImpl.class);
 
-       private final static int TYPE_FIELD_LENGTH = 1; // bytes
-
-       @VisibleForTesting
-       final static int LENGTH_FIELD_LENGTH = 2; // bytes
-
-       @VisibleForTesting
-       final static int MARKER_LENGTH = 16; // bytes
-
-       @VisibleForTesting
-       final static int COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
-
        private final HandlerRegistry<Notification, MessageParser, MessageSerializer> handlers;
 
        private BGPMessageFactoryImpl(final HandlerRegistry<Notification, MessageParser, MessageSerializer> handlers) {
@@ -91,7 +80,7 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
                }
 
                final byte[] msgBody = serializer.serializeMessage(message);
-               final byte[] retBytes = formatMessage(serializer.messageType(), msgBody);
+               final byte[] retBytes = MessageUtil.formatMessage(serializer.messageType(), msgBody);
 
                return retBytes;
        }
@@ -105,9 +94,9 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
                if (bytes == null) {
                        throw new IllegalArgumentException("Array of bytes is mandatory.");
                }
-               if (bytes.length < COMMON_HEADER_LENGTH) {
+               if (bytes.length < MessageUtil.COMMON_HEADER_LENGTH) {
                        throw new IllegalArgumentException("Too few bytes in passed array. Passed: " + bytes.length + ". Expected: >= "
-                                       + COMMON_HEADER_LENGTH + ".");
+                                       + MessageUtil.COMMON_HEADER_LENGTH + ".");
                }
                /*
                 * byte array starts with message length
@@ -116,25 +105,25 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
                // Arrays.fill(ones, (byte)0xff);
                // if (Arrays.equals(bytes, ones))
                // throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
-               final byte[] bs = ByteArray.cutBytes(bytes, MARKER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, LENGTH_FIELD_LENGTH));
-               final int messageType = UnsignedBytes.toInt(bs[LENGTH_FIELD_LENGTH]);
+               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]);
 
-               final byte[] msgBody = ByteArray.cutBytes(bs, LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH);
+               final byte[] msgBody = ByteArray.cutBytes(bs, MessageUtil.LENGTH_FIELD_LENGTH + MessageUtil.TYPE_FIELD_LENGTH);
 
-               if (messageLength < COMMON_HEADER_LENGTH) {
+               if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
                        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
                }
-               if (msgBody.length != messageLength - COMMON_HEADER_LENGTH) {
+               if (msgBody.length != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
                        throw new DeserializerException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
-                                       + (messageLength - COMMON_HEADER_LENGTH) + ". ");
+                                       + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
                }
 
                logger.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[LENGTH_FIELD_LENGTH] });
+                       throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
                }
 
                return Lists.newArrayList(msg);
@@ -156,23 +145,4 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
                logger.trace("Serialized BGP message {}.", Arrays.toString(ret));
                return ret;
        }
-
-       /**
-        * Serializes this BGP Message header to byte array.
-        * 
-        * @return byte array representation of this header
-        */
-       private byte[] formatMessage(final int msgType, final byte[] body) {
-               final byte[] retBytes = new byte[COMMON_HEADER_LENGTH + body.length];
-
-               Arrays.fill(retBytes, 0, MARKER_LENGTH, (byte) 0xff);
-               System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH),
-                               Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH,
-                               retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
-
-               retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(msgType);
-               ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
-
-               return retBytes;
-       }
 }
index aaf500835d048618bbf796c0f445b6b5e1d69d21..5bff0c0056bd135e11737c5add747a97f957e280 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
 import org.opendaylight.protocol.concepts.IGPMetric;
 import org.opendaylight.protocol.concepts.IPv4;
 import org.opendaylight.protocol.concepts.IPv4Prefix;
@@ -160,9 +161,9 @@ public class BGPParserTest {
        // FIXME: to be fixed in testing phase
        public void testGetUpdateMessage1() throws Exception {
 
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(0), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
@@ -286,9 +287,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testGetUpdateMessage2() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(1), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
                // check fields
 
@@ -391,9 +392,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testGetUpdateMessage3() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(2), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
@@ -496,9 +497,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testGetUpdateMessage4() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(3), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
@@ -576,9 +577,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testGetUpdateMessage5() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(4), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // attributes
@@ -603,9 +604,9 @@ public class BGPParserTest {
         */
        @Test
        public void testEORIpv4() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(5), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                assertEquals(new UpdateBuilder().build(), message);
@@ -629,9 +630,9 @@ public class BGPParserTest {
        @Ignore
        //FIXME: to be fixed in testing phase
        public void testEORIpv6() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(6), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
@@ -661,9 +662,9 @@ public class BGPParserTest {
        @Ignore
        //FIXME: to be fixed in testing phase
        public void testEORLS() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(7), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(7), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getAfi();
@@ -826,9 +827,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testBGPLink() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(8), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(8), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
@@ -957,9 +958,9 @@ public class BGPParserTest {
        @Ignore
        // FIXME: to be fixed in testing phase
        public void testBGPNode() throws Exception {
-               final byte[] body = ByteArray.cutBytes(inputBytes.get(9), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
-               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), BGPMessageFactoryImpl.MARKER_LENGTH,
-                               BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+               final byte[] body = ByteArray.cutBytes(inputBytes.get(9), MessageUtil.COMMON_HEADER_LENGTH);
+               final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), MessageUtil.MARKER_LENGTH,
+                               MessageUtil.LENGTH_FIELD_LENGTH));
                final Update message = updateParser.parseMessage(body, messageLength);
 
                // check fields
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java
new file mode 100644 (file)
index 0000000..16175b7
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2013 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 java.util.Arrays;
+
+import org.opendaylight.protocol.util.ByteArray;
+
+import com.google.common.primitives.UnsignedBytes;
+
+
+public final class MessageUtil {
+
+       public static final int LENGTH_FIELD_LENGTH = 2; // bytes
+       public static final int MARKER_LENGTH = 16; // bytes
+       public static final int TYPE_FIELD_LENGTH = 1; // bytes
+       public static final int COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
+
+       private MessageUtil() {
+
+       }
+
+       /**
+        * Serializes this BGP Message header to byte array.
+        * 
+        * @param type message type to be formatted
+        * @param body message body
+        * 
+        * @return byte array representation of this header
+        */
+       public static byte[] formatMessage(final int type, final byte[] body) {
+               final byte[] retBytes = new byte[COMMON_HEADER_LENGTH + body.length];
+
+               Arrays.fill(retBytes, 0, MARKER_LENGTH, (byte) 0xff);
+               System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH),
+                               Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH,
+                               retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
+
+               retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(type);
+               ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
+
+               return retBytes;
+       }
+}