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;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import io.netty.channel.embedded.EmbeddedChannel;
21 import java.util.Queue;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.netconf.api.NetconfMessage;
26 import org.opendaylight.controller.netconf.util.handler.ChunkedFramingMechanismEncoder;
27 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
28 import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
29 import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
30 import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
31 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
32 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
33 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
34 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
35 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
37 public class MessageParserTest {
39 private NetconfMessage msg;
42 public void setUp() throws Exception {
43 this.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(this.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) {
68 for (int i = 1; i <= chunkCount; i++) {
69 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
70 int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
71 if (i == chunkCount) {
72 exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
73 byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
74 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
76 assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
79 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
80 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
81 recievedOutbound.getBytes(0, header);
82 NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
83 assertEquals(exptHeaderLength, messageHeader.getLength());
85 testChunkChannel.writeInbound(recievedOutbound);
87 assertTrue(messages.isEmpty());
89 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
90 assertNotNull(receivedMessage);
91 assertXMLEqual(this.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(this.msg);
101 ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
103 byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
104 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
105 assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
107 testChunkChannel.writeInbound(recievedOutbound);
108 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
109 assertNotNull(receivedMessage);
110 assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());