NetconfClientFactoryImpl should propagate stack failure
[netconf.git] / protocol / netconf-client / src / main / java / org / opendaylight / netconf / client / ClientTransportChannelListener.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.client;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.FutureListener;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.netconf.transport.api.TransportChannel;
19 import org.opendaylight.netconf.transport.api.TransportChannelListener;
20 import org.opendaylight.netconf.transport.api.TransportStack;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 final class ClientTransportChannelListener implements TransportChannelListener, FutureListener<NetconfClientSession>,
25         FutureCallback<TransportStack> {
26     private static final Logger LOG = LoggerFactory.getLogger(ClientTransportChannelListener.class);
27
28     private final @NonNull SettableFuture<NetconfClientSession> sessionFuture = SettableFuture.create();
29     private final ClientChannelInitializer initializer;
30
31     ClientTransportChannelListener(final ClientChannelInitializer initializer) {
32         this.initializer = requireNonNull(initializer);
33     }
34
35     @NonNull ListenableFuture<NetconfClientSession> sessionFuture() {
36         return sessionFuture;
37     }
38
39     @Override
40     public void onTransportChannelEstablished(final TransportChannel channel) {
41         LOG.debug("Initializing established transport {}", channel);
42         final var nettyChannel = channel.channel();
43         final var promise = nettyChannel.eventLoop().<NetconfClientSession>newPromise();
44         initializer.initialize(nettyChannel, promise);
45         promise.addListener(this);
46     }
47
48     @Override
49     public void onTransportChannelFailed(final Throwable cause) {
50         sessionFuture.setException(cause);
51     }
52
53     @Override
54     public void operationComplete(final Future<NetconfClientSession> future) throws Exception {
55         final var cause = future.cause();
56         if (cause != null) {
57             sessionFuture.setException(cause);
58         } else {
59             sessionFuture.set(future.getNow());
60         }
61     }
62
63     @Override
64     public void onSuccess(final TransportStack result) {
65         // No-op
66     }
67
68     @Override
69     public void onFailure(final Throwable cause) {
70         onTransportChannelFailed(cause);
71     }
72 }