2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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.socket.SocketChannel;
21 import io.netty.channel.socket.nio.NioServerSocketChannel;
22 import io.netty.channel.socket.nio.NioSocketChannel;
23 import io.netty.util.concurrent.DefaultPromise;
24 import io.netty.util.concurrent.EventExecutor;
25 import io.netty.util.concurrent.Future;
26 import io.netty.util.concurrent.GlobalEventExecutor;
27 import io.netty.util.concurrent.Promise;
28 import java.net.InetSocketAddress;
29 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
30 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
31 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
35 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
36 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
37 import org.opendaylight.protocol.framework.ReconnectStrategy;
38 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
39 import org.opendaylight.tcpmd5.api.KeyMapping;
40 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
41 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
42 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Implementation of BGPDispatcher.
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;
55 private final MD5ServerChannelFactory<?> serverChannelFactory;
56 private final MD5ChannelFactory<?> channelFactory;
57 private final BGPHandlerFactory handlerFactory;
58 private final EventLoopGroup bossGroup;
59 private final EventLoopGroup workerGroup;
60 private final EventExecutor executor;
61 private Optional<KeyMapping> keys;
63 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
64 this(messageRegistry, bossGroup, workerGroup, null, null);
67 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
68 this.bossGroup = Preconditions.checkNotNull(bossGroup);
69 this.workerGroup = Preconditions.checkNotNull(workerGroup);
70 this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
71 this.handlerFactory = new BGPHandlerFactory(messageRegistry);
72 this.channelFactory = cf;
73 this.serverChannelFactory = scf;
74 this.keys = Optional.absent();
78 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
79 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(listener);
80 final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(BGPDispatcherImpl.this.handlerFactory, snf);
82 final Bootstrap bootstrap = createClientBootStrap();
83 final BGPProtocolSessionPromise sessionPromise = new BGPProtocolSessionPromise(this.executor, address, strategy, bootstrap);
84 bootstrap.handler(BGPChannel.createChannelInitializer(initializer, sessionPromise));
85 sessionPromise.connect();
86 LOG.debug("Client created.");
87 return sessionPromise;
90 protected Bootstrap createClientBootStrap() {
91 final Bootstrap bootstrap = new Bootstrap();
92 if (this.keys.isPresent()) {
93 if (this.channelFactory == null) {
94 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
96 bootstrap.channelFactory(this.channelFactory);
97 bootstrap.option(MD5ChannelOption.TCP_MD5SIG, this.keys.get());
99 bootstrap.channel(NioSocketChannel.class);
102 // Make sure we are doing round-robin processing
103 bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
104 bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
105 bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
106 bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
108 if (bootstrap.group() == null) {
109 bootstrap.group(this.workerGroup);
116 public void close() {
120 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPPeerRegistry peerRegistry,
121 final ReconnectStrategyFactory connectStrategyFactory, final Optional<KeyMapping> keys) {
122 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(peerRegistry);
124 final Bootstrap bootstrap = createClientBootStrap();
125 final BGPReconnectPromise reconnectPromise = new BGPReconnectPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE, address,
126 connectStrategyFactory, bootstrap, BGPChannel.createChannelPipelineInitializer(BGPDispatcherImpl.this.handlerFactory, snf));
127 reconnectPromise.connect();
128 this.keys = Optional.absent();
129 return reconnectPromise;
133 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address) {
134 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(registry);
135 final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(BGPDispatcherImpl.this.handlerFactory, snf);
136 final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
137 final ChannelFuture channelFuture = serverBootstrap.bind(address);
138 LOG.debug("Initiated server {} at {}.", channelFuture, address);
139 return channelFuture;
142 private ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
143 final ServerBootstrap serverBootstrap = new ServerBootstrap();
144 serverBootstrap.childHandler(BGPChannel.createChannelInitializer(initializer, new DefaultPromise(BGPDispatcherImpl.this.executor)));
145 serverBootstrap.option(ChannelOption.SO_BACKLOG, Integer.valueOf(SOCKET_BACKLOG_SIZE));
146 serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
147 serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
148 serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
149 if (this.keys.isPresent()) {
150 if (this.serverChannelFactory == null) {
151 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
153 serverBootstrap.channelFactory(this.serverChannelFactory);
154 serverBootstrap.option(MD5ChannelOption.TCP_MD5SIG, this.keys.get());
156 serverBootstrap.channel(NioServerSocketChannel.class);
159 // Make sure we are doing round-robin processing
160 serverBootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
162 if (serverBootstrap.group() == null) {
163 serverBootstrap.group(this.bossGroup, this.workerGroup);
165 return serverBootstrap;
168 private static final class BGPChannel {
169 private static final String NEGOTIATOR = "negotiator";
171 private BGPChannel() {
175 public static <S extends BGPSession, T extends BGPSessionNegotiatorFactory> ChannelPipelineInitializer
176 createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
177 return new ChannelPipelineInitializer<S>() {
179 public void initializeChannel(final SocketChannel channel, final Promise<S> promise) {
180 channel.pipeline().addLast(hf.getDecoders());
181 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
182 channel.pipeline().addLast(hf.getEncoders());
187 public static <S extends BGPSession> ChannelHandler createChannelInitializer(final ChannelPipelineInitializer initializer, final Promise<S> promise) {
188 return new ChannelInitializer<SocketChannel>() {
190 protected void initChannel(final SocketChannel channel) {
191 initializer.initializeChannel(channel, promise);