2 * Copyright (c) 2023 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.topology.callhome;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.channel.Channel;
14 import java.net.SocketAddress;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener;
18 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
20 public class CallHomeSshSessionContextManager extends AbstractCallHomeSessionContextManager<CallHomeSshSessionContext> {
23 public CallHomeSshSessionContext findByChannel(final Channel channel) {
24 requireNonNull(channel);
25 return channel.isOpen() ? findByAddress(channel.remoteAddress()) : null;
28 private CallHomeSshSessionContext findByAddress(final SocketAddress address) {
29 return contexts.entrySet().stream()
30 .filter(entry -> address.equals(entry.getValue().remoteAddress()))
31 .findFirst().map(Map.Entry::getValue).orElse(null);
34 public CallHomeSshSessionContext findBySshSession(final ClientSession clientSession) {
35 requireNonNull(clientSession);
36 return contexts.entrySet().stream()
37 .filter(entry -> clientSession == entry.getValue().sshSession())
38 .findFirst().map(Map.Entry::getValue).orElse(null);
42 * Builds {@link CallHomeSshSessionContext} based on SSH {@link ClientSession}.
44 * <p> The method expected to be overridden in order to inject
45 * {@link org.opendaylight.netconf.client.NetconfClientSessionListener NetconfClientSessionListener} and/or
46 * {@link SettableFuture} of a {@link org.opendaylight.netconf.client.NetconfClientSession NetconfClientSession}
47 * to be established on current connection.
49 * @param id unique client (device) identifier
50 * @param clientSession SSH session instance
51 * @return created object or {@code null} if it cannot be created for some reason
53 public @Nullable CallHomeSshSessionContext createContext(final String id, final ClientSession clientSession) {
54 return new CallHomeSshSessionContext(id, clientSession.getRemoteAddress(), clientSession,
55 new SimpleNetconfClientSessionListener(), SettableFuture.create());