Rework NETCONF client reconnection
[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 java.util.Objects.requireNonNull;
11
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelFutureListener;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.EventExecutor;
17 import io.netty.util.concurrent.Promise;
18 import java.net.InetSocketAddress;
19 import org.checkerframework.checker.lock.qual.GuardedBy;
20 import org.opendaylight.netconf.api.NetconfSession;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @Deprecated
25 final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromise<S> {
26     private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionPromise.class);
27
28     private final Bootstrap bootstrap;
29     private InetSocketAddress address;
30
31     @GuardedBy("this")
32     private ChannelFuture pending;
33
34     NetconfSessionPromise(final EventExecutor executor, final InetSocketAddress address, final Bootstrap bootstrap) {
35         super(executor);
36         this.address = requireNonNull(address);
37         this.bootstrap = requireNonNull(bootstrap);
38     }
39
40     @SuppressWarnings("checkstyle:illegalCatch")
41     synchronized void connect() {
42         final ChannelFuture connectFuture;
43         try {
44             if (address.isUnresolved()) {
45                 address = new InetSocketAddress(address.getHostName(), address.getPort());
46             }
47             connectFuture = bootstrap.connect(address);
48         } catch (final Exception e) {
49             LOG.info("Failed to connect to {}", address, e);
50             setFailure(e);
51             return;
52         }
53
54         pending = connectFuture;
55         // Add listener that attempts reconnect by invoking this method again.
56         connectFuture.addListener((ChannelFutureListener) this::channelConnectComplete);
57     }
58
59     @Override
60     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
61         if (super.cancel(mayInterruptIfRunning)) {
62             pending.cancel(mayInterruptIfRunning);
63             return true;
64         }
65
66         return false;
67     }
68
69     @Override
70     public synchronized Promise<S> setSuccess(final S result) {
71         LOG.debug("Promise {} completed", this);
72         return super.setSuccess(result);
73     }
74
75     // Triggered when a connection attempt is resolved.
76     private synchronized void channelConnectComplete(final ChannelFuture cf) {
77         LOG.debug("Promise {} connection resolved", this);
78
79         /*
80          * The promise we gave out could have been cancelled,
81          * which cascades to the connect getting cancelled,
82          * but there is a slight race window, where the connect
83          * is already resolved, but the listener has not yet
84          * been notified -- cancellation at that point won't
85          * stop the notification arriving, so we have to close
86          * the race here.
87          */
88         if (isCancelled()) {
89             if (cf.isSuccess()) {
90                 LOG.debug("Closing channel for cancelled promise {}", this);
91                 cf.channel().close();
92             }
93             return;
94         }
95
96         if (cf.isSuccess()) {
97             LOG.debug("Promise {} connection successful", this);
98             return;
99         }
100
101         LOG.debug("Attempt to connect to {} failed", address, cf.cause());
102         setFailure(cf.cause());
103     }
104 }