BGPCEP-576: Neighbor’s local address configurable
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
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.FixedRecvByteBufAllocator;
22 import io.netty.channel.WriteBufferWaterMark;
23 import io.netty.channel.epoll.Epoll;
24 import io.netty.channel.epoll.EpollChannelOption;
25 import io.netty.channel.epoll.EpollEventLoopGroup;
26 import io.netty.channel.epoll.EpollMode;
27 import io.netty.channel.epoll.EpollServerSocketChannel;
28 import io.netty.channel.epoll.EpollSocketChannel;
29 import io.netty.channel.socket.SocketChannel;
30 import io.netty.channel.socket.nio.NioServerSocketChannel;
31 import io.netty.channel.socket.nio.NioSocketChannel;
32 import io.netty.util.concurrent.DefaultPromise;
33 import io.netty.util.concurrent.Future;
34 import io.netty.util.concurrent.GlobalEventExecutor;
35 import io.netty.util.concurrent.Promise;
36 import java.net.InetSocketAddress;
37 import java.util.concurrent.TimeUnit;
38 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
39 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
40 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
44 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
45 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
46 import org.opendaylight.protocol.concepts.KeyMapping;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Implementation of BGPDispatcher.
52  */
53 public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
54     private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
55     private static final int SOCKET_BACKLOG_SIZE = 128;
56     private static final int FIX_BUFFER_SIZE = 1;
57     private static final long TIMEOUT = 10;
58
59     private static final WriteBufferWaterMark WATER_MARK = new WriteBufferWaterMark(128 * 1024, 256 * 1024);
60
61     private final BGPHandlerFactory handlerFactory;
62     private final EventLoopGroup bossGroup;
63     private final EventLoopGroup workerGroup;
64     private final BGPPeerRegistry bgpPeerRegistry;
65
66     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup,
67             final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
68         if (Epoll.isAvailable()) {
69             this.bossGroup = new EpollEventLoopGroup();
70             this.workerGroup = new EpollEventLoopGroup();
71         } else {
72             this.bossGroup = requireNonNull(bossGroup);
73             this.workerGroup = requireNonNull(workerGroup);
74         }
75         this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
76         this.handlerFactory = new BGPHandlerFactory(messageRegistry);
77     }
78
79     @VisibleForTesting
80     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress,
81             final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
82         final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.getKeyMapping(), reuseAddress, localAddress);
83         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
84         final ChannelPipelineInitializer<BGPSessionImpl> initializer = BGPChannel.createChannelPipelineInitializer(
85                 this.handlerFactory, snf);
86
87         final BGPProtocolSessionPromise<BGPSessionImpl> sessionPromise = new BGPProtocolSessionPromise<>(remoteAddress,
88                 retryTimer, clientBootStrap, this.bgpPeerRegistry);
89         clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
90         sessionPromise.connect();
91         LOG.debug("Client created.");
92         return sessionPromise;
93     }
94
95     private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
96             final InetSocketAddress localAddress) {
97         final Bootstrap bootstrap = new Bootstrap();
98         if (Epoll.isAvailable()) {
99             bootstrap.channel(EpollSocketChannel.class);
100             bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
101         } else {
102             bootstrap.channel(NioSocketChannel.class);
103         }
104         if (keys != null && !keys.isEmpty()) {
105             if (Epoll.isAvailable()) {
106                 bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
107             } else {
108                 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
109             }
110         }
111
112         // Make sure we are doing round-robin processing
113         bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
114         bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
115         bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
116         bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
117
118         if (bootstrap.config().group() == null) {
119             bootstrap.group(this.workerGroup);
120         }
121         bootstrap.localAddress(localAddress);
122
123         return bootstrap;
124     }
125
126     @Override
127     public synchronized void close() {
128         if (Epoll.isAvailable()) {
129             LOG.debug("Closing Dispatcher");
130             this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
131             this.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
132         }
133     }
134
135     @Override
136     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
137             final InetSocketAddress localAddress, final int retryTimer, final KeyMapping keys) {
138         return createReconnectingClient(remoteAddress, retryTimer, keys, localAddress, false);
139     }
140
141     @VisibleForTesting
142     synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
143             final int retryTimer, final KeyMapping keys, final InetSocketAddress localAddress,
144             final boolean reuseAddress) {
145         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
146         final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress, localAddress);
147         final BGPReconnectPromise<?> reconnectPromise = new BGPReconnectPromise<>(GlobalEventExecutor.INSTANCE,
148                 remoteAddress, retryTimer, bootstrap, this.bgpPeerRegistry,
149                 BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
150         reconnectPromise.connect();
151         return reconnectPromise;
152     }
153
154     @Override
155     public synchronized ChannelFuture createServer(final InetSocketAddress serverAddress) {
156         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.bgpPeerRegistry);
157         final ChannelPipelineInitializer<?> initializer = BGPChannel.
158                 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     @Override
166     public BGPPeerRegistry getBGPPeerRegistry() {
167         return this.bgpPeerRegistry;
168     }
169
170     private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
171         final ServerBootstrap serverBootstrap = new ServerBootstrap();
172         if (Epoll.isAvailable()) {
173             serverBootstrap.channel(EpollServerSocketChannel.class);
174             serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
175         } else {
176             serverBootstrap.channel(NioServerSocketChannel.class);
177         }
178         final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
179         serverBootstrap.childHandler(serverChannelHandler);
180
181         serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
182         serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
183         serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
184
185         // Make sure we are doing round-robin processing
186         serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
187
188         if (serverBootstrap.config().group() == null) {
189             serverBootstrap.group(this.bossGroup, this.workerGroup);
190         }
191         return serverBootstrap;
192     }
193
194     private static final class BGPChannel {
195         private static final String NEGOTIATOR = "negotiator";
196
197         private BGPChannel() {
198
199         }
200
201         static <S extends BGPSession, T extends BGPSessionNegotiatorFactory<S>> ChannelPipelineInitializer<S>
202         createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
203             return (channel, promise) -> {
204                 channel.pipeline().addLast(hf.getDecoders());
205                 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
206                 channel.pipeline().addLast(hf.getEncoders());
207             };
208         }
209
210         static <S extends BGPSession> ChannelHandler createClientChannelHandler(
211                 final ChannelPipelineInitializer<S> initializer, final Promise<S> promise) {
212             return new ChannelInitializer<SocketChannel>() {
213                 @Override
214                 protected void initChannel(final SocketChannel channel) {
215                     initializer.initializeChannel(channel, promise);
216                 }
217             };
218         }
219
220         static ChannelHandler createServerChannelHandler(final ChannelPipelineInitializer initializer) {
221             return new ChannelInitializer<SocketChannel>() {
222                 @Override
223                 @SuppressWarnings("unchecked")
224                 protected void initChannel(final SocketChannel channel) {
225                     initializer.initializeChannel(channel,
226                             new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
227                 }
228             };
229         }
230     }
231 }