BUG-113: split off singleton nature of BGPExtensionProviderContextImpl 23/2023/2
authorRobert Varga <rovarga@cisco.com>
Sat, 19 Oct 2013 10:50:20 +0000 (12:50 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 20 Oct 2013 09:03:50 +0000 (11:03 +0200)
Also rename it into SimpleBGPExtensionProviderContext and start using it
in OSGi contexts.

Change-Id: I74cb00bce2c255c497f4af717c306da34c8a470e
Signed-off-by: Robert Varga <rovarga@cisco.com>
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/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/osgi/OSGiBGPExtensionConsumerContext.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/ServiceLoaderBGPExtensionProviderContext.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionConsumerContext.java [new file with mode: 0644]
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionProviderContext.java [moved from bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/BGPExtensionProviderContextImpl.java with 60% similarity]
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ParserTest.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
concepts/src/main/java/org/opendaylight/protocol/concepts/HandlerRegistry.java

index 8588411ef070fc3ac15cb852d07a44364d858da9..a17d4faf653fa0bea71a5a7691c127081ced38b4 100644 (file)
@@ -26,7 +26,7 @@ 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.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
@@ -85,7 +85,7 @@ public class BGPParserTest {
 
        @BeforeClass
        public static void setUp() throws Exception {
-               updateParser = new BGPUpdateMessageParser(BGPExtensionProviderContextImpl.getSingletonInstance().getAttributeRegistry());
+               updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getAttributeRegistry());
 
                for (int i = 1; i <= COUNTER; i++) {
                        final String name = "/up" + i + ".bin";
@@ -1058,7 +1058,7 @@ public class BGPParserTest {
         */
        @Test
        public void testOpenMessage() throws Exception {
-               final MessageRegistry msgReg = BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry();
+               final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
                final Open open = (Open) msgReg.parseMessage(inputBytes.get(13));
                final Set<BgpTableType> types = Sets.newHashSet();
                for (final BgpParameters param : open.getBgpParameters()) {
index b45305c9d20338273e70d3c3bed7f4e6e81f9bfa..a25092369b3fbfa692f8726b549458365d05cbc0 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.fail;
 
-import java.io.IOException;
 import java.util.Map;
 
 import org.junit.Test;
@@ -21,7 +20,7 @@ import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
 import org.opendaylight.protocol.util.ByteList;
@@ -220,8 +219,8 @@ public class ComplementaryTest {
        }
 
        @Test
-       public void testBGPHeaderParser() throws IOException {
-               final MessageRegistry msgReg = BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry();
+       public void testBGPHeaderParser() throws Exception {
+               final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
                try {
                        msgReg.parseMessage(new byte[] { (byte) 0, (byte) 0 });
                        fail("Exception should have occured.");
@@ -249,8 +248,8 @@ public class ComplementaryTest {
        }
 
        @Test
-       public void testMessageParser() throws IOException {
-               final MessageRegistry msgReg = BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry();
+       public void testMessageParser() throws Exception {
+               final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
                String ex = "";
                try {
                        msgReg.serializeMessage(null);
index 7f32720be1f8be9db78d00676f1dff518b51cf6c..edbabd5188eb073165bd7195e823b09a3592251d 100644 (file)
@@ -14,16 +14,16 @@ import static org.junit.Assert.fail;
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 
 /*
  * To test incorrect values.
  */
 public class PathAttributeParserTest {
        @Test
-       public void testOriginParser() {
+       public void testOriginParser() throws Exception {
                try {
-                       BGPExtensionProviderContextImpl.getSingletonInstance().getAttributeRegistry().parseAttributes(new byte[] { 0x40, 0x01, 0x01, 0x04 });
+                       ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getAttributeRegistry().parseAttributes(new byte[] { 0x40, 0x01, 0x01, 0x04 });
                        fail("This needs to fail.");
                } catch (final BGPDocumentedException e) {
                        assertEquals("Unknown Origin type.", e.getMessage());
index 3954f68800c268a50160a54d4d898cadb9e11f96..1d34eb83fc17e7ab3bad9334a7af97e62cd4dbeb 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
 import org.opendaylight.protocol.concepts.AbstractRegistration;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
@@ -22,13 +23,14 @@ import org.osgi.framework.ServiceRegistration;
 import com.google.common.base.Preconditions;
 
 class OSGiBGPExtensionConsumerContext implements BGPExtensionConsumerContext {
+       protected final SimpleBGPExtensionProviderContext providerContext = new SimpleBGPExtensionProviderContext();
        protected final BundleContext bundleContext;
 
        OSGiBGPExtensionConsumerContext(final BundleContext context) {
                this.bundleContext = Preconditions.checkNotNull(context);
        }
 
-       protected <T> AutoCloseable register(final Class<T> clazz, final T object) {
+       protected final <T> AutoCloseable register(final Class<T> clazz, final T object) {
                final ServiceRegistration<T> reg = bundleContext.registerService(clazz, object, null);
 
                return new AbstractRegistration() {
@@ -39,50 +41,42 @@ class OSGiBGPExtensionConsumerContext implements BGPExtensionConsumerContext {
                };
        }
 
-       public BundleContext getBundleContext() {
+       public final BundleContext getBundleContext() {
                return bundleContext;
        }
 
        @Override
-       public AddressFamilyRegistry getAddressFamilyRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final AddressFamilyRegistry getAddressFamilyRegistry() {
+               return providerContext.getAddressFamilyRegistry();
        }
 
        @Override
-       public AttributeRegistry getAttributeRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final AttributeRegistry getAttributeRegistry() {
+               return providerContext.getAttributeRegistry();
        }
 
        @Override
-       public CapabilityRegistry getCapabilityRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final CapabilityRegistry getCapabilityRegistry() {
+               return providerContext.getCapabilityRegistry();
        }
 
        @Override
-       public MessageRegistry getMessageRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final MessageRegistry getMessageRegistry() {
+               return providerContext.getMessageRegistry();
        }
 
        @Override
-       public NlriRegistry getNlriRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final NlriRegistry getNlriRegistry() {
+               return providerContext.getNlriRegistry();
        }
 
        @Override
-       public ParameterRegistry getParameterRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final ParameterRegistry getParameterRegistry() {
+               return providerContext.getParameterRegistry();
        }
 
        @Override
-       public SubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry() {
-               // TODO Auto-generated method stub
-               return null;
+       public final SubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry() {
+               return providerContext.getSubsequentAddressFamilyRegistry();
        }
-
 }
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/ServiceLoaderBGPExtensionProviderContext.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/ServiceLoaderBGPExtensionProviderContext.java
new file mode 100644 (file)
index 0000000..57a8a58
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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.pojo;
+
+import java.util.ServiceLoader;
+
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderActivator;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
+
+public final class ServiceLoaderBGPExtensionProviderContext {
+       private ServiceLoaderBGPExtensionProviderContext() {
+
+       }
+
+       public static BGPExtensionConsumerContext createConsumerContext() throws Exception {
+               final BGPExtensionProviderContext ctx = new SimpleBGPExtensionProviderContext();
+
+               final ServiceLoader<BGPExtensionProviderActivator> loader = ServiceLoader.load(BGPExtensionProviderActivator.class);
+               for (BGPExtensionProviderActivator a : loader) {
+                       a.start(ctx);
+               }
+
+               return ctx;
+       }
+}
diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionConsumerContext.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleBGPExtensionConsumerContext.java
new file mode 100644 (file)
index 0000000..58288c3
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.pojo;
+
+import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
+import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
+
+class SimpleBGPExtensionConsumerContext implements BGPExtensionConsumerContext {
+       protected final SimpleAddressFamilyRegistry afiReg = new SimpleAddressFamilyRegistry();
+       protected final SimpleAttributeRegistry attrReg = new SimpleAttributeRegistry();
+       protected final SimpleCapabilityRegistry capReg = new SimpleCapabilityRegistry();
+       protected final SimpleMessageRegistry msgReg = new SimpleMessageRegistry();
+       protected final SimpleSubsequentAddressFamilyRegistry safiReg = new SimpleSubsequentAddressFamilyRegistry();
+       protected final SimpleParameterRegistry paramReg = new SimpleParameterRegistry();
+       protected final SimpleNlriRegistry nlriReg = new SimpleNlriRegistry(afiReg, safiReg);
+
+       @Override
+       public final AddressFamilyRegistry getAddressFamilyRegistry() {
+               return afiReg;
+       }
+
+       @Override
+       public final AttributeRegistry getAttributeRegistry() {
+               return attrReg;
+       }
+
+       @Override
+       public final CapabilityRegistry getCapabilityRegistry() {
+               return capReg;
+       }
+
+       @Override
+       public final MessageRegistry getMessageRegistry() {
+               return msgReg;
+       }
+
+       @Override
+       public final NlriRegistry getNlriRegistry() {
+               return nlriReg;
+       }
+
+       @Override
+       public final ParameterRegistry getParameterRegistry() {
+               return paramReg;
+       }
+
+       @Override
+       public final SubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry() {
+               return safiReg;
+       }
+}
@@ -7,28 +7,17 @@
  */
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
-import java.util.ServiceLoader;
-
-import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
-import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
-import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderActivator;
 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
-import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
 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.bgp.parser.spi.NlriParser;
-import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
-import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
-import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
@@ -36,61 +25,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
-public final class BGPExtensionProviderContextImpl implements BGPExtensionProviderContext {
-       private static final class Holder {
-               private static final BGPExtensionConsumerContext INSTANCE;
-
-               static {
-                       try {
-                               INSTANCE = BGPExtensionProviderContextImpl.create();
-                       } catch (Exception e) {
-                               throw new ExceptionInInitializerError(e);
-                       }
-               }
-       }
-
-       private final SimpleAddressFamilyRegistry afiReg = new SimpleAddressFamilyRegistry();
-       private final SimpleAttributeRegistry attrReg = new SimpleAttributeRegistry();
-       private final SimpleCapabilityRegistry capReg = new SimpleCapabilityRegistry();
-       private final SimpleMessageRegistry msgReg = new SimpleMessageRegistry();
-       private final SimpleSubsequentAddressFamilyRegistry safiReg = new SimpleSubsequentAddressFamilyRegistry();
-       private final SimpleParameterRegistry paramReg = new SimpleParameterRegistry();
-       private final SimpleNlriRegistry nlriReg = new SimpleNlriRegistry(afiReg, safiReg);
-
-       private BGPExtensionProviderContextImpl() {
-
-       }
-
-       public static BGPExtensionConsumerContext getSingletonInstance() {
-               return Holder.INSTANCE;
-       }
-
-       public static BGPExtensionConsumerContext create() throws Exception {
-               final BGPExtensionProviderContextImpl ctx = new BGPExtensionProviderContextImpl();
-
-               final ServiceLoader<BGPExtensionProviderActivator> loader = ServiceLoader.load(BGPExtensionProviderActivator.class);
-               for (BGPExtensionProviderActivator a : loader) {
-                       a.start(ctx);
-               }
-
-               return ctx;
-       }
-
-       @Override
-       public AddressFamilyRegistry getAddressFamilyRegistry() {
-               return afiReg;
-       }
-
+public final class SimpleBGPExtensionProviderContext extends SimpleBGPExtensionConsumerContext implements BGPExtensionProviderContext {
        @Override
        public AutoCloseable registerAddressFamily(final Class<? extends AddressFamily> clazz, final int number) {
                return afiReg.registerAddressFamily(clazz, number);
        }
 
-       @Override
-       public AttributeRegistry getAttributeRegistry() {
-               return attrReg;
-       }
-
        @Override
        public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
                return attrReg.registerAttributeParser(attributeType, parser);
@@ -101,11 +41,6 @@ public final class BGPExtensionProviderContextImpl implements BGPExtensionProvid
                return attrReg.registerAttributeSerializer(attributeClass, serializer);
        }
 
-       @Override
-       public CapabilityRegistry getCapabilityRegistry() {
-               return capReg;
-       }
-
        @Override
        public AutoCloseable registerCapabilityParser(final int capabilityType, final CapabilityParser parser) {
                return capReg.registerCapabilityParser(capabilityType, parser);
@@ -116,11 +51,6 @@ public final class BGPExtensionProviderContextImpl implements BGPExtensionProvid
                return capReg.registerCapabilitySerializer(capabilityClass, serializer);
        }
 
-       @Override
-       public MessageRegistry getMessageRegistry() {
-               return msgReg;
-       }
-
        @Override
        public AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
                return msgReg.registerMessageParser(messageType, parser);
@@ -131,11 +61,6 @@ public final class BGPExtensionProviderContextImpl implements BGPExtensionProvid
                return msgReg.registerMessageSerializer(messageClass, serializer);
        }
 
-       @Override
-       public NlriRegistry getNlriRegistry() {
-               return nlriReg;
-       }
-
        @Override
        public AutoCloseable registerNlriParser(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi,
                        final NlriParser parser) {
@@ -147,11 +72,6 @@ public final class BGPExtensionProviderContextImpl implements BGPExtensionProvid
                throw new UnsupportedOperationException("NLRI serialization not implemented");
        }
 
-       @Override
-       public ParameterRegistry getParameterRegistry() {
-               return paramReg;
-       }
-
        @Override
        public AutoCloseable registerParameterParser(final int parameterType, final ParameterParser parser) {
                return paramReg.registerParameterParser(parameterType, parser);
@@ -162,11 +82,6 @@ public final class BGPExtensionProviderContextImpl implements BGPExtensionProvid
                return paramReg.registerParameterSerializer(paramClass, serializer);
        }
 
-       @Override
-       public SubsequentAddressFamilyRegistry getSubsequentAddressFamilyRegistry() {
-               return safiReg;
-       }
-
        @Override
        public AutoCloseable registerSubsequentAddressFamily(final Class<? extends SubsequentAddressFamily> clazz, final int number) {
                return safiReg.registerSubsequentAddressFamily(clazz, number);
index 6b23256441492af9fcb00b8f50fbc86b341622ae..6308f7893557e2e874cd8ed2a1ff477a8d9462b2 100644 (file)
@@ -20,12 +20,13 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import org.junit.BeforeClass;
 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.spi.MessageRegistry;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -80,8 +81,12 @@ 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 MessageRegistry factory =
-                       BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry();
+       static MessageRegistry factory;
+
+       @BeforeClass
+       public static void setupClass() throws Exception {
+               factory = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
+       }
 
        @Test
        public void testHeaderErrors() throws DeserializerException, DocumentedException {
@@ -89,7 +94,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 };
                wrong = ByteArray.cutBytes(wrong, 16);
                try {
-                       this.factory.parseMessage(wrong);
+                       ParserTest.factory.parseMessage(wrong);
                        fail("Exception should have occcured.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("Too few bytes in passed array. Passed: " + wrong.length + ". Expected: >= 19.", e.getMessage());
@@ -104,7 +109,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                                (byte) 0x00, (byte) 0x13, (byte) 0x08 };
                try {
-                       this.factory.parseMessage(bytes);
+                       ParserTest.factory.parseMessage(bytes);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertEquals(BGPError.BAD_MSG_TYPE, ((BGPDocumentedException) e).getError());
@@ -116,10 +121,10 @@ public class ParserTest {
        @Test
        public void testKeepAliveMsg() throws DeserializerException, DocumentedException {
                final Notification keepAlive = new KeepaliveBuilder().build();
-               final byte[] bytes = this.factory.serializeMessage(keepAlive);
+               final byte[] bytes = ParserTest.factory.serializeMessage(keepAlive);
                assertArrayEquals(keepAliveBMsg, bytes);
 
-               final Notification m = this.factory.parseMessage(bytes);
+               final Notification m = ParserTest.factory.parseMessage(bytes);
 
                assertTrue(m instanceof Keepalive);
        }
@@ -131,7 +136,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x14, (byte) 0x04, (byte) 0x05 };
 
                try {
-                       this.factory.parseMessage(bytes);
+                       ParserTest.factory.parseMessage(bytes);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertThat(e.getMessage(), containsString("Message length field not within valid range."));
@@ -145,10 +150,10 @@ public class ParserTest {
        public void testOpenMessage() throws UnknownHostException, DeserializerException, DocumentedException {
                final Notification open = new OpenBuilder().setMyAsNumber(100).setHoldTimer(180).setBgpIdentifier(new Ipv4Address("20.20.20.20")).setVersion(
                                new ProtocolVersion((short) 4)).build();
-               final byte[] bytes = this.factory.serializeMessage(open);
+               final byte[] bytes = ParserTest.factory.serializeMessage(open);
                assertArrayEquals(openBMsg, bytes);
 
-               final Notification m = this.factory.parseMessage(bytes);
+               final Notification m = ParserTest.factory.parseMessage(bytes);
 
                assertTrue(m instanceof Open);
                assertEquals(100, ((Open) m).getMyAsNumber().intValue());
@@ -165,7 +170,7 @@ public class ParserTest {
                                (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
 
                try {
-                       this.factory.parseMessage(bMsg);
+                       ParserTest.factory.parseMessage(bMsg);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertEquals("Hold time value not acceptable.", e.getMessage());
@@ -183,7 +188,7 @@ public class ParserTest {
                                (byte) 0xff, (byte) 0xff };
 
                try {
-                       this.factory.parseMessage(bMsg);
+                       ParserTest.factory.parseMessage(bMsg);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertEquals("Open message too small.", e.getMessage());
@@ -199,7 +204,7 @@ public class ParserTest {
                                (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
 
                try {
-                       this.factory.parseMessage(bMsg);
+                       ParserTest.factory.parseMessage(bMsg);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertEquals("BGP Protocol version 8 not supported.", e.getMessage());
@@ -213,10 +218,10 @@ public class ParserTest {
        public void testNotificationMsg() throws DeserializerException, DocumentedException {
                Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode()).setErrorSubcode(
                                BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
-               byte[] bytes = this.factory.serializeMessage(notMsg);
+               byte[] bytes = ParserTest.factory.serializeMessage(notMsg);
                assertArrayEquals(notificationBMsg, bytes);
 
-               Notification m = this.factory.parseMessage(bytes);
+               Notification m = ParserTest.factory.parseMessage(bytes);
 
                assertTrue(m instanceof Notify);
                assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
@@ -224,9 +229,9 @@ public class ParserTest {
 
                notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(
                                BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
-               bytes = this.factory.serializeMessage(notMsg);
+               bytes = ParserTest.factory.serializeMessage(notMsg);
 
-               m = this.factory.parseMessage(bytes);
+               m = ParserTest.factory.parseMessage(bytes);
 
                assertTrue(m instanceof Notify);
                assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
@@ -240,7 +245,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x14, (byte) 0x03, (byte) 0x02 };
 
                try {
-                       this.factory.parseMessage(bMsg);
+                       ParserTest.factory.parseMessage(bMsg);
                        fail("Exception should have occured.");
                } catch (final DocumentedException e) {
                        assertEquals("Notification message too small.", e.getMessage());
@@ -257,7 +262,7 @@ public class ParserTest {
                                (byte) 0x00, (byte) 0x15, (byte) 0x03, (byte) 0x02, (byte) 0xaa };
 
                try {
-                       this.factory.parseMessage(bMsg);
+                       ParserTest.factory.parseMessage(bMsg);
                        fail("Exception should have occured.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("BGP Error code 2 and subcode 170 not recognized.", e.getMessage());
@@ -288,7 +293,7 @@ public class ParserTest {
                final Open open = new OpenBuilder().setMyAsNumber(72).setHoldTimer(180).setBgpIdentifier(new Ipv4Address("172.20.160.170")).setVersion(
                                new ProtocolVersion((short) 4)).setBgpParameters(tlvs).build();
 
-               final byte[] result = this.factory.serializeMessage(open);
+               final byte[] result = ParserTest.factory.serializeMessage(open);
 
                // the capabilities can be swapped.
                assertTrue(Arrays.equals(openWithCpblt1, result) || Arrays.equals(openWithCpblt2, result));
index 38a043044f75649648cf02fd65d33c1eaf0b3fba..95e7ff3a2d8673be7f93ed483362f6e4ae77e871 100644 (file)
@@ -9,13 +9,12 @@ package org.opendaylight.protocol.bgp.testtool;
 
 import io.netty.util.concurrent.GlobalEventExecutor;
 
-import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 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,11 +48,12 @@ public class Main {
 
        BGPDispatcherImpl dispatcher;
 
-       public Main() throws IOException {
-               this.dispatcher = new BGPDispatcherImpl(new BGPMessageFactoryImpl(BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry()));
+       public Main() throws Exception {
+               this.dispatcher = new BGPDispatcherImpl(new BGPMessageFactoryImpl(
+                               ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry()));
        }
 
-       public static void main(final String[] args) throws NumberFormatException, IOException {
+       public static void main(final String[] args) throws Exception {
                if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
                        System.out.println(Main.usage);
                        return;
index 98249aa451e5aa8372bb1e3501e940c08c8ac1ec..24db1cae01269a4ec9e96d6eb24729b9e8387dac 100644 (file)
@@ -13,12 +13,11 @@ import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.GlobalEventExecutor;
 import io.netty.util.concurrent.Promise;
 
-import java.io.IOException;
 import java.net.InetSocketAddress;
 
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.BGPExtensionProviderContextImpl;
+import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionNegotiatorFactory;
@@ -59,7 +58,7 @@ public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionLi
                });
        }
 
-       public static void main(final String[] args) throws IOException {
+       public static void main(final String[] args) throws Exception {
 
                final SessionListenerFactory<BGPSessionListener> f = new SessionListenerFactory<BGPSessionListener>() {
                        @Override
@@ -73,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(new BGPMessageFactoryImpl(BGPExtensionProviderContextImpl.getSingletonInstance().getMessageRegistry())),
+                               new BGPHandlerFactory(new BGPMessageFactoryImpl(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry())),
                                new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
                mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
index b38bb496028fe1a9c96035c3c6ca27eb81b1807e..d0165c68f5e32f258987343fb9cbafd8553948f4 100644 (file)
@@ -22,7 +22,7 @@ public class HandlerRegistry<CLASS, PARSER, SERIALIZER> {
                return parsers.get(type);
        }
 
-       public AutoCloseable registerSerializer(final Class<? extends CLASS> clazz, final SERIALIZER serializer) {
+       public AbstractRegistration registerSerializer(final Class<? extends CLASS> clazz, final SERIALIZER serializer) {
                return serializers.register(clazz, serializer);
        }