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.NetconfMessage;
23 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
24 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
25 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
26 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
27 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
29 import org.opendaylight.netconf.test.util.XmlFileLoader;
30 import org.xmlunit.builder.DiffBuilder;
32 public class MessageParserTest {
33 private NetconfMessage msg;
36 public void setUp() throws Exception {
37 msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
41 public void testChunkedFramingMechanismOnPipeline() throws Exception {
42 final var testChunkChannel = new EmbeddedChannel(
43 new ChunkedFramingMechanismEncoder(),
44 new NetconfMessageToXMLEncoder(),
45 new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
46 new NetconfXMLToMessageDecoder());
48 testChunkChannel.writeOutbound(msg);
49 final var messages = testChunkChannel.outboundMessages();
50 assertEquals(1, messages.size());
52 final var enc = new NetconfMessageToXMLEncoder();
53 final var out = Unpooled.buffer();
54 enc.encode(null, msg, out);
55 final int msgLength = out.readableBytes();
57 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
58 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
62 final var endOfChunkBytes = "\n##\n".getBytes(StandardCharsets.US_ASCII);
63 for (int i = 1; i <= chunkCount; i++) {
64 final var recievedOutbound = (ByteBuf) messages.poll();
65 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
66 if (i == chunkCount) {
67 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
68 byte[] eom = new byte[endOfChunkBytes.length];
69 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
70 assertArrayEquals(endOfChunkBytes, eom);
73 final var header = new byte[String.valueOf(exptHeaderLength).length() + 3];
74 recievedOutbound.getBytes(0, header);
75 assertEquals(exptHeaderLength, getHeaderLength(header));
77 testChunkChannel.writeInbound(recievedOutbound);
79 assertEquals(0, messages.size());
81 final NetconfMessage receivedMessage = testChunkChannel.readInbound();
82 assertNotNull(receivedMessage);
84 final var diff = DiffBuilder.compare(msg.getDocument())
85 .withTest(receivedMessage.getDocument())
89 assertFalse(diff.toString(), diff.hasDifferences());
93 public void testEOMFramingMechanismOnPipeline() throws Exception {
94 final var testChunkChannel = new EmbeddedChannel(
95 new EOMFramingMechanismEncoder(),
96 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
98 testChunkChannel.writeOutbound(msg);
99 final ByteBuf recievedOutbound = testChunkChannel.readOutbound();
101 final var endOfMsgBytes = "]]>]]>".getBytes(StandardCharsets.US_ASCII);
102 final var eom = new byte[endOfMsgBytes.length];
103 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
104 assertArrayEquals(endOfMsgBytes, eom);
106 testChunkChannel.writeInbound(recievedOutbound);
107 NetconfMessage receivedMessage = testChunkChannel.readInbound();
108 assertNotNull(receivedMessage);
110 final var diff = DiffBuilder.compare(msg.getDocument())
111 .withTest(receivedMessage.getDocument())
115 assertFalse(diff.toString(), diff.hasDifferences());
118 private static long getHeaderLength(final byte[] bytes) {
119 final var headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
120 return Long.parseLong(StandardCharsets.US_ASCII.decode(
121 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());