c7db99a9f9e3004aa9e915614c85aae50618a901
[netconf.git] / transport / transport-ssh / src / main / java / org / opendaylight / netconf / transport / ssh / OutboundChannelHandler.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.transport.ssh;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelOutboundHandlerAdapter;
15 import io.netty.channel.ChannelPromise;
16 import java.io.IOException;
17 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
18 import org.opendaylight.netconf.shaded.sshd.common.io.IoWriteFuture;
19 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.ByteArrayBuffer;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * A ChannelOutboundHandler responsible for redirecting whatever bytes need to be written out on the Netty channel so
25  * that they pass into SSHD's output.
26  */
27 final class OutboundChannelHandler extends ChannelOutboundHandlerAdapter {
28     private static final Logger LOG = LoggerFactory.getLogger(OutboundChannelHandler.class);
29
30     private final IoOutputStream out;
31
32     OutboundChannelHandler(final IoOutputStream out) {
33         this.out = requireNonNull(out);
34     }
35
36     @Override
37     public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
38         // redirect channel outgoing packets to output stream linked to transport
39         if (!(msg instanceof ByteBuf byteBuf)) {
40             LOG.trace("Ignoring unrecognized {}", msg == null ? null : msg.getClass());
41             return;
42         }
43
44         final var sshBuf = toSshBuffer(byteBuf);
45         final IoWriteFuture writeFuture;
46         try {
47             writeFuture = out.writeBuffer(sshBuf);
48         } catch (IOException e) {
49             LOG.error("Error writing buffer", e);
50             promise.setFailure(e);
51             return;
52         }
53
54         writeFuture.addListener(future -> {
55             if (future.isWritten()) {
56                 // report outbound message being handled
57                 promise.setSuccess();
58             } else if (future.getException() != null) {
59                 LOG.error("Error writing buffer", future.getException());
60                 promise.setFailure(future.getException());
61             }
62         });
63     }
64
65     // TODO: This can amount to a lot of copying around. Is it worth our while to create a ByteBufBuffer, which
66     //       would implement Buffer API on top a ByteBuf?
67     //       If we decide to do that, we need to decide to interface with ByteBuf (readRetainedSlice() ?) and then
68     //       release it only after the write has been resolved
69     private static ByteArrayBuffer toSshBuffer(final ByteBuf byteBuf) {
70         final var bytes = new byte[byteBuf.readableBytes()];
71         byteBuf.readBytes(bytes);
72         // Netty buffer can be recycled now
73         byteBuf.release();
74         return new ByteArrayBuffer(bytes);
75     }
76 }