1a465d888765608fe7a4f8823b6ad8d430ad7ff0
[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 com.google.common.annotations.Beta;
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.channel.ChannelPipeline;
13 import java.io.IOException;
14 import org.opendaylight.netconf.shaded.sshd.client.ClientFactoryManager;
15 import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
16 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
17 import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
18
19 /**
20  * A {@link ClientSessionImpl} which additionally allows creation of NETCONF subsystem channel, which is routed to
21  * a particular {@link ChannelHandlerContext}.
22  */
23 @Beta
24 public final class NetconfClientSessionImpl extends ClientSessionImpl implements NettyAwareClientSession {
25     public NetconfClientSessionImpl(final ClientFactoryManager client, final IoSession ioSession) throws Exception {
26         super(client, ioSession);
27     }
28
29     @Override
30     public NettyAwareChannelSubsystem createSubsystemChannel(final String subsystem, final ChannelHandlerContext ctx)
31             throws IOException {
32         return registerSubsystem(new NettyAwareChannelSubsystem(subsystem, ctx));
33     }
34
35     @Override
36     public NettyPipelineAwareChannelSubsystem createSubsystemChannel(final String subsystem,
37             final ChannelPipeline pipeline) throws IOException {
38         return registerSubsystem(new NettyPipelineAwareChannelSubsystem(subsystem, pipeline));
39     }
40
41     private <T extends ChannelSubsystem> T registerSubsystem(final T subsystem) throws IOException {
42         final var service = getConnectionService();
43         final var id = service.registerChannel(subsystem);
44         if (log.isDebugEnabled()) {
45             log.debug("createSubsystemChannel({})[{}] created id={}", this, subsystem.getSubsystem(), id);
46         }
47         return subsystem;
48     }
49 }