26e555dc095607640dfbc8ae1a631209b102df4f
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / NetconfClientSessionImpl.java
1 /*
2  * Copyright (c) 2019 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.nettyutil.handler.ssh.client;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelPipeline;
14 import java.io.IOException;
15 import org.opendaylight.netconf.shaded.sshd.client.ClientFactoryManager;
16 import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
17 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
18 import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
19
20 /**
21  * A {@link ClientSessionImpl} which additionally allows creation of NETCONF subsystem channel, which is routed to
22  * a particular {@link ChannelHandlerContext}.
23  */
24 @Deprecated(since = "7.0.0", forRemoval = true)
25 public final class NetconfClientSessionImpl extends ClientSessionImpl implements NettyAwareClientSession {
26     public NetconfClientSessionImpl(final ClientFactoryManager client, final IoSession ioSession) throws Exception {
27         super(client, ioSession);
28     }
29
30     @Override
31     public ChannelSubsystem createSubsystemChannel(final String subsystem, final ChannelHandlerContext ctx)
32             throws IOException {
33         requireNonNull(ctx);
34         return registerSubsystem(new NettyChannelSubsystem(subsystem) {
35             @Override
36             ChannelHandlerContext context() {
37                 return ctx;
38             }
39         });
40     }
41
42     @Override
43     public ChannelSubsystem createSubsystemChannel(final String subsystem,
44             final ChannelPipeline pipeline) throws IOException {
45         requireNonNull(pipeline);
46         return registerSubsystem(new NettyChannelSubsystem(subsystem) {
47             @Override
48             ChannelHandlerContext context() {
49                 return pipeline.firstContext();
50             }
51         });
52     }
53
54     private ChannelSubsystem registerSubsystem(final ChannelSubsystem subsystem) throws IOException {
55         final var service = getConnectionService();
56         final var id = service.registerChannel(subsystem);
57         if (log.isDebugEnabled()) {
58             log.debug("createSubsystemChannel({})[{}] created id={}", this, subsystem.getSubsystem(), id);
59         }
60         return subsystem;
61     }
62 }