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.test.XmlFileLoader;
37 public class MessageParserTest {
39 private NetconfMessage msg;
42 public void setUp() throws Exception {
43 msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
47 public void testChunkedFramingMechanismOnPipeline() throws Exception {
48 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
49 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
50 new NetconfMessageToXMLEncoder(),
52 new NetconfChunkAggregator(),
53 new NetconfXMLToMessageDecoder());
55 testChunkChannel.writeOutbound(msg);
56 Queue<Object> messages = testChunkChannel.outboundMessages();
57 assertFalse(messages.isEmpty());
59 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
60 final ByteBuf out = Unpooled.buffer();
61 enc.encode(null, msg, out);
62 int msgLength = out.readableBytes();
64 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
65 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
69 byte[] endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
70 for (int i = 1; i <= chunkCount; i++) {
71 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
72 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
73 if (i == chunkCount) {
74 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
75 byte[] eom = new byte[endOfChunkBytes.length];
76 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
77 assertArrayEquals(endOfChunkBytes, eom);
80 byte[] header = new byte[String.valueOf(exptHeaderLength).length() + 3];
81 recievedOutbound.getBytes(0, header);
82 assertEquals(exptHeaderLength, getHeaderLength(header));
84 testChunkChannel.writeInbound(recievedOutbound);
86 assertTrue(messages.isEmpty());
88 NetconfMessage receivedMessage = testChunkChannel.readInbound();
89 assertNotNull(receivedMessage);
90 XMLUnit.setIgnoreWhitespace(true);
91 assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
95 public void testEOMFramingMechanismOnPipeline() throws Exception {
96 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
97 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
98 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
100 testChunkChannel.writeOutbound(msg);
101 ByteBuf recievedOutbound = testChunkChannel.readOutbound();
103 byte[] endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
104 byte[] eom = new byte[endOfMsgBytes.length];
105 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
106 assertArrayEquals(endOfMsgBytes, eom);
108 testChunkChannel.writeInbound(recievedOutbound);
109 NetconfMessage receivedMessage = testChunkChannel.readInbound();
110 assertNotNull(receivedMessage);
111 XMLUnit.setIgnoreWhitespace(true);
112 assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
115 private static long getHeaderLength(final byte[] bytes) {
116 byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
117 return Long.parseLong(StandardCharsets.US_ASCII.decode(
118 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());