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.callhome.server.ssh;
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.callhome.server.AbstractCallHomeSessionContextManager;
18 import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener;
19 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
21 public class CallHomeSshSessionContextManager extends AbstractCallHomeSessionContextManager<CallHomeSshSessionContext> {
24 public CallHomeSshSessionContext findByChannel(final Channel channel) {
25 requireNonNull(channel);
26 return channel.isOpen() ? findByAddress(channel.remoteAddress()) : null;
29 private CallHomeSshSessionContext findByAddress(final SocketAddress address) {
30 return contexts.entrySet().stream()
31 .filter(entry -> address.equals(entry.getValue().remoteAddress()))
32 .findFirst().map(Map.Entry::getValue).orElse(null);
35 public CallHomeSshSessionContext findBySshSession(final ClientSession clientSession) {
36 requireNonNull(clientSession);
37 return contexts.entrySet().stream()
38 .filter(entry -> clientSession == entry.getValue().sshSession())
39 .findFirst().map(Map.Entry::getValue).orElse(null);
43 * Builds {@link CallHomeSshSessionContext} based on SSH {@link ClientSession}.
45 * <p> The method expected to be overridden in order to inject
46 * {@link org.opendaylight.netconf.client.NetconfClientSessionListener NetconfClientSessionListener} and/or
47 * {@link SettableFuture} of a {@link org.opendaylight.netconf.client.NetconfClientSession NetconfClientSession}
48 * to be established on current connection.
50 * @param id unique client (device) identifier
51 * @param clientSession SSH session instance
52 * @return created object or {@code null} if it cannot be created for some reason
54 public @Nullable CallHomeSshSessionContext createContext(final String id, final ClientSession clientSession) {
55 return new CallHomeSshSessionContext(id, clientSession.getRemoteAddress(), clientSession,
56 new SimpleNetconfClientSessionListener(), SettableFuture.create());