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.netconf.nettyutil;
10 import static java.util.Objects.requireNonNull;
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;
25 final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromise<S> {
26 private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionPromise.class);
28 private final Bootstrap bootstrap;
29 private InetSocketAddress address;
32 private ChannelFuture pending;
34 NetconfSessionPromise(final EventExecutor executor, final InetSocketAddress address, final Bootstrap bootstrap) {
36 this.address = requireNonNull(address);
37 this.bootstrap = requireNonNull(bootstrap);
40 @SuppressWarnings("checkstyle:illegalCatch")
41 synchronized void connect() {
42 final ChannelFuture connectFuture;
44 if (address.isUnresolved()) {
45 address = new InetSocketAddress(address.getHostName(), address.getPort());
47 connectFuture = bootstrap.connect(address);
48 } catch (final Exception e) {
49 LOG.info("Failed to connect to {}", address, e);
54 pending = connectFuture;
55 // Add listener that attempts reconnect by invoking this method again.
56 connectFuture.addListener((ChannelFutureListener) this::channelConnectComplete);
60 public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
61 if (super.cancel(mayInterruptIfRunning)) {
62 pending.cancel(mayInterruptIfRunning);
70 public synchronized Promise<S> setSuccess(final S result) {
71 LOG.debug("Promise {} completed", this);
72 return super.setSuccess(result);
75 // Triggered when a connection attempt is resolved.
76 private synchronized void channelConnectComplete(final ChannelFuture cf) {
77 LOG.debug("Promise {} connection resolved", this);
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
90 LOG.debug("Closing channel for cancelled promise {}", this);
97 LOG.debug("Promise {} connection successful", this);
101 LOG.debug("Attempt to connect to {} failed", address, cf.cause());
102 setFailure(cf.cause());