178be8bfe1f3ef13ec748eb6c2c39aad2af929f7
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / NetconfSessionPromise.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.netconf.nettyutil;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelFutureListener;
16 import io.netty.util.concurrent.DefaultPromise;
17 import io.netty.util.concurrent.EventExecutor;
18 import io.netty.util.concurrent.Future;
19 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
21 import org.checkerframework.checker.lock.qual.GuardedBy;
22 import org.opendaylight.netconf.api.NetconfSession;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Deprecated
27 final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromise<S> {
28     private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionPromise.class);
29     private final ReconnectStrategy strategy;
30     private InetSocketAddress address;
31     private final Bootstrap bootstrap;
32
33     @GuardedBy("this")
34     private Future<?> pending;
35
36     NetconfSessionPromise(final EventExecutor executor, final InetSocketAddress address,
37             final ReconnectStrategy strategy, final Bootstrap bootstrap) {
38         super(executor);
39         this.strategy = requireNonNull(strategy);
40         this.address = requireNonNull(address);
41         this.bootstrap = requireNonNull(bootstrap);
42     }
43
44     @SuppressWarnings("checkstyle:illegalCatch")
45     synchronized void connect() {
46         final int timeout;
47         try {
48             timeout = strategy.getConnectTimeout();
49         } catch (Exception e) {
50             LOG.info("Connection to {} aborted due to strategy decision", address, e);
51             setFailure(e);
52             return;
53         }
54
55         LOG.debug("Promise {} attempting connect for {}ms", this, timeout);
56
57         try {
58             if (address.isUnresolved()) {
59                 address = new InetSocketAddress(address.getHostName(), address.getPort());
60             }
61             final ChannelFuture connectFuture = bootstrap.connect(address);
62             pending = connectFuture;
63             // Add listener that attempts reconnect by invoking this method again.
64             connectFuture.addListener((ChannelFutureListener) this::channelConnectComplete);
65         } catch (final Exception e) {
66             LOG.info("Failed to connect to {}", address, e);
67             setFailure(e);
68         }
69     }
70
71     @Override
72     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
73         if (super.cancel(mayInterruptIfRunning)) {
74             pending.cancel(mayInterruptIfRunning);
75             return true;
76         }
77
78         return false;
79     }
80
81     @Override
82     public synchronized Promise<S> setSuccess(final S result) {
83         LOG.debug("Promise {} completed", this);
84         strategy.reconnectSuccessful();
85         return super.setSuccess(result);
86     }
87
88     // Triggered when a connection attempt is resolved.
89     private synchronized void channelConnectComplete(final ChannelFuture cf) {
90         LOG.debug("Promise {} connection resolved", this);
91         verify(pending == cf, "Completed channel future %s while pending %s", cf, pending);
92
93         /*
94          * The promise we gave out could have been cancelled,
95          * which cascades to the connect getting cancelled,
96          * but there is a slight race window, where the connect
97          * is already resolved, but the listener has not yet
98          * been notified -- cancellation at that point won't
99          * stop the notification arriving, so we have to close
100          * the race here.
101          */
102         if (isCancelled()) {
103             if (cf.isSuccess()) {
104                 LOG.debug("Closing channel for cancelled promise {}", this);
105                 cf.channel().close();
106             }
107             return;
108         }
109
110         if (cf.isSuccess()) {
111             LOG.debug("Promise {} connection successful", this);
112             return;
113         }
114
115         LOG.debug("Attempt to connect to {} failed", address, cf.cause());
116
117         final Future<Void> rf = strategy.scheduleReconnect(cf.cause());
118         pending = rf;
119         rf.addListener(this::reconnectFutureComplete);
120     }
121
122     // Triggered when a connection attempt is to be made.
123     private synchronized void reconnectFutureComplete(final Future<?> sf) {
124         LOG.debug("Promise {} strategy triggered reconnect", this);
125         verify(pending == sf, "Completed strategy future %s while pending %s", sf, pending);
126
127         /*
128          * The promise we gave out could have been cancelled,
129          * which cascades to the reconnect attempt getting
130          * cancelled, but there is a slight race window, where
131          * the reconnect attempt is already enqueued, but the
132          * listener has not yet been notified -- if cancellation
133          * happens at that point, we need to catch it here.
134          */
135         if (!isCancelled()) {
136             if (sf.isSuccess()) {
137                 connect();
138             } else {
139                 setFailure(sf.cause());
140             }
141         }
142     }
143 }