116a9bc53ca0e8cfc453bf0fa3c48636efb34562
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / NettyChannelSubsystem.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.nettyutil.handler.ssh.client;
9
10 import io.netty.buffer.Unpooled;
11 import io.netty.channel.ChannelHandlerContext;
12 import java.io.IOException;
13 import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Abstract base class for {@link ChannelSubsystem}s backed by a Netty {@link ChannelHandlerContext}.
19  */
20 @Deprecated(since = "7.0.0", forRemoval = true)
21 abstract class NettyChannelSubsystem extends ChannelSubsystem {
22     private static final Logger LOG = LoggerFactory.getLogger(NettyChannelSubsystem.class);
23
24     NettyChannelSubsystem(final String subsystem) {
25         super(subsystem);
26     }
27
28     @Override
29     public final void close() {
30         close(false);
31     }
32
33     @Override
34     protected final void doWriteExtendedData(final byte[] data, final int off, final long len) throws IOException {
35         // If we're already closing, ignore incoming data
36         if (isClosing()) {
37             return;
38         }
39
40         LOG.debug("Discarding {} bytes of extended data", len);
41         if (len > 0) {
42             getLocalWindow().release(len);
43         }
44     }
45
46     @Override
47     protected final void doWriteData(final byte[] data, final int off, final long len) throws IOException {
48         // If we're already closing, ignore incoming data
49         if (isClosing()) {
50             return;
51         }
52
53         // TODO: consider using context's allocator for heap buffer here
54         final int reqLen = (int) len;
55         context().fireChannelRead(Unpooled.copiedBuffer(data, off, reqLen));
56         if (reqLen > 0) {
57             getLocalWindow().release(reqLen);
58         }
59     }
60
61     abstract ChannelHandlerContext context();
62 }