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;
28 final class NetconfSubsystem extends AbstractCommandSupport
29 implements AsyncCommand, ChannelSessionAware, ChannelDataReceiver {
30 // FIXME: NETCONF-1106: do not use EmbeddedChannel here!
31 private final EmbeddedChannel innerChannel = new EmbeddedChannel();
32 private final ServerChannelInitializer channelInitializer;
34 NetconfSubsystem(final String name, final ServerChannelInitializer channelInitializer) {
36 this.channelInitializer = requireNonNull(channelInitializer);
45 public void setIoInputStream(final IoInputStream in) {
50 public void setIoErrorStream(final IoOutputStream err) {
55 public void setIoOutputStream(final IoOutputStream out) {
57 * While NETCONF protocol handlers are designed to operate over Netty channel, the inner channel is used to
58 * serve NETCONF over SSH.
60 // outbound packet handler, adding fist means it will be invoked last because of flow direction
61 innerChannel.pipeline().addFirst(new NetconfSubsystemOutboundChannelHandler(out));
63 // inner channel termination handler
64 innerChannel.pipeline().addLast(
65 new ChannelInboundHandlerAdapter() {
67 public void channelInactive(final ChannelHandlerContext ctx) {
72 // NETCONF protocol handlers
73 channelInitializer.initialize(innerChannel, GlobalEventExecutor.INSTANCE.newPromise());
74 // trigger negotiation flow
75 innerChannel.pipeline().fireChannelActive();
76 // set additional info for upcoming netconf session
77 innerChannel.writeInbound(Unpooled.wrappedBuffer(getHelloAdditionalMessageBytes()));
81 public void setChannelSession(final ChannelSession channelSession) {
83 * Inbound packets handler
84 * NOTE: The channel data receiver require to be set within current method, so it could be handled
85 * with subsequent logic of ChannelSession#prepareChannelCommand() where this method is executed from.
87 channelSession.setDataReceiver(this);
91 public int data(final ChannelSession channel, final byte[] buf, final int start, final int len) {
92 // Do not propagate empty invocations
94 innerChannel.writeInbound(Unpooled.copiedBuffer(buf, start, len));
100 public void close() {
101 innerChannel.close();
105 protected void onExit(final int exitValue, final String exitMessage) {
106 super.onExit(exitValue, exitMessage);
107 innerChannel.close();
110 private byte[] getHelloAdditionalMessageBytes() {
111 final var session = getServerSession();
112 final var address = (InetSocketAddress) session.getClientAddress();
113 return new NetconfHelloMessageAdditionalHeader(session.getUsername(), address.getAddress().getHostAddress(),
114 String.valueOf(address.getPort()), "ssh", "client")
115 .toFormattedString().getBytes(StandardCharsets.UTF_8);