Do not propagate empty writes
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / NetconfSubsystem.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.server;
9
10 import static java.util.Objects.requireNonNull;
11
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
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;
33
34     NetconfSubsystem(final String name, final ServerChannelInitializer channelInitializer) {
35         super(name, null);
36         this.channelInitializer = requireNonNull(channelInitializer);
37     }
38
39     @Override
40     public void run() {
41         // not used
42     }
43
44     @Override
45     public void setIoInputStream(final IoInputStream in) {
46         // not used
47     }
48
49     @Override
50     public void setIoErrorStream(final IoOutputStream err) {
51         // not used
52     }
53
54     @Override
55     public void setIoOutputStream(final IoOutputStream out) {
56         /*
57          * While NETCONF protocol handlers are designed to operate over Netty channel, the inner channel is used to
58          * serve NETCONF over SSH.
59          */
60         // outbound packet handler, adding fist means it will be invoked last because of flow direction
61         innerChannel.pipeline().addFirst(new NetconfSubsystemOutboundChannelHandler(out));
62
63         // inner channel termination handler
64         innerChannel.pipeline().addLast(
65             new ChannelInboundHandlerAdapter() {
66                 @Override
67                 public void channelInactive(final ChannelHandlerContext ctx) {
68                     onExit(0);
69                 }
70             });
71
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()));
78     }
79
80     @Override
81     public void setChannelSession(final ChannelSession channelSession) {
82         /*
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.
86          */
87         channelSession.setDataReceiver(this);
88     }
89
90     @Override
91     public int data(final ChannelSession channel, final byte[] buf, final int start, final int len) {
92         // Do not propagate empty invocations
93         if (len != 0) {
94             innerChannel.writeInbound(Unpooled.copiedBuffer(buf, start, len));
95         }
96         return len;
97     }
98
99     @Override
100     public void close() {
101         innerChannel.close();
102     }
103
104     @Override
105     protected void onExit(final int exitValue, final String exitMessage) {
106         super.onExit(exitValue, exitMessage);
107         innerChannel.close();
108     }
109
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);
116     }
117 }