2 * Copyright (c) 2015 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.protocol;
10 import static java.util.Objects.requireNonNull;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.ChannelHandler;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.socket.SocketChannel;
18 import io.netty.util.concurrent.DefaultPromise;
19 import io.netty.util.concurrent.EventExecutor;
20 import java.net.InetSocketAddress;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
24 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class BGPReconnectPromise<S extends BGPSession> extends DefaultPromise<Void> {
29 private static final Logger LOG = LoggerFactory.getLogger(BGPReconnectPromise.class);
31 private final InetSocketAddress address;
32 private final int retryTimer;
33 private final Bootstrap bootstrap;
34 private final BGPPeerRegistry peerRegistry;
35 private final ChannelPipelineInitializer<S> initializer;
36 private BGPProtocolSessionPromise<S> pending;
38 public BGPReconnectPromise(@Nonnull final EventExecutor executor, @Nonnull final InetSocketAddress address,
39 final int retryTimer, @Nonnull final Bootstrap bootstrap, @Nonnull final BGPPeerRegistry peerRegistry,
40 @Nonnull final ChannelPipelineInitializer<S> initializer) {
42 this.bootstrap = bootstrap;
43 this.initializer = requireNonNull(initializer);
44 this.address = requireNonNull(address);
45 this.retryTimer = retryTimer;
46 this.peerRegistry = requireNonNull(peerRegistry);
49 public synchronized void connect() {
50 if (this.pending != null) {
51 this.pending.cancel(true);
54 // Set up a client with pre-configured bootstrap, but add a closed channel handler
55 // into the pipeline to support reconnect attempts
56 this.pending = connectSessionPromise(this.address, this.retryTimer, this.bootstrap, this.peerRegistry,
57 (channel, promise) -> {
58 this.initializer.initializeChannel(channel, promise);
59 // add closed channel handler
60 // This handler has to be added as last channel handler and the channel inactive event has to be
62 // Handlers in front of it can react to channelInactive event, but have to forward the event or
63 // the reconnect will not work. This handler is last so all handlers in front of it can handle
64 // channel inactive (to e.g. resource cleanup) before a new connection is started
65 channel.pipeline().addLast(new ClosedChannelHandler(this));
68 this.pending.addListener(future -> {
69 if (!future.isSuccess() && !this.isDone()) {
70 this.setFailure(future.cause());
75 private BGPProtocolSessionPromise<S> connectSessionPromise(final InetSocketAddress address, final int retryTimer,
76 final Bootstrap bootstrap, final BGPPeerRegistry peerRegistry,
77 final ChannelPipelineInitializer<S> initializer) {
78 final BGPProtocolSessionPromise<S> sessionPromise = new BGPProtocolSessionPromise<>(address, retryTimer,
79 bootstrap, peerRegistry);
80 final ChannelHandler chInit = new ChannelInitializer<SocketChannel>() {
82 protected void initChannel(final SocketChannel channel) {
83 initializer.initializeChannel(channel, sessionPromise);
87 bootstrap.handler(chInit);
88 sessionPromise.connect();
89 LOG.debug("Client created.");
90 return sessionPromise;
94 * @return true if initial connection was established successfully, false if initial connection failed due
95 * to e.g. Connection refused, Negotiation failed
97 private synchronized boolean isInitialConnectFinished() {
98 requireNonNull(this.pending);
99 return this.pending.isDone() && this.pending.isSuccess();
102 private synchronized void reconnect() {
103 requireNonNull(this.pending);
104 this.pending.reconnect();
108 public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
109 if (super.cancel(mayInterruptIfRunning)) {
110 requireNonNull(this.pending);
111 this.pending.cancel(mayInterruptIfRunning);
118 * Channel handler that responds to channelInactive event and reconnects the session.
119 * Only if the promise was not canceled.
121 private static final class ClosedChannelHandler extends ChannelInboundHandlerAdapter {
122 private final BGPReconnectPromise<?> promise;
124 ClosedChannelHandler(final BGPReconnectPromise<?> promise) {
125 this.promise = promise;
129 public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
130 // This is the ultimate channel inactive handler, not forwarding
131 if (this.promise.isCancelled()) {
135 if (!this.promise.isInitialConnectFinished()) {
136 LOG.debug("Connection to {} was dropped during negotiation, reattempting", this.promise.address);
137 this.promise.reconnect();
141 LOG.debug("Reconnecting after connection to {} was dropped", this.promise.address);
142 this.promise.connect();