Rework SslHandlerFactory
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / topology / callhome / 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.topology.callhome;
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.client.SimpleNetconfClientSessionListener;
18 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
19
20 public class CallHomeSshSessionContextManager extends AbstractCallHomeSessionContextManager<CallHomeSshSessionContext> {
21
22     @Override
23     public CallHomeSshSessionContext findByChannel(final Channel channel) {
24         requireNonNull(channel);
25         return channel.isOpen() ? findByAddress(channel.remoteAddress()) : null;
26     }
27
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);
32     }
33
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);
39     }
40
41     /**
42      * Builds {@link CallHomeSshSessionContext} based on SSH {@link ClientSession}.
43      *
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.
48      *
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
52      */
53     public @Nullable CallHomeSshSessionContext createContext(final String id, final ClientSession clientSession) {
54         return new CallHomeSshSessionContext(id, clientSession.getRemoteAddress(), clientSession,
55             new SimpleNetconfClientSessionListener(), SettableFuture.create());
56     }
57 }