Move MessageRegistry into impl for refactor 75/1775/1
authorRobert Varga <rovarga@cisco.com>
Wed, 9 Oct 2013 15:29:23 +0000 (17:29 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 9 Oct 2013 15:29:23 +0000 (17:29 +0200)
Also lower visibility of headerToBytes()

Change-Id: I6c215bb8e2d56aac1bf2c56233829b78cafa7d41
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/main/java/org/opendaylight/protocol/bgp/parser/impl/MessageRegistry.java [moved from bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageRegistry.java with 67% similarity]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/MessageRegistryImpl.java

index d59a1db4da9a0374eb8982116cd36c613ead8949..08686f5cf27ac902903e1e7e5630ae7b1ff36783 100644 (file)
@@ -14,7 +14,6 @@ import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
-import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
@@ -110,11 +109,7 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
                }
 
                final byte[] msgBody = serializer.serializeMessage(msg);
-               final byte[] headerBytes = headerToBytes(msgBody.length + COMMON_HEADER_LENGTH, serializer.messageType());
-               final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
-
-               ByteArray.copyWhole(headerBytes, retBytes, 0);
-               ByteArray.copyWhole(msgBody, retBytes, COMMON_HEADER_LENGTH);
+               final byte[] retBytes = formatMessage(serializer.messageType(), msgBody);
 
                logger.trace("Serialized BGP message {}.", Arrays.toString(retBytes));
                return retBytes;
@@ -125,15 +120,16 @@ public final class BGPMessageFactoryImpl implements BGPMessageFactory {
         * 
         * @return byte array representation of this header
         */
-       public byte[] headerToBytes(final int msgLength, final int msgType) {
-               final byte[] retBytes = new byte[COMMON_HEADER_LENGTH];
+       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);
 
-               System.arraycopy(ByteArray.intToBytes(msgLength), Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH, retBytes, MARKER_LENGTH,
-                               LENGTH_FIELD_LENGTH);
-
-               retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = (byte) msgType;
+               retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(msgType);
+               ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
 
                return retBytes;
        }
similarity index 67%
rename from bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageRegistry.java
rename to bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/MessageRegistry.java
index da48f829b893da1ba17173ac138a0324e478b1d8..a97815c4caa3bad59c6bc3fec3e929117f01a4fa 100644 (file)
@@ -5,14 +5,13 @@
  * 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;
+package org.opendaylight.protocol.bgp.parser.impl;
 
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public interface MessageRegistry {
-       public AutoCloseable registerMessageParser(int messageType, MessageParser parser);
        public MessageParser getMessageParser(int messageType);
-
-       public AutoCloseable registerMessageSerializer(Class<? extends Notification> msgClass, MessageSerializer serializer);
        public MessageSerializer getMessageSerializer(Notification message);
 }
index 04ee5a25bd53ebb95668dbf39b9563aa636ca006..93ef8ef461d7b7fecd2a4ca041f08d056368b0a6 100644 (file)
@@ -12,7 +12,6 @@ import org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageP
 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.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
@@ -26,7 +25,7 @@ implements MessageRegistry {
        public static final MessageRegistry INSTANCE;
 
        static {
-               final MessageRegistry reg = new MessageRegistryImpl();
+               final MessageRegistryImpl reg = new MessageRegistryImpl();
 
                reg.registerMessageParser(1, BGPOpenMessageParser.PARSER);
                reg.registerMessageSerializer(Open.class, BGPOpenMessageParser.SERIALIZER);
@@ -40,8 +39,7 @@ implements MessageRegistry {
                INSTANCE = reg;
        }
 
-       @Override
-       public synchronized AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
+       private synchronized AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
                Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
                return super.registerParser(messageType, parser);
        }
@@ -51,8 +49,7 @@ implements MessageRegistry {
                return super.getParser(messageType);
        }
 
-       @Override
-       public AutoCloseable registerMessageSerializer(final Class<? extends Notification> msgClass, final MessageSerializer serializer) {
+       private AutoCloseable registerMessageSerializer(final Class<? extends Notification> msgClass, final MessageSerializer serializer) {
                return super.registerSerializer(msgClass, serializer);
        }