2 * Copyright (c) 2013 Cisco Systems, Inc. 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.server;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.embedded.EmbeddedChannel;
18 import java.nio.ByteBuffer;
19 import java.nio.charset.StandardCharsets;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.netconf.api.messages.FramingMechanism;
23 import org.opendaylight.netconf.api.messages.NetconfMessage;
24 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
25 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
26 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
27 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
30 import org.opendaylight.netconf.test.util.XmlFileLoader;
31 import org.xmlunit.builder.DiffBuilder;
33 public class MessageParserTest {
34 private NetconfMessage msg;
37 public void setUp() throws Exception {
38 msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
42 public void testChunkedFramingMechanismOnPipeline() throws Exception {
43 final var testChunkChannel = new EmbeddedChannel(
44 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
45 new NetconfMessageToXMLEncoder(),
46 new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
47 new NetconfXMLToMessageDecoder());
49 testChunkChannel.writeOutbound(msg);
50 final var messages = testChunkChannel.outboundMessages();
51 assertEquals(1, messages.size());
53 final var enc = new NetconfMessageToXMLEncoder();
54 final var out = Unpooled.buffer();
55 enc.encode(null, msg, out);
56 final int msgLength = out.readableBytes();
58 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
59 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
63 final var endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
64 for (int i = 1; i <= chunkCount; i++) {
65 final var recievedOutbound = (ByteBuf) messages.poll();
66 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
67 if (i == chunkCount) {
68 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
69 byte[] eom = new byte[endOfChunkBytes.length];
70 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
71 assertArrayEquals(endOfChunkBytes, eom);
74 final var header = new byte[String.valueOf(exptHeaderLength).length() + 3];
75 recievedOutbound.getBytes(0, header);
76 assertEquals(exptHeaderLength, getHeaderLength(header));
78 testChunkChannel.writeInbound(recievedOutbound);
80 assertEquals(0, messages.size());
82 final NetconfMessage receivedMessage = testChunkChannel.readInbound();
83 assertNotNull(receivedMessage);
85 final var diff = DiffBuilder.compare(msg.getDocument())
86 .withTest(receivedMessage.getDocument())
90 assertFalse(diff.toString(), diff.hasDifferences());
94 public void testEOMFramingMechanismOnPipeline() throws Exception {
95 final var testChunkChannel = new EmbeddedChannel(
96 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
97 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
99 testChunkChannel.writeOutbound(msg);
100 final ByteBuf recievedOutbound = testChunkChannel.readOutbound();
102 final var endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
103 final var eom = new byte[endOfMsgBytes.length];
104 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
105 assertArrayEquals(endOfMsgBytes, eom);
107 testChunkChannel.writeInbound(recievedOutbound);
108 NetconfMessage receivedMessage = testChunkChannel.readInbound();
109 assertNotNull(receivedMessage);
111 final var diff = DiffBuilder.compare(msg.getDocument())
112 .withTest(receivedMessage.getDocument())
116 assertFalse(diff.toString(), diff.hasDifferences());
119 private static long getHeaderLength(final byte[] bytes) {
120 final var headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
121 return Long.parseLong(StandardCharsets.US_ASCII.decode(
122 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());