Remove reliance on BGPMessageFactory
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.java
index 38a324867aeef5e75c02b8a13005520b4a8367a4..8f7a63cea2f1bc9b551608fe3a1db2ba9c5fb7e2 100644 (file)
@@ -7,54 +7,58 @@
  */
 package org.opendaylight.protocol.bgp.testtool;
 
+import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.util.HashedWheelTimer;
 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.BGPMessage;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
+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;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
-import org.opendaylight.protocol.concepts.IPv4;
 import org.opendaylight.protocol.framework.AbstractDispatcher;
-import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
 import org.opendaylight.protocol.framework.ProtocolSession;
 import org.opendaylight.protocol.framework.SessionListener;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
-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;
+import org.opendaylight.yangtools.yang.binding.Notification;
 
 import com.google.common.base.Preconditions;
 
-public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
-AbstractDispatcher<S, L> {
+public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
 
        private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
-       private final ProtocolHandlerFactory<?> factory;
+       private final BGPHandlerFactory factory;
 
-       public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
+       public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
                        final DefaultPromise<BGPSessionImpl> defaultPromise) {
+               super(new NioEventLoopGroup(), new NioEventLoopGroup());
                this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
                this.factory = Preconditions.checkNotNull(factory);
        }
 
-       @Override
-       public void initializeChannel(final SocketChannel ch, final Promise<S> promise, final SessionListenerFactory<L> listenerFactory) {
-               ch.pipeline().addLast(this.factory.getDecoders());
-               ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
-               ch.pipeline().addLast(this.factory.getEncoders());
+       public void createServer(final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory) {
+               super.createServer(address, new PipelineInitializer<S>() {
+
+                       @Override
+                       public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
+                               ch.pipeline().addLast(BGPSpeakerMock.this.factory.getDecoders());
+                               ch.pipeline().addLast("negotiator",
+                                               BGPSpeakerMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
+                               ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
+                       }
+               });
        }
 
-       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
@@ -63,11 +67,13 @@ AbstractDispatcher<S, L> {
                        }
                };
 
-               final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, new AsNumber((long) 25), IPv4.FAMILY.addressForString("127.0.0.2")).getProposal();
+               final BGPSessionPreferences prefs = new BGPSessionProposalImpl((short) 90, 25, new Ipv4Address("127.0.0.2")).getProposal();
 
-               final SessionNegotiatorFactory<BGPMessage, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
+               final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
 
-               final BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<BGPMessage, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(new BGPMessageFactoryImpl()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+               final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf,
+                               new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry()),
+                               new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
                mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
        }