Merge "BUG-731: last warnings fixed"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index 698743c9a8bb578fe4ed558039b8391268ce113d..7d924de27cf7fbcbb9e9b1100cbc9fcd9c8d6bdf 100644 (file)
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import io.netty.util.HashedWheelTimer;
-import io.netty.util.Timer;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
 import io.netty.util.concurrent.Future;
-
+import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
-
-import org.opendaylight.protocol.bgp.parser.BGPMessage;
-import org.opendaylight.protocol.bgp.parser.BGPSession;
-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.bgp.rib.impl.spi.BGPSessionPreferences;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
 import org.opendaylight.protocol.framework.AbstractDispatcher;
-import org.opendaylight.protocol.framework.ProtocolMessageFactory;
 import org.opendaylight.protocol.framework.ReconnectStrategy;
-
-import com.google.common.base.Preconditions;
+import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
+import org.opendaylight.tcpmd5.api.KeyMapping;
+import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
+import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
+import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 
 /**
  * Implementation of BGPDispatcher.
  */
-public final class BGPDispatcherImpl extends AbstractDispatcher implements BGPDispatcher {
-       private final Timer timer = new HashedWheelTimer();
-       private final ProtocolMessageFactory<BGPMessage> parser;
-
-       public BGPDispatcherImpl(final ProtocolMessageFactory<BGPMessage> parser) {
-               super();
-               this.parser = Preconditions.checkNotNull(parser);
-       }
-
-       @Override
-       public Future<? extends BGPSession> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
-                       final BGPSessionListener listener, final ReconnectStrategy strategy) {
-               return createClient(address, listener, new BGPSessionNegotiatorFactory(timer, preferences), parser, strategy);
-       }
+public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
+    private final MD5ServerChannelFactory<?> scf;
+    private final MD5ChannelFactory<?> cf;
+    private final BGPHandlerFactory hf;
+    private KeyMapping keys;
+
+    public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
+        this(messageRegistry, bossGroup, workerGroup, null, null);
+    }
+
+    public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
+        super(bossGroup, workerGroup);
+        this.hf = new BGPHandlerFactory(messageRegistry);
+        this.cf = cf;
+        this.scf = scf;
+    }
+
+    @Override
+    public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
+        final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
+        return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
+            @Override
+            public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
+                ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
+            }
+        });
+    }
+
+    @Override
+    public Future<Void> createReconnectingClient(final InetSocketAddress address,
+        final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
+        final ReconnectStrategyFactory reestablishStrategyFactory) {
+        return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
+            null);
+    }
+
+    @Override
+    public void close() {
+    }
+
+    @Override
+    public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
+        final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
+        final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
+
+        this.keys = keys;
+        final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
+            @Override
+            public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
+                ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
+            }
+        });
+        this.keys = null;
+
+        return ret;
+    }
+
+    @Override
+    public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
+        return this.createServer(registry, address, sessionValidator, null);
+    }
+
+    @Override
+    public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
+        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
+
+        this.keys = keys;
+        final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
+            @Override
+            public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
+                ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
+                ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
+            }
+        });
+        this.keys = null;
+
+        return ret;
+    }
+
+    @Override
+    protected void customizeBootstrap(final Bootstrap b) {
+        if (this.keys != null && !this.keys.isEmpty()) {
+            if (this.cf == null) {
+                throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
+            }
+            b.channelFactory(this.cf);
+            b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
+            // Make sure we are doing round-robin processing
+            b.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+        }
+    }
+
+    @Override
+    protected void customizeBootstrap(final ServerBootstrap b) {
+        if (this.keys != null && !this.keys.isEmpty()) {
+            if (this.scf == null) {
+                throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
+            }
+            b.channelFactory(this.scf);
+            b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
+            // Make sure we are doing round-robin processing
+            b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+        }
+    }
+
 }