Clean up NetconfClientSessionImpl
[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 abstract class NettyChannelSubsystem extends ChannelSubsystem {
21     private static final Logger LOG = LoggerFactory.getLogger(NettyChannelSubsystem.class);
22
23     NettyChannelSubsystem(final String subsystem) {
24         super(subsystem);
25     }
26
27     @Override
28     public final void close() {
29         close(false);
30     }
31
32     @Override
33     protected final void doWriteExtendedData(final byte[] data, final int off, final long len) throws IOException {
34         // If we're already closing, ignore incoming data
35         if (isClosing()) {
36             return;
37         }
38
39         LOG.debug("Discarding {} bytes of extended data", len);
40         if (len > 0) {
41             getLocalWindow().release(len);
42         }
43     }
44
45     @Override
46     protected final void doWriteData(final byte[] data, final int off, final long len) throws IOException {
47         // If we're already closing, ignore incoming data
48         if (isClosing()) {
49             return;
50         }
51
52         // TODO: consider using context's allocator for heap buffer here
53         final int reqLen = (int) len;
54         context().fireChannelRead(Unpooled.copiedBuffer(data, off, reqLen));
55         if (reqLen > 0) {
56             getLocalWindow().release(reqLen);
57         }
58     }
59
60     abstract ChannelHandlerContext context();
61 }