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
9 package org.opendaylight.netconf.impl;
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.embedded.EmbeddedChannel;
21 import java.nio.ByteBuffer;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Queue;
24 import org.custommonkey.xmlunit.XMLUnit;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
29 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
33 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
34 import org.opendaylight.netconf.util.messages.FramingMechanism;
35 import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
36 import org.opendaylight.netconf.util.test.XmlFileLoader;
38 public class MessageParserTest {
40 private NetconfMessage msg;
43 public void setUp() throws Exception {
44 this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
48 public void testChunkedFramingMechanismOnPipeline() throws Exception {
49 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
50 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
51 new NetconfMessageToXMLEncoder(),
53 new NetconfChunkAggregator(),
54 new NetconfXMLToMessageDecoder());
56 testChunkChannel.writeOutbound(this.msg);
57 Queue<Object> messages = testChunkChannel.outboundMessages();
58 assertFalse(messages.isEmpty());
60 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
61 final ByteBuf out = Unpooled.buffer();
62 enc.encode(null, msg, out);
63 int msgLength = out.readableBytes();
65 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
66 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
70 byte[] endOfChunkBytes = NetconfMessageConstants.END_OF_CHUNK.getBytes(StandardCharsets.UTF_8);
71 for (int i = 1; i <= chunkCount; i++) {
72 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
73 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
74 if (i == chunkCount) {
75 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
76 byte[] eom = new byte[endOfChunkBytes.length];
77 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
78 assertArrayEquals(endOfChunkBytes, eom);
81 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
82 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
83 recievedOutbound.getBytes(0, header);
84 assertEquals(exptHeaderLength, getHeaderLength(header));
86 testChunkChannel.writeInbound(recievedOutbound);
88 assertTrue(messages.isEmpty());
90 NetconfMessage receivedMessage = testChunkChannel.readInbound();
91 assertNotNull(receivedMessage);
92 XMLUnit.setIgnoreWhitespace(true);
93 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
97 public void testEOMFramingMechanismOnPipeline() throws Exception {
98 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
99 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
100 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
102 testChunkChannel.writeOutbound(this.msg);
103 ByteBuf recievedOutbound = testChunkChannel.readOutbound();
105 byte[] endOfMsgBytes = NetconfMessageConstants.END_OF_MESSAGE.getBytes(StandardCharsets.UTF_8);
106 byte[] eom = new byte[endOfMsgBytes.length];
107 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
108 assertArrayEquals(endOfMsgBytes, eom);
110 testChunkChannel.writeInbound(recievedOutbound);
111 NetconfMessage receivedMessage = testChunkChannel.readInbound();
112 assertNotNull(receivedMessage);
113 XMLUnit.setIgnoreWhitespace(true);
114 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
117 private static long getHeaderLength(final byte[] bytes) {
118 byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
119 return Long.parseLong(StandardCharsets.US_ASCII.decode(
120 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());