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.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
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 java.util.Queue;
21 import org.custommonkey.xmlunit.XMLUnit;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.api.messages.FramingMechanism;
26 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
27 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
32 import org.opendaylight.netconf.test.util.XmlFileLoader;
34 public class MessageParserTest {
35 private NetconfMessage msg;
38 public void setUp() throws Exception {
39 msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
43 public void testChunkedFramingMechanismOnPipeline() throws Exception {
44 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
45 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
46 new NetconfMessageToXMLEncoder(),
47 new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
48 new NetconfXMLToMessageDecoder());
50 testChunkChannel.writeOutbound(msg);
51 Queue<Object> messages = testChunkChannel.outboundMessages();
52 assertEquals(1, messages.size());
54 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
55 final ByteBuf out = Unpooled.buffer();
56 enc.encode(null, msg, out);
57 int msgLength = out.readableBytes();
59 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
60 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
64 byte[] endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
65 for (int i = 1; i <= chunkCount; i++) {
66 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
67 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
68 if (i == chunkCount) {
69 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
70 byte[] eom = new byte[endOfChunkBytes.length];
71 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
72 assertArrayEquals(endOfChunkBytes, eom);
75 byte[] header = new byte[String.valueOf(exptHeaderLength).length() + 3];
76 recievedOutbound.getBytes(0, header);
77 assertEquals(exptHeaderLength, getHeaderLength(header));
79 testChunkChannel.writeInbound(recievedOutbound);
81 assertEquals(0, messages.size());
83 NetconfMessage receivedMessage = testChunkChannel.readInbound();
84 assertNotNull(receivedMessage);
85 XMLUnit.setIgnoreWhitespace(true);
86 assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
90 public void testEOMFramingMechanismOnPipeline() throws Exception {
91 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
92 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
93 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
95 testChunkChannel.writeOutbound(msg);
96 ByteBuf recievedOutbound = testChunkChannel.readOutbound();
98 byte[] endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
99 byte[] eom = new byte[endOfMsgBytes.length];
100 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
101 assertArrayEquals(endOfMsgBytes, eom);
103 testChunkChannel.writeInbound(recievedOutbound);
104 NetconfMessage receivedMessage = testChunkChannel.readInbound();
105 assertNotNull(receivedMessage);
106 XMLUnit.setIgnoreWhitespace(true);
107 assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
110 private static long getHeaderLength(final byte[] bytes) {
111 byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
112 return Long.parseLong(StandardCharsets.US_ASCII.decode(
113 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());