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.transport.ssh;
10 import static java.util.Objects.requireNonNull;
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;
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.
27 final class OutboundChannelHandler extends ChannelOutboundHandlerAdapter {
28 private static final Logger LOG = LoggerFactory.getLogger(OutboundChannelHandler.class);
30 private final IoOutputStream out;
32 OutboundChannelHandler(final IoOutputStream out) {
33 this.out = requireNonNull(out);
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());
44 final var sshBuf = toSshBuffer(byteBuf);
45 final IoWriteFuture writeFuture;
47 writeFuture = out.writeBuffer(sshBuf);
48 } catch (IOException e) {
49 LOG.error("Error writing buffer", e);
50 promise.setFailure(e);
54 writeFuture.addListener(future -> {
55 if (future.isWritten()) {
56 // report outbound message being handled
58 } else if (future.getException() != null) {
59 LOG.error("Error writing buffer", future.getException());
60 promise.setFailure(future.getException());
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
74 return new ByteArrayBuffer(bytes);