BUG-185 : implemented Best Path Selection Algorithm.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index 709965bcc4c111f4e10055e1c2f21a2280580f0d..e159c7568e7b77d94631e9dbd8f1ca997f985464 100644 (file)
@@ -7,33 +7,81 @@
  */
 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 io.netty.util.concurrent.Promise;
 
-import java.io.IOException;
+import java.net.InetSocketAddress;
 
-import org.opendaylight.protocol.bgp.parser.BGPSession;
-import org.opendaylight.protocol.bgp.rib.impl.spi.BGPConnection;
+import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
+import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
-import org.opendaylight.protocol.framework.Dispatcher;
-import org.opendaylight.protocol.framework.ProtocolMessageFactory;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
+import org.opendaylight.protocol.framework.SessionListenerFactory;
 
 /**
  * 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 Dispatcher dispatcher;
+       private final BGPHandlerFactory hf;
 
-       public BGPDispatcherImpl(final Dispatcher dispatcher) throws IOException {
-               this.dispatcher = dispatcher;
+       public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
+               super(bossGroup, workerGroup);
+               this.hf = new BGPHandlerFactory(messageRegistry);
        }
 
        @Override
-       public Future<? extends BGPSession> createClient(final BGPConnection connection, final ProtocolMessageFactory parser) throws IOException {
-               return this.dispatcher.createClient(connection, new BGPSessionFactory(parser));
+       public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
+                       final BGPSessionListener listener, final ReconnectStrategy 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 Future<Void> createReconnectingClient(final InetSocketAddress address,
+                       final BGPSessionPreferences preferences, final BGPSessionListener listener,
+                       final ReconnectStrategyFactory connectStrategyFactory,
+                       final ReconnectStrategy reestablishStrategy) {
+               final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
+               final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
+                       @Override
+                       public BGPSessionListener getSessionListener() {
+                               return listener;
+                       }
+               };
+
+               return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy, 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());
+                       }
+               });
+       }
+
+       @Override
+       public void close() {
        }
 }