import io.netty.channel.Channel;
import io.netty.util.concurrent.Promise;
import org.opendaylight.netconf.api.NetconfSession;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
-import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfHelloMessageToXMLEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
public void initialize(final Channel ch, final Promise<S> promise) {
ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
initializeMessageDecoder(ch);
- ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
+ ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, new EOMFramingMechanismEncoder());
initializeMessageEncoder(ch);
initializeSessionNegotiator(ch, promise);
import org.opendaylight.netconf.api.NamespaceURN;
import org.opendaylight.netconf.api.NetconfDocumentedException;
import org.opendaylight.netconf.api.NetconfSessionListener;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.api.messages.NetconfMessage;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
*/
private void insertChunkFramingToPipeline() {
replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER,
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK));
+ new ChunkedFramingMechanismEncoder());
replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR,
- new NetconfChunkAggregator(maximumIncomingChunkSize));
+ new NetconfChunkAggregator(maximumIncomingChunkSize));
}
private boolean shouldUseChunkFraming(final Document doc) {
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.nettyutil.handler;
-import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.MessageToByteEncoder;
import java.nio.charset.StandardCharsets;
+import org.opendaylight.netconf.api.messages.FramingMechanism;
-public class ChunkedFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
+/**
+ * A {@link FramingMechanismEncoder} handling {@link FramingMechanism#CHUNK}.
+ */
+public final class ChunkedFramingMechanismEncoder extends FramingMechanismEncoder {
public static final int DEFAULT_CHUNK_SIZE = 8192;
public static final int MIN_CHUNK_SIZE = 128;
public static final int MAX_CHUNK_SIZE = 16 * 1024 * 1024;
}
public ChunkedFramingMechanismEncoder(final int chunkSize) {
- Preconditions.checkArgument(chunkSize >= MIN_CHUNK_SIZE && chunkSize <= MAX_CHUNK_SIZE,
- "Unsupported chunk size %s", chunkSize);
+ if (chunkSize < MIN_CHUNK_SIZE) {
+ throw new IllegalArgumentException(chunkSize + " is lower than minimum supported " + MIN_CHUNK_SIZE);
+ }
+ if (chunkSize > MAX_CHUNK_SIZE) {
+ throw new IllegalArgumentException(chunkSize + " is lower than maximum supported " + MAX_CHUNK_SIZE);
+ }
this.chunkSize = chunkSize;
}
- public final int getChunkSize() {
- return chunkSize;
- }
-
@Override
protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final ByteBuf out) {
do {
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.nettyutil.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.MessageToByteEncoder;
+import org.opendaylight.netconf.api.messages.FramingMechanism;
-public class EOMFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
+/**
+ * A {@link FramingMechanismEncoder} handling {@link FramingMechanism#EOM}.
+ */
+public final class EOMFramingMechanismEncoder extends FramingMechanismEncoder {
@Override
- protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
+ protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final ByteBuf out) {
out.writeBytes(msg);
out.writeBytes(MessageParts.END_OF_MESSAGE);
}
/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.MessageToByteEncoder;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.messages.FramingMechanism;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public final class FramingMechanismHandlerFactory {
- private static final Logger LOG = LoggerFactory.getLogger(FramingMechanismHandlerFactory.class);
+/**
+ * An channel handler framing outbound messages into specified framing.
+ */
+public abstract sealed class FramingMechanismEncoder extends MessageToByteEncoder<ByteBuf>
+ permits ChunkedFramingMechanismEncoder, EOMFramingMechanismEncoder {
+ private static final Logger LOG = LoggerFactory.getLogger(FramingMechanismEncoder.class);
- private FramingMechanismHandlerFactory() {
- // not called - private constructor for utility class
+ FramingMechanismEncoder() {
+ // Hidden on purpose
}
- public static MessageToByteEncoder<ByteBuf> createHandler(final FramingMechanism framingMechanism) {
+ /**
+ * Return a {@link FramingMechanismEncoder} for specified {@link FramingMechanism}.
+ *
+ * @param framingMechanism Desired {@link FramingMechanism}
+ * @return A {@link FramingMechanismEncoder}
+ */
+ public static final @NonNull FramingMechanismEncoder of(final FramingMechanism framingMechanism) {
LOG.debug("{} framing mechanism was selected.", framingMechanism);
return switch (framingMechanism) {
case CHUNK -> new ChunkedFramingMechanismEncoder();
import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.netconf.api.CapabilityURN;
import org.opendaylight.netconf.api.NetconfSessionListener;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
-import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
new ChannelInboundHandlerAdapter());
channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, xmlToHello);
- channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
+ channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, new EOMFramingMechanismEncoder());
channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
hello = HelloMessage.createClientHello(Set.of(), Optional.empty());
helloBase11 = HelloMessage.createClientHello(Set.of(CapabilityURN.BASE_1_1), Optional.empty());
import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.netconf.api.CapabilityURN;
import org.opendaylight.netconf.api.NetconfSessionListener;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
-import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
new ChannelInboundHandlerAdapter());
channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
new NetconfXMLToHelloMessageDecoder());
- channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
+ channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, new EOMFramingMechanismEncoder());
channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
final HelloMessage serverHello = HelloMessage.createClientHello(Set.of(CapabilityURN.BASE_1_1),
Optional.empty());
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.nettyutil.handler;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import org.junit.jupiter.api.Test;
+import org.opendaylight.netconf.api.messages.FramingMechanism;
+
+class FramingMechanismEncoderTest {
+ @Test
+ void testCreate() {
+ assertInstanceOf(ChunkedFramingMechanismEncoder.class, FramingMechanismEncoder.of(FramingMechanism.CHUNK));
+ assertInstanceOf(EOMFramingMechanismEncoder.class, FramingMechanismEncoder.of(FramingMechanism.EOM));
+ }
+}
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.nettyutil.handler;
-
-import org.hamcrest.CoreMatchers;
-import org.hamcrest.MatcherAssert;
-import org.junit.Test;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
-
-public class FramingMechanismHandlerFactoryTest {
- @Test
- public void testCreate() throws Exception {
- MatcherAssert.assertThat(FramingMechanismHandlerFactory
- .createHandler(FramingMechanism.CHUNK), CoreMatchers
- .instanceOf(ChunkedFramingMechanismEncoder.class));
- MatcherAssert.assertThat(FramingMechanismHandlerFactory
- .createHandler(FramingMechanism.EOM), CoreMatchers
- .instanceOf(EOMFramingMechanismEncoder.class));
- }
-}
\ No newline at end of file
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.netconf.api.messages.FramingMechanism;
import org.opendaylight.netconf.api.messages.NetconfMessage;
import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
-import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
@Test
public void testChunkedFramingMechanismOnPipeline() throws Exception {
final var testChunkChannel = new EmbeddedChannel(
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
+ new ChunkedFramingMechanismEncoder(),
new NetconfMessageToXMLEncoder(),
new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
new NetconfXMLToMessageDecoder());
chunkCount++;
}
- final var endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
+ final var endOfChunkBytes = "\n##\n".getBytes(StandardCharsets.US_ASCII);
for (int i = 1; i <= chunkCount; i++) {
final var recievedOutbound = (ByteBuf) messages.poll();
int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
@Test
public void testEOMFramingMechanismOnPipeline() throws Exception {
final var testChunkChannel = new EmbeddedChannel(
- FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
+ new EOMFramingMechanismEncoder(),
new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
testChunkChannel.writeOutbound(msg);
final ByteBuf recievedOutbound = testChunkChannel.readOutbound();
- final var endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
+ final var endOfMsgBytes = "]]>]]>".getBytes(StandardCharsets.US_ASCII);
final var eom = new byte[endOfMsgBytes.length];
recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
assertArrayEquals(endOfMsgBytes, eom);