BUG-113: Factor MessageRegistry out of BGPMessageFactory 58/1858/3
authorRobert Varga <rovarga@cisco.com>
Sun, 13 Oct 2013 17:58:51 +0000 (19:58 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 14 Oct 2013 14:21:18 +0000 (16:21 +0200)
Change-Id: I9afed602c68eb15de0955797ea1200a30da23e07
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/AbstractMessageRegistry.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java [new file with mode: 0644]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleMessageRegistry.java [moved from bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleBGPMessageFactory.java with 78% similarity]
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SingletonProviderContext.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.java
bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java
bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java
bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java

index 3d68c4500f1faba131cafc0fee8772737a6ac2d1..b7b5fc696a1b0912f7d9bdbf86b5e13f7c7979d0 100644 (file)
@@ -8,24 +8,20 @@
 package org.opendaylight.protocol.bgp.parser.impl;
 
 import java.util.Arrays;
-import java.util.List;
 
 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.MessageRegistry;
 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;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
 
-abstract class AbstractMessageRegistry implements BGPMessageFactory, MessageRegistry {
+abstract class AbstractMessageRegistry implements MessageRegistry {
        private final static Logger logger = LoggerFactory.getLogger(AbstractMessageRegistry.class);
 
        protected abstract Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException;
@@ -87,18 +83,4 @@ abstract class AbstractMessageRegistry implements BGPMessageFactory, MessageRegi
                logger.trace("Serialized BGP message {}.", Arrays.toString(ret));
                return ret;
        }
-
-       /*
-        * (non-Javadoc)
-        * @see org.opendaylight.protocol.bgp.parser.BGPMessageParser#parse(byte[])
-        */
-       @Override
-       public final List<Notification> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
-               return Lists.newArrayList(parseMessage(bytes));
-       }
-
-       @Override
-       public final byte[] put(final Notification msg) {
-               return serializeMessage(msg);
-       }
 }
diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java
new file mode 100644 (file)
index 0000000..8998836
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.impl;
+
+import java.util.List;
+
+import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
+import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.ProviderContext;
+import org.opendaylight.protocol.framework.DeserializerException;
+import org.opendaylight.protocol.framework.DocumentedException;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+public final class BGPMessageFactoryImpl implements BGPMessageFactory {
+       private static final class Holder {
+               private static final BGPMessageFactoryImpl INSTANCE;
+
+               static {
+                       final ProviderContext pc = SingletonProviderContext.getInstance();
+
+                       new ActivatorImpl().start(pc);
+
+                       INSTANCE = new BGPMessageFactoryImpl(pc.getMessageRegistry());
+               }
+       }
+
+       private final MessageRegistry registry;
+
+       private BGPMessageFactoryImpl(final MessageRegistry registry) {
+               this.registry = Preconditions.checkNotNull(registry);
+       }
+
+       public static BGPMessageFactoryImpl getInstance() {
+               return Holder.INSTANCE;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.opendaylight.protocol.bgp.parser.BGPMessageParser#parse(byte[])
+        */
+       @Override
+       public final List<Notification> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
+               return Lists.newArrayList(registry.parseMessage(bytes));
+       }
+
+       @Override
+       public final byte[] put(final Notification msg) {
+               return registry.serializeMessage(msg);
+       }
+}
similarity index 78%
rename from bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleBGPMessageFactory.java
rename to bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleMessageRegistry.java
index 7aaf328abe351c9c049f6368496156fdd351ed7a..ccc68170d6c323fa9cac4dd8bc27c1bd13abdfa7 100644 (file)
@@ -9,35 +9,24 @@ package org.opendaylight.protocol.bgp.parser.impl;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 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.concepts.HandlerRegistry;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
-public final class SimpleBGPMessageFactory extends AbstractMessageRegistry {
+public final class SimpleMessageRegistry extends AbstractMessageRegistry {
        private static final class Holder {
-               private static final SimpleBGPMessageFactory INSTANCE;
-
-               static {
-                       final SimpleBGPMessageFactory f = new SimpleBGPMessageFactory();
-
-                       // Wrong order, but needed for circular dep.
-                       INSTANCE = f;
-                       try {
-                               new ActivatorImpl().start(SingletonProviderContext.getInstance());
-                       } catch (Exception e) {
-                               throw new ExceptionInInitializerError(e);
-                       }
-               }
+               private static final MessageRegistry INSTANCE = new SimpleMessageRegistry();
        }
 
        private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
 
-       private SimpleBGPMessageFactory() {
+       private SimpleMessageRegistry() {
 
        }
 
-       public static SimpleBGPMessageFactory getInstance() {
+       public static MessageRegistry getInstance() {
                return Holder.INSTANCE;
        }
 
index 94f772fb6320605bec3212af1b10c951f09128c4..9befb4ba9cd75da5fbefe92f1673a9950f365169 100644 (file)
@@ -46,7 +46,7 @@ public final class SingletonProviderContext implements ProviderContext {
 
        @Override
        public MessageRegistry getMessageRegistry() {
-               return SimpleBGPMessageFactory.getInstance();
+               return SimpleMessageRegistry.getInstance();
        }
 
        @Override
index f25ea37db1272e87b8797b7bbd7ce374e8e7745d..c12dfedbfe10de00baa8ff54de17f4c69242caf4 100644 (file)
@@ -25,7 +25,6 @@ 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.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
 import org.opendaylight.protocol.concepts.IGPMetric;
 import org.opendaylight.protocol.concepts.IPv4;
@@ -86,13 +85,13 @@ public class BGPParserTest {
 
        private static int MAX_SIZE = 300;
 
-       private static MessageRegistry reg;
+       private static BGPMessageFactory reg;
 
        private static BGPUpdateMessageParser updateParser;
 
        @BeforeClass
        public static void setUp() throws Exception {
-               reg = SimpleBGPMessageFactory.getInstance();
+               reg = BGPMessageFactoryImpl.getInstance();
                updateParser = new BGPUpdateMessageParser(SimpleAttributeRegistry.getInstance());
 
                for (int i = 1; i <= COUNTER; i++) {
@@ -1040,7 +1039,7 @@ public class BGPParserTest {
         */
        @Test
        public void testOpenMessage() throws Exception {
-               final BGPMessageFactory msgFactory = SimpleBGPMessageFactory.getInstance();
+               final BGPMessageFactory msgFactory = BGPMessageFactoryImpl.getInstance();
                final Open open = (Open) msgFactory.parse(inputBytes.get(13)).get(0);
                final Set<BgpTableType> types = Sets.newHashSet();
                for (final BgpParameters param : open.getBgpParameters()) {
index 907765525bb4e6ae264e6bfb1424f8384a95183c..69a6b0a5d17744c04d55b771b2a74f5048be88ff 100644 (file)
@@ -220,7 +220,7 @@ public class ComplementaryTest {
 
        @Test
        public void testBGPHeaderParser() throws IOException {
-               final BGPMessageFactory h = SimpleBGPMessageFactory.getInstance();
+               final BGPMessageFactory h = BGPMessageFactoryImpl.getInstance();
                try {
                        h.parse(new byte[] { (byte) 0, (byte) 0 });
                        fail("Exception should have occured.");
@@ -249,7 +249,7 @@ public class ComplementaryTest {
 
        @Test
        public void testMessageParser() throws IOException {
-               final BGPMessageFactory parser = SimpleBGPMessageFactory.getInstance();
+               final BGPMessageFactory parser = BGPMessageFactoryImpl.getInstance();
                String ex = "";
                try {
                        parser.put(null);
index 4e9aa25dd6f8ac28d295d0cf490bb5cf4c30eaab..50f01b5aaff959fb2da628375fbacf5c5d330802 100644 (file)
@@ -24,7 +24,7 @@ public class PathAttributeParserTest {
        @Before
        public void setUp() {
                // Activates everything
-               SimpleBGPMessageFactory.getInstance();
+               BGPMessageFactoryImpl.getInstance();
        }
 
        @Test
index 0b209e8953fb03ea5092bb5009fdfd54fdc54728..4ca8627dd86066b8b9c03419bbdf0788b908c6b2 100644 (file)
@@ -24,7 +24,7 @@ import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
-import org.opendaylight.protocol.bgp.parser.impl.SimpleBGPMessageFactory;
+import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
@@ -80,7 +80,7 @@ public class ParserTest {
                (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
                (byte) 0x01, (byte) 0x00, (byte) 0x01 };
 
-       final ProtocolMessageFactory<Notification> factory = SimpleBGPMessageFactory.getInstance();
+       final ProtocolMessageFactory<Notification> factory = BGPMessageFactoryImpl.getInstance();
 
        @Test
        public void testHeaderErrors() throws DeserializerException, DocumentedException {
index 522dcaf69c77b9e0d3d3a088458c203ffa09fb1f..517903a16f2380f668cb099708a3f62e084f5af3 100644 (file)
@@ -17,7 +17,7 @@ import javax.annotation.concurrent.ThreadSafe;
 
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.impl.SimpleBGPMessageFactory;
+import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGP;
 import org.opendaylight.protocol.concepts.ListenerRegistration;
 import org.opendaylight.protocol.framework.DeserializerException;
@@ -55,7 +55,7 @@ public final class BGPMock implements BGP, Closeable {
 
        private List<Notification> parsePrevious(final List<byte[]> msgs) {
                final List<Notification> messages = Lists.newArrayList();
-               final ProtocolMessageFactory<Notification> parser = SimpleBGPMessageFactory.getInstance();
+               final ProtocolMessageFactory<Notification> parser = BGPMessageFactoryImpl.getInstance();
                try {
                        for (final byte[] b : msgs) {
 
index 123ba263f1cca044d4bf170fdecc22c9664da41f..9f9e73cecc8adfb75ca66264b155d3db65d0467d 100644 (file)
@@ -14,7 +14,7 @@ import java.net.InetAddress;
 import java.net.InetSocketAddress;
 
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.impl.SimpleBGPMessageFactory;
+import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
@@ -49,7 +49,7 @@ public class Main {
        BGPDispatcherImpl dispatcher;
 
        public Main() throws IOException {
-               this.dispatcher = new BGPDispatcherImpl(SimpleBGPMessageFactory.getInstance());
+               this.dispatcher = new BGPDispatcherImpl(BGPMessageFactoryImpl.getInstance());
        }
 
        public static void main(final String[] args) throws NumberFormatException, IOException {
index d02789c61a405b2c15e802204344876d1ed7595a..bc97576179b65cf9db62741f5fd215bd407ff508 100644 (file)
@@ -17,7 +17,7 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.impl.SimpleBGPMessageFactory;
+import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
@@ -72,7 +72,7 @@ public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionLi
                final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
 
                final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
-                               new BGPHandlerFactory(SimpleBGPMessageFactory.getInstance()),
+                               new BGPHandlerFactory(BGPMessageFactoryImpl.getInstance()),
                                new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
                mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);