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.impl;
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.nettyutil.handler.ChunkedFramingMechanismEncoder;
26 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
27 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
31 import org.opendaylight.netconf.util.messages.FramingMechanism;
32 import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
33 import org.opendaylight.netconf.util.test.XmlFileLoader;
35 public class MessageParserTest {
36 private NetconfMessage msg;
39 public void setUp() throws Exception {
40 this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
44 public void testChunkedFramingMechanismOnPipeline() throws Exception {
45 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
46 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
47 new NetconfMessageToXMLEncoder(),
48 new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
49 new NetconfXMLToMessageDecoder());
51 testChunkChannel.writeOutbound(this.msg);
52 Queue<Object> messages = testChunkChannel.outboundMessages();
53 assertEquals(1, messages.size());
55 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
56 final ByteBuf out = Unpooled.buffer();
57 enc.encode(null, msg, out);
58 int msgLength = out.readableBytes();
60 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
61 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
65 byte[] endOfChunkBytes = NetconfMessageConstants.END_OF_CHUNK.getBytes(StandardCharsets.UTF_8);
66 for (int i = 1; i <= chunkCount; i++) {
67 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
68 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
69 if (i == chunkCount) {
70 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
71 byte[] eom = new byte[endOfChunkBytes.length];
72 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
73 assertArrayEquals(endOfChunkBytes, eom);
76 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
77 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
78 recievedOutbound.getBytes(0, header);
79 assertEquals(exptHeaderLength, getHeaderLength(header));
81 testChunkChannel.writeInbound(recievedOutbound);
83 assertEquals(0, messages.size());
85 NetconfMessage receivedMessage = testChunkChannel.readInbound();
86 assertNotNull(receivedMessage);
87 XMLUnit.setIgnoreWhitespace(true);
88 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
92 public void testEOMFramingMechanismOnPipeline() throws Exception {
93 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
94 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
95 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
97 testChunkChannel.writeOutbound(this.msg);
98 ByteBuf recievedOutbound = testChunkChannel.readOutbound();
100 byte[] endOfMsgBytes = NetconfMessageConstants.END_OF_MESSAGE.getBytes(StandardCharsets.UTF_8);
101 byte[] eom = new byte[endOfMsgBytes.length];
102 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
103 assertArrayEquals(endOfMsgBytes, eom);
105 testChunkChannel.writeInbound(recievedOutbound);
106 NetconfMessage receivedMessage = testChunkChannel.readInbound();
107 assertNotNull(receivedMessage);
108 XMLUnit.setIgnoreWhitespace(true);
109 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
112 private static long getHeaderLength(final byte[] bytes) {
113 byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
114 return Long.parseLong(StandardCharsets.US_ASCII.decode(
115 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());