From 29e45f7da3aada7f8ad3241e28966789a0b839a6 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 13 Oct 2013 19:58:51 +0200 Subject: [PATCH] BUG-113: Factor MessageRegistry out of BGPMessageFactory Change-Id: I9afed602c68eb15de0955797ea1200a30da23e07 Signed-off-by: Robert Varga --- .../parser/impl/AbstractMessageRegistry.java | 20 +------ .../parser/impl/BGPMessageFactoryImpl.java | 58 +++++++++++++++++++ ...actory.java => SimpleMessageRegistry.java} | 21 ++----- .../parser/impl/SingletonProviderContext.java | 2 +- .../bgp/parser/impl/BGPParserTest.java | 7 +-- .../bgp/parser/impl/ComplementaryTest.java | 4 +- .../parser/impl/PathAttributeParserTest.java | 2 +- .../protocol/bgp/rib/impl/ParserTest.java | 4 +- .../protocol/bgp/rib/mock/BGPMock.java | 4 +- .../protocol/bgp/testtool/Main.java | 4 +- .../protocol/bgp/testtool/BGPSpeakerMock.java | 4 +- 11 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java rename bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/{SimpleBGPMessageFactory.java => SimpleMessageRegistry.java} (78%) diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/AbstractMessageRegistry.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/AbstractMessageRegistry.java index 3d68c4500f..b7b5fc696a 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/AbstractMessageRegistry.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/AbstractMessageRegistry.java @@ -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 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 index 0000000000..899883654a --- /dev/null +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPMessageFactoryImpl.java @@ -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 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); + } +} diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleBGPMessageFactory.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleMessageRegistry.java 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 7aaf328abe..ccc68170d6 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleBGPMessageFactory.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SimpleMessageRegistry.java @@ -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 handlers = new HandlerRegistry<>(); - private SimpleBGPMessageFactory() { + private SimpleMessageRegistry() { } - public static SimpleBGPMessageFactory getInstance() { + public static MessageRegistry getInstance() { return Holder.INSTANCE; } diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SingletonProviderContext.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SingletonProviderContext.java index 94f772fb63..9befb4ba9c 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SingletonProviderContext.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/SingletonProviderContext.java @@ -46,7 +46,7 @@ public final class SingletonProviderContext implements ProviderContext { @Override public MessageRegistry getMessageRegistry() { - return SimpleBGPMessageFactory.getInstance(); + return SimpleMessageRegistry.getInstance(); } @Override diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java index f25ea37db1..c12dfedbfe 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java @@ -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 types = Sets.newHashSet(); for (final BgpParameters param : open.getBgpParameters()) { diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java index 907765525b..69a6b0a5d1 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java @@ -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); diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java index 4e9aa25dd6..50f01b5aaf 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/PathAttributeParserTest.java @@ -24,7 +24,7 @@ public class PathAttributeParserTest { @Before public void setUp() { // Activates everything - SimpleBGPMessageFactory.getInstance(); + BGPMessageFactoryImpl.getInstance(); } @Test diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.java index 0b209e8953..4ca8627dd8 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.java @@ -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 factory = SimpleBGPMessageFactory.getInstance(); + final ProtocolMessageFactory factory = BGPMessageFactoryImpl.getInstance(); @Test public void testHeaderErrors() throws DeserializerException, DocumentedException { diff --git a/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java b/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java index 522dcaf69c..517903a16f 100644 --- a/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java +++ b/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java @@ -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 parsePrevious(final List msgs) { final List messages = Lists.newArrayList(); - final ProtocolMessageFactory parser = SimpleBGPMessageFactory.getInstance(); + final ProtocolMessageFactory parser = BGPMessageFactoryImpl.getInstance(); try { for (final byte[] b : msgs) { diff --git a/bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java b/bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java index 123ba263f1..9f9e73cecc 100644 --- a/bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java +++ b/bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java @@ -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 { diff --git a/bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java b/bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java index d02789c61a..bc97576179 100644 --- a/bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java +++ b/bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java @@ -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, L extends SessionLi final SessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs); final BGPSpeakerMock mock = new BGPSpeakerMock<>(snf, - new BGPHandlerFactory(SimpleBGPMessageFactory.getInstance()), + new BGPHandlerFactory(BGPMessageFactoryImpl.getInstance()), new DefaultPromise(GlobalEventExecutor.INSTANCE)); mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f); -- 2.36.6