Disconnect bgp-rib-impl from global event loop groups
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
index 7d924de27cf7fbcbb9e9b1100cbc9fcd9c8d6bdf..5d321ba29aa65c9c10550a87f94add47ceca4c4c 100644 (file)
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.VisibleForTesting;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.AdaptiveRecvByteBufAllocator;
 import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
-import io.netty.channel.EventLoopGroup;
+import io.netty.channel.RecvByteBufAllocator;
+import io.netty.channel.WriteBufferWaterMark;
 import io.netty.channel.socket.SocketChannel;
+import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GlobalEventExecutor;
 import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
-import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
+import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
+import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
 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.ReconnectStrategy;
-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;
+import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
+import org.opendaylight.protocol.concepts.KeyMapping;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of BGPDispatcher.
  */
-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);
+@Singleton
+@Component(immediate = true)
+public final class BGPDispatcherImpl implements BGPDispatcher {
+    private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
+    private static final int SOCKET_BACKLOG_SIZE = 128;
+
+    private static final WriteBufferWaterMark WATER_MARK = new WriteBufferWaterMark(128 * 1024, 256 * 1024);
+
+    // An adaptive allocator, so we size our message buffers based on what we receive, but make sure we process one
+    // message at a time. This should be good enough for most cases, although we could optimize it a bit based on
+    // whether we actually negotiate use of large messages -- based on that the range of allocations can be constrained
+    // from the default 64-65536 range to 64-4096.
+    private static final RecvByteBufAllocator RECV_ALLOCATOR = new AdaptiveRecvByteBufAllocator().maxMessagesPerRead(1);
+
+    private final BGPHandlerFactory handlerFactory;
+    private final BGPPeerRegistry bgpPeerRegistry;
+    private final BGPNettyGroups nettyGroups;
+
+    @Inject
+    @Activate
+    public BGPDispatcherImpl(@Reference final BGPExtensionConsumerContext extensions,
+            @Reference final BGPNettyGroups nettyGroups, @Reference final BGPPeerRegistry bgpPeerRegistry) {
+        this.nettyGroups = requireNonNull(nettyGroups);
+        this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
+        handlerFactory = new BGPHandlerFactory(extensions.getMessageRegistry());
     }
 
-    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;
+    @VisibleForTesting
+    public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress,
+            final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
+        final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.of(), reuseAddress, localAddress);
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(bgpPeerRegistry);
+        final ChannelPipelineInitializer<BGPSessionImpl> initializer = BGPChannel.createChannelPipelineInitializer(
+                handlerFactory, snf);
+
+        final BGPProtocolSessionPromise<BGPSessionImpl> sessionPromise = new BGPProtocolSessionPromise<>(remoteAddress,
+                retryTimer, clientBootStrap, bgpPeerRegistry);
+        clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
+        sessionPromise.connect();
+        LOG.debug("Client created.");
+        return sessionPromise;
     }
 
-    @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());
-            }
-        });
+    private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
+            final InetSocketAddress localAddress) {
+        return nettyGroups.createBootstrap(keys)
+            // Make sure we are doing round-robin processing
+            .option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR)
+            .option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
+            .option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK)
+            .option(ChannelOption.SO_REUSEADDR, reuseAddress)
+            .localAddress(localAddress);
     }
 
     @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);
+    public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
+            final InetSocketAddress localAddress, final int retryTimer, final KeyMapping keys) {
+        return createReconnectingClient(remoteAddress, retryTimer, keys, localAddress, false);
     }
 
-    @Override
-    public void close() {
+    @VisibleForTesting
+    synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
+            final int retryTimer, final KeyMapping keys, final InetSocketAddress localAddress,
+            final boolean reuseAddress) {
+        final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(bgpPeerRegistry);
+        final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress, localAddress);
+        final BGPReconnectPromise<?> reconnectPromise = new BGPReconnectPromise<>(GlobalEventExecutor.INSTANCE,
+                remoteAddress, retryTimer, bootstrap, bgpPeerRegistry,
+                BGPChannel.createChannelPipelineInitializer(handlerFactory, snf));
+        reconnectPromise.connect();
+        return reconnectPromise;
     }
 
     @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;
+    public synchronized ChannelFuture createServer(final InetSocketAddress serverAddress) {
+        final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(bgpPeerRegistry);
+        final ChannelPipelineInitializer<?> initializer = BGPChannel.createChannelPipelineInitializer(
+            handlerFactory, snf);
+        final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
+        final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
+        LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
+        return channelFuture;
     }
 
     @Override
-    public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
-        return this.createServer(registry, address, sessionValidator, null);
+    public BGPPeerRegistry getBGPPeerRegistry() {
+        return bgpPeerRegistry;
     }
 
-    @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;
+    private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
+        return nettyGroups.createServerBootstrap()
+            .childHandler(BGPChannel.createServerChannelHandler(initializer))
+            .option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE)
+            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
+            .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK)
+            // Make sure we are doing round-robin processing
+            .option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
     }
 
-    @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);
+    private static final class BGPChannel {
+        private static final String NEGOTIATOR = "negotiator";
+
+        private BGPChannel() {
+
         }
-    }
 
-    @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);
+        static <S extends BGPSession, T extends BGPSessionNegotiatorFactory<S>> ChannelPipelineInitializer<S>
+            createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
+            return (channel, promise) -> {
+                channel.pipeline().addLast(hf.getDecoders());
+                channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
+                channel.pipeline().addLast(hf.getEncoders());
+            };
         }
-    }
 
-}
+        static <S extends BGPSession> ChannelHandler createClientChannelHandler(
+                final ChannelPipelineInitializer<S> initializer, final Promise<S> promise) {
+            return new ChannelInitializer<SocketChannel>() {
+                @Override
+                protected void initChannel(final SocketChannel channel) {
+                    initializer.initializeChannel(channel, promise);
+                }
+            };
+        }
+
+        static <S extends BGPSession> ChannelHandler createServerChannelHandler(
+                final ChannelPipelineInitializer<S> initializer) {
+            return new ChannelInitializer<SocketChannel>() {
+                @Override
+                protected void initChannel(final SocketChannel channel) {
+                    initializer.initializeChannel(channel, new DefaultPromise<>(GlobalEventExecutor.INSTANCE));
+                }
+            };
+        }
+    }
+}
\ No newline at end of file