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;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.opendaylight.netconf.api.NetconfMessage;
29 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
33 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
34 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
35 import org.opendaylight.netconf.util.messages.FramingMechanism;
36 import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
37 import org.opendaylight.netconf.util.test.XmlFileLoader;
40 public class MessageParserTest {
42 private NetconfMessage msg;
45 public void setUp() throws Exception {
46 this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
51 public void testChunkedFramingMechanismOnPipeline() throws Exception {
52 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
53 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
54 new NetconfMessageToXMLEncoder(),
56 new NetconfChunkAggregator(),
57 new NetconfXMLToMessageDecoder());
59 testChunkChannel.writeOutbound(this.msg);
60 Queue<Object> messages = testChunkChannel.outboundMessages();
61 assertFalse(messages.isEmpty());
63 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
64 final ByteBuf out = Unpooled.buffer();
65 enc.encode(null, msg, out);
66 int msgLength = out.readableBytes();
68 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
69 if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
73 byte[] endOfChunkBytes = NetconfMessageConstants.END_OF_CHUNK.getBytes(StandardCharsets.UTF_8);
74 for (int i = 1; i <= chunkCount; i++) {
75 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
76 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
77 if (i == chunkCount) {
78 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
79 byte[] eom = new byte[endOfChunkBytes.length];
80 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
81 assertArrayEquals(endOfChunkBytes, eom);
84 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
85 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
86 recievedOutbound.getBytes(0, header);
87 assertEquals(exptHeaderLength, getHeaderLength(header));
89 testChunkChannel.writeInbound(recievedOutbound);
91 assertTrue(messages.isEmpty());
93 NetconfMessage receivedMessage = testChunkChannel.readInbound();
94 assertNotNull(receivedMessage);
95 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
100 public void testEOMFramingMechanismOnPipeline() throws Exception {
101 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
102 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
103 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
105 testChunkChannel.writeOutbound(this.msg);
106 ByteBuf recievedOutbound = testChunkChannel.readOutbound();
108 byte[] endOfMsgBytes = NetconfMessageConstants.END_OF_MESSAGE.getBytes(StandardCharsets.UTF_8);
109 byte[] eom = new byte[endOfMsgBytes.length];
110 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
111 assertArrayEquals(endOfMsgBytes, eom);
113 testChunkChannel.writeInbound(recievedOutbound);
114 NetconfMessage receivedMessage = testChunkChannel.readInbound();
115 assertNotNull(receivedMessage);
116 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
119 private static long getHeaderLength(byte[] bytes) {
120 byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
121 return Long.parseLong(StandardCharsets.US_ASCII.decode(
122 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());