0c69d6930eae1e9139483cf91f0a7c824731e365
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / server / ssh / CallHomeSshSessionContextManager.java
1 /*
2  * Copyright (c) 2023 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.callhome.server.ssh;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.channel.Channel;
14 import java.net.SocketAddress;
15 import java.util.Map;
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;
20
21 public class CallHomeSshSessionContextManager extends AbstractCallHomeSessionContextManager<CallHomeSshSessionContext> {
22
23     @Override
24     public CallHomeSshSessionContext findByChannel(final Channel channel) {
25         requireNonNull(channel);
26         return channel.isOpen() ? findByAddress(channel.remoteAddress()) : null;
27     }
28
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);
33     }
34
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);
40     }
41
42     /**
43      * Builds {@link CallHomeSshSessionContext} based on SSH {@link ClientSession}.
44      *
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.
49      *
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
53      */
54     public @Nullable CallHomeSshSessionContext createContext(final String id, final ClientSession clientSession) {
55         return new CallHomeSshSessionContext(id, clientSession.getRemoteAddress(), clientSession,
56             new SimpleNetconfClientSessionListener(), SettableFuture.create());
57     }
58 }