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.controller.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.util.Queue;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfMessage;
25 import org.opendaylight.controller.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
26 import org.opendaylight.controller.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
27 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator;
28 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEOMAggregator;
29 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
30 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
31 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
32 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
33 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
34 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
36 public class MessageParserTest {
38 private NetconfMessage msg;
41 public void setUp() throws Exception {
42 this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
46 public void testChunkedFramingMechanismOnPipeline() throws Exception {
47 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
48 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
49 new NetconfMessageToXMLEncoder(),
51 new NetconfChunkAggregator(),
52 new NetconfXMLToMessageDecoder());
54 testChunkChannel.writeOutbound(this.msg);
55 Queue<Object> messages = testChunkChannel.outboundMessages();
56 assertFalse(messages.isEmpty());
58 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
59 final ByteBuf out = Unpooled.buffer();
60 enc.encode(null, msg, out);
61 int msgLength = out.readableBytes();
63 int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
64 if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
67 for (int i = 1; i <= chunkCount; i++) {
68 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
69 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
70 if (i == chunkCount) {
71 exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
72 byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
73 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
75 assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
78 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
79 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
80 recievedOutbound.getBytes(0, header);
81 NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
82 assertEquals(exptHeaderLength, messageHeader.getLength());
84 testChunkChannel.writeInbound(recievedOutbound);
86 assertTrue(messages.isEmpty());
88 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
89 assertNotNull(receivedMessage);
90 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
94 public void testEOMFramingMechanismOnPipeline() throws Exception {
95 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
96 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
97 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
99 testChunkChannel.writeOutbound(this.msg);
100 ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
102 byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
103 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
104 assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
106 testChunkChannel.writeInbound(recievedOutbound);
107 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
108 assertNotNull(receivedMessage);
109 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());