Fix checkstyle complains
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / protocol / BGPReconnectPromise.java
1 /*
2  * Copyright (c) 2015 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.protocol;
9
10 import static java.util.Objects.requireNonNull;
11
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;
27
28 public class BGPReconnectPromise<S extends BGPSession> extends DefaultPromise<Void> {
29     private static final Logger LOG = LoggerFactory.getLogger(BGPReconnectPromise.class);
30
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;
37
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) {
41         super(executor);
42         this.bootstrap = bootstrap;
43         this.initializer = requireNonNull(initializer);
44         this.address = requireNonNull(address);
45         this.retryTimer = retryTimer;
46         this.peerRegistry = requireNonNull(peerRegistry);
47     }
48
49     public synchronized void connect() {
50         if (this.pending != null) {
51             this.pending.cancel(true);
52         }
53
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
61                     // caught by it
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));
66                 });
67
68         this.pending.addListener(future -> {
69             if (!future.isSuccess() && !this.isDone()) {
70                 this.setFailure(future.cause());
71             }
72         });
73     }
74
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>() {
81             @Override
82             protected void initChannel(final SocketChannel channel) {
83                 initializer.initializeChannel(channel, sessionPromise);
84             }
85         };
86
87         bootstrap.handler(chInit);
88         sessionPromise.connect();
89         LOG.debug("Client created.");
90         return sessionPromise;
91     }
92
93     /**
94      * @return true if initial connection was established successfully, false if initial connection failed due
95      *         to e.g. Connection refused, Negotiation failed
96      */
97     private  synchronized boolean isInitialConnectFinished() {
98         requireNonNull(this.pending);
99         return this.pending.isDone() && this.pending.isSuccess();
100     }
101
102     private synchronized void reconnect() {
103         requireNonNull(this.pending);
104         this.pending.reconnect();
105     }
106
107     @Override
108     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
109         if (super.cancel(mayInterruptIfRunning)) {
110             requireNonNull(this.pending);
111             this.pending.cancel(mayInterruptIfRunning);
112             return true;
113         }
114         return false;
115     }
116
117     /**
118      * Channel handler that responds to channelInactive event and reconnects the session.
119      * Only if the promise was not canceled.
120      */
121     private static final class ClosedChannelHandler extends ChannelInboundHandlerAdapter {
122         private final BGPReconnectPromise<?> promise;
123
124         ClosedChannelHandler(final BGPReconnectPromise<?> promise) {
125             this.promise = promise;
126         }
127
128         @Override
129         public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
130             // This is the ultimate channel inactive handler, not forwarding
131             if (this.promise.isCancelled()) {
132                 return;
133             }
134
135             if (!this.promise.isInitialConnectFinished()) {
136                 LOG.debug("Connection to {} was dropped during negotiation, reattempting", this.promise.address);
137                 this.promise.reconnect();
138                 return;
139             }
140
141             LOG.debug("Reconnecting after connection to {} was dropped", this.promise.address);
142             this.promise.connect();
143         }
144     }
145 }