BUG-2873 : Remove dependency protocol-framework on BGP
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.java
index 280788c53b03f89a571579f4b648fa1b480aa28a..80a8336b1672f777391861f127919a5e586e5d49 100644 (file)
@@ -9,15 +9,14 @@ package org.opendaylight.protocol.bgp.testtool;
 
 import com.google.common.base.Preconditions;
 import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
 import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.GlobalEventExecutor;
-import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
 import java.util.HashMap;
 import java.util.Map;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
+import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
 import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionNegotiatorFactory;
 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
@@ -27,10 +26,6 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
-import org.opendaylight.protocol.framework.AbstractDispatcher;
-import org.opendaylight.protocol.framework.ProtocolSession;
-import org.opendaylight.protocol.framework.SessionListener;
-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.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
@@ -41,44 +36,30 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
-import org.opendaylight.yangtools.yang.binding.Notification;
 
-public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
+public class BGPSpeakerMock {
 
-    private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
+    private final BGPServerSessionNegotiatorFactory negotiatorFactory;
     private final BGPHandlerFactory factory;
+    private final BGPDispatcherImpl disp;
+    private final BGPPeerRegistry peerRegistry;
+    private final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables;
 
-    public BGPSpeakerMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final BGPHandlerFactory factory,
-        final DefaultPromise<BGPSessionImpl> defaultPromise) {
-        super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
+    private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
+                           final DefaultPromise<BGPSessionImpl> defaultPromise) {
+        disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
         this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
         this.factory = Preconditions.checkNotNull(factory);
-    }
 
-    public void createServer(final InetSocketAddress address) {
-        super.createServer(address, new PipelineInitializer<S>() {
 
+        peerRegistry = new BGPPeerRegistry() {
             @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(null, ch, promise));
-                ch.pipeline().addLast(BGPSpeakerMock.this.factory.getEncoders());
+            public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {
             }
-        });
-    }
-
-    public static void main(final String[] args) throws Exception {
-        final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables = new HashMap<>();
-        tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
-        tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
 
-        final BGPPeerRegistry peerRegistry = new BGPPeerRegistry() {
             @Override
-            public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {}
-
-            @Override
-            public void removePeer(final IpAddress ip) {}
+            public void removePeer(final IpAddress ip) {
+            }
 
             @Override
             public boolean isPeerConfigured(final IpAddress ip) {
@@ -101,18 +82,35 @@ public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionLi
             }
 
             @Override
-            public void removePeerSession(final IpAddress ip) {}
+            public void removePeerSession(final IpAddress ip) {
+            }
         };
 
-        final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPServerSessionNegotiatorFactory(new BGPSessionValidator() {
+        tables = new HashMap<>();
+        tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+        tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+    }
+
+    public void main(final String[] args) {
+
+        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(new BGPSessionValidator() {
             @Override
             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
                 // NOOP
             }
         }, peerRegistry);
 
-        final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<>(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+        final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
         mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
     }
+
+    private void createServer(final InetSocketAddress address) {
+        disp.createServer(peerRegistry,address, new BGPSessionValidator() {
+            @Override
+            public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
+                // NOOP
+            }
+        });
+    }
 }