BUG-7386 Remove blocking gracefulshutdown
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.bootstrap.ServerBootstrap;
15 import io.netty.buffer.PooledByteBufAllocator;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandler;
18 import io.netty.channel.ChannelInitializer;
19 import io.netty.channel.ChannelOption;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.epoll.Epoll;
22 import io.netty.channel.epoll.EpollChannelOption;
23 import io.netty.channel.epoll.EpollEventLoopGroup;
24 import io.netty.channel.epoll.EpollMode;
25 import io.netty.channel.epoll.EpollServerSocketChannel;
26 import io.netty.channel.epoll.EpollSocketChannel;
27 import io.netty.channel.socket.SocketChannel;
28 import io.netty.channel.socket.nio.NioServerSocketChannel;
29 import io.netty.channel.socket.nio.NioSocketChannel;
30 import io.netty.util.concurrent.DefaultPromise;
31 import io.netty.util.concurrent.Future;
32 import io.netty.util.concurrent.GlobalEventExecutor;
33 import io.netty.util.concurrent.Promise;
34 import java.net.InetSocketAddress;
35 import java.util.concurrent.TimeUnit;
36 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
37 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
38 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
42 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
43 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
44 import org.opendaylight.protocol.concepts.KeyMapping;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Implementation of BGPDispatcher.
50  */
51 public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
52     private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
53     private static final int SOCKET_BACKLOG_SIZE = 128;
54     private static final int HIGH_WATER_MARK = 256 * 1024;
55     private static final int LOW_WATER_MARK = 128 * 1024;
56     private static final long TIMEOUT = 10;
57
58     private final BGPHandlerFactory handlerFactory;
59     private final EventLoopGroup bossGroup;
60     private final EventLoopGroup workerGroup;
61
62     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
63         if (Epoll.isAvailable()) {
64             this.bossGroup = new EpollEventLoopGroup();
65             this.workerGroup = new EpollEventLoopGroup();
66         } else {
67             this.bossGroup = Preconditions.checkNotNull(bossGroup);
68             this.workerGroup = Preconditions.checkNotNull(workerGroup);
69         }
70         this.handlerFactory = new BGPHandlerFactory(messageRegistry);
71     }
72
73     @Override
74     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry listener, final int retryTimer) {
75         return createClient(remoteAddress, listener, retryTimer, createClientBootStrap(Optional.absent(), false));
76     }
77
78     private synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry listener, final int retryTimer,
79             final Bootstrap clientBootStrap) {
80         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(listener);
81         final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
82
83         final BGPProtocolSessionPromise sessionPromise = new BGPProtocolSessionPromise(remoteAddress, retryTimer, clientBootStrap, listener);
84         clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
85         sessionPromise.connect();
86         LOG.debug("Client created.");
87         return sessionPromise;
88     }
89
90     @VisibleForTesting
91     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
92             final BGPPeerRegistry strictBGPPeerRegistry, final int retryTimer, final boolean reuseAddress) {
93         final Bootstrap clientBootStrap = createClientBootStrap(Optional.absent(), reuseAddress);
94         clientBootStrap.localAddress(localAddress);
95         return createClient(remoteAddress, strictBGPPeerRegistry, retryTimer, clientBootStrap);
96     }
97
98     private synchronized Bootstrap createClientBootStrap(final Optional<KeyMapping> keys, final boolean reuseAddress) {
99         final Bootstrap bootstrap = new Bootstrap();
100         if (Epoll.isAvailable()) {
101             bootstrap.channel(EpollSocketChannel.class);
102             bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
103         } else {
104             bootstrap.channel(NioSocketChannel.class);
105         }
106         if (keys.isPresent()) {
107             if (Epoll.isAvailable()) {
108                 bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.get());
109             } else {
110                 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
111             }
112         }
113
114         // Make sure we are doing round-robin processing
115         bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
116         bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
117         bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
118         bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
119         bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
120
121         if (bootstrap.group() == null) {
122             bootstrap.group(this.workerGroup);
123         }
124
125         return bootstrap;
126     }
127
128     @Override
129     public synchronized void close() throws InterruptedException {
130         if (Epoll.isAvailable()) {
131             LOG.debug("Closing Dispatcher");
132             this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
133             this.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
134         }
135     }
136
137     @Override
138     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry peerRegistry,
139             final int retryTimer, final Optional<KeyMapping> keys) {
140         return createReconnectingClient(remoteAddress, peerRegistry, retryTimer, keys, null, false);
141     }
142
143     @VisibleForTesting
144     protected synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry peerRegistry,
145         final int retryTimer, final Optional<KeyMapping> keys, final InetSocketAddress localAddress, final boolean reuseAddress) {
146         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(peerRegistry);
147         final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress);
148         bootstrap.localAddress(localAddress);
149         final BGPReconnectPromise reconnectPromise = new BGPReconnectPromise(GlobalEventExecutor.INSTANCE, remoteAddress,
150             retryTimer, bootstrap, peerRegistry, BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
151         reconnectPromise.connect();
152         return reconnectPromise;
153     }
154
155     @Override
156     public synchronized ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress serverAddress) {
157         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(registry);
158         final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
159         final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
160         final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
161         LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
162         return channelFuture;
163     }
164
165     private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
166         final ServerBootstrap serverBootstrap = new ServerBootstrap();
167         if (Epoll.isAvailable()) {
168             serverBootstrap.channel(EpollServerSocketChannel.class);
169             serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
170         } else {
171             serverBootstrap.channel(NioServerSocketChannel.class);
172         }
173         final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
174         serverBootstrap.childHandler(serverChannelHandler);
175
176         serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
177         serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
178         serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
179         serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
180
181         // Make sure we are doing round-robin processing
182         serverBootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
183
184         if (serverBootstrap.group() == null) {
185             serverBootstrap.group(this.bossGroup, this.workerGroup);
186         }
187         return serverBootstrap;
188     }
189
190     private static final class BGPChannel {
191         private static final String NEGOTIATOR = "negotiator";
192
193         private BGPChannel() {
194
195         }
196
197         static <T extends BGPSessionNegotiatorFactory> ChannelPipelineInitializer
198         createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
199             return (channel, promise) -> {
200                 channel.pipeline().addLast(hf.getDecoders());
201                 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
202                 channel.pipeline().addLast(hf.getEncoders());
203             };
204         }
205
206         static <S extends BGPSession> ChannelHandler createClientChannelHandler(final ChannelPipelineInitializer initializer, final Promise<S> promise) {
207             return new ChannelInitializer<SocketChannel>() {
208                 @Override
209                 protected void initChannel(final SocketChannel channel) {
210                     initializer.initializeChannel(channel, promise);
211                 }
212             };
213         }
214
215         static ChannelHandler createServerChannelHandler(final ChannelPipelineInitializer initializer) {
216             return new ChannelInitializer<SocketChannel>() {
217                 @Override
218                 protected void initChannel(final SocketChannel channel) {
219                     initializer.initializeChannel(channel, new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
220                 }
221             };
222         }
223     }
224 }