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