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