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.server;
10 import static java.util.Objects.requireNonNull;
12 import io.netty.buffer.Unpooled;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
15 import io.netty.channel.embedded.EmbeddedChannel;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import java.net.InetSocketAddress;
18 import java.nio.charset.StandardCharsets;
19 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
20 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
21 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
22 import org.opendaylight.netconf.shaded.sshd.server.channel.ChannelDataReceiver;
23 import org.opendaylight.netconf.shaded.sshd.server.channel.ChannelSession;
24 import org.opendaylight.netconf.shaded.sshd.server.channel.ChannelSessionAware;
25 import org.opendaylight.netconf.shaded.sshd.server.command.AbstractCommandSupport;
26 import org.opendaylight.netconf.shaded.sshd.server.command.AsyncCommand;
27 import org.opendaylight.netconf.transport.ssh.OutboundChannelHandler;
29 final class NetconfSubsystem extends AbstractCommandSupport
30 implements AsyncCommand, ChannelSessionAware, ChannelDataReceiver {
31 // FIXME: NETCONF-1106: do not use EmbeddedChannel here!
32 private final EmbeddedChannel innerChannel = new EmbeddedChannel();
33 private final ServerChannelInitializer channelInitializer;
35 NetconfSubsystem(final String name, final ServerChannelInitializer channelInitializer) {
37 this.channelInitializer = requireNonNull(channelInitializer);
46 public void setIoInputStream(final IoInputStream in) {
51 public void setIoErrorStream(final IoOutputStream err) {
56 public void setIoOutputStream(final IoOutputStream out) {
58 * While NETCONF protocol handlers are designed to operate over Netty channel, the inner channel is used to
59 * serve NETCONF over SSH.
61 // outbound packet handler, adding fist means it will be invoked last because of flow direction
62 innerChannel.pipeline().addFirst(new OutboundChannelHandler(out));
64 // inner channel termination handler
65 innerChannel.pipeline().addLast(
66 new ChannelInboundHandlerAdapter() {
68 public void channelInactive(final ChannelHandlerContext ctx) {
73 // NETCONF protocol handlers
74 channelInitializer.initialize(innerChannel, GlobalEventExecutor.INSTANCE.newPromise());
75 // trigger negotiation flow
76 innerChannel.pipeline().fireChannelActive();
77 // set additional info for upcoming netconf session
78 innerChannel.writeInbound(Unpooled.wrappedBuffer(getHelloAdditionalMessageBytes()));
82 public void setChannelSession(final ChannelSession channelSession) {
84 * Inbound packets handler
85 * NOTE: The channel data receiver require to be set within current method, so it could be handled
86 * with subsequent logic of ChannelSession#prepareChannelCommand() where this method is executed from.
88 channelSession.setDataReceiver(this);
92 public int data(final ChannelSession channel, final byte[] buf, final int start, final int len) {
93 // Do not propagate empty invocations
95 innerChannel.writeInbound(Unpooled.copiedBuffer(buf, start, len));
101 public void close() {
102 innerChannel.close();
106 protected void onExit(final int exitValue, final String exitMessage) {
107 super.onExit(exitValue, exitMessage);
108 innerChannel.close();
111 private byte[] getHelloAdditionalMessageBytes() {
112 final var session = getServerSession();
113 final var address = (InetSocketAddress) session.getClientAddress();
114 return new NetconfHelloMessageAdditionalHeader(session.getUsername(), address.getAddress().getHostAddress(),
115 String.valueOf(address.getPort()), "ssh", "client")
116 .toFormattedString().getBytes(StandardCharsets.UTF_8);