Merge "BUG-108: further progress on implementation"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index d6d165fc1520fca1e7a50becc6cc8e4b1641449c..f53c85ea5df5f6ef3d72845fa278aff4e952abec 100644 (file)
@@ -7,43 +7,56 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.Timer;
 import io.netty.util.concurrent.Future;
-
-import java.net.InetSocketAddress;
-
-import org.opendaylight.protocol.bgp.parser.BGPMessage;
-import org.opendaylight.protocol.bgp.parser.BGPSession;
+import io.netty.util.concurrent.Promise;
+import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
-import org.opendaylight.protocol.framework.Dispatcher;
-import org.opendaylight.protocol.framework.ProtocolMessageFactory;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
 import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.SessionListenerFactory;
 
-import com.google.common.base.Preconditions;
+import java.net.InetSocketAddress;
 
 /**
  * Implementation of BGPDispatcher.
  */
-public final class BGPDispatcherImpl implements BGPDispatcher {
+public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
        private final Timer timer = new HashedWheelTimer();
-       private final ProtocolMessageFactory<BGPMessage> parser;
-       private final Dispatcher dispatcher;
 
-       public BGPDispatcherImpl(final Dispatcher dispatcher, final ProtocolMessageFactory<BGPMessage> parser) {
-               this.dispatcher = Preconditions.checkNotNull(dispatcher);
-               this.parser = Preconditions.checkNotNull(parser);
+       private final BGPHandlerFactory hf;
+
+       public BGPDispatcherImpl(final BGPMessageFactory parser, EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
+               super(bossGroup, workerGroup);
+               this.hf = new BGPHandlerFactory(parser);
        }
 
        @Override
-       public Future<? extends BGPSession> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
+       public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
                        final BGPSessionListener listener, final ReconnectStrategy strategy) {
-               return this.dispatcher.createClient(address, listener, new BGPSessionNegotiatorFactory(timer, preferences), parser, strategy);
+               final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
+               final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
+                       @Override
+                       public BGPSessionListener getSessionListener() {
+                               return listener;
+                       }
+               };
+               return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
+                       @Override
+                       public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
+                               ch.pipeline().addLast(hf.getDecoders());
+                               ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
+                               ch.pipeline().addLast(hf.getEncoders());
+                       }
+               });
        }
 
-       public Dispatcher getDispatcher() {
-               return this.dispatcher;
+       @Override
+               public void close() throws Exception {
        }
 }