2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.client;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.FutureListener;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.netconf.transport.api.TransportChannel;
18 import org.opendaylight.netconf.transport.api.TransportChannelListener;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 final class ClientTransportChannelListener implements TransportChannelListener, FutureListener<NetconfClientSession> {
23 private static final Logger LOG = LoggerFactory.getLogger(ClientTransportChannelListener.class);
25 private final @NonNull SettableFuture<NetconfClientSession> sessionFuture = SettableFuture.create();
26 private final ClientChannelInitializer initializer;
28 ClientTransportChannelListener(final ClientChannelInitializer initializer) {
29 this.initializer = requireNonNull(initializer);
32 @NonNull ListenableFuture<NetconfClientSession> sessionFuture() {
37 public void onTransportChannelEstablished(final TransportChannel channel) {
38 LOG.debug("Initializing established transport {}", channel);
39 final var nettyChannel = channel.channel();
40 final var promise = nettyChannel.eventLoop().<NetconfClientSession>newPromise();
41 initializer.initialize(nettyChannel, promise);
42 promise.addListener(this);
46 public void onTransportChannelFailed(final Throwable cause) {
47 sessionFuture.setException(cause);
51 public void operationComplete(final Future<NetconfClientSession> future) throws Exception {
52 final var cause = future.cause();
54 sessionFuture.setException(cause);
56 sessionFuture.set(future.getNow());