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.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import io.netty.channel.embedded.EmbeddedChannel;
20 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.util.handler.FramingMechanismHandlerFactory;
26 import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
27 import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
28 import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
29 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
30 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
31 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
32 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
33 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
35 public class MessageParserTest {
37 private NetconfMessage msg;
40 public void setUp() throws Exception {
41 this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
45 public void testChunkedFramingMechanismOnPipeline() throws Exception {
46 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
47 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
48 new NetconfMessageToXMLEncoder(),
50 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
51 new NetconfXMLToMessageDecoder());
53 testChunkChannel.writeOutbound(this.msg);
54 Queue<Object> messages = testChunkChannel.outboundMessages();
55 assertFalse(messages.isEmpty());
57 final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
58 final ByteBuf out = Unpooled.buffer();
59 enc.encode(null, msg, out);
60 int msgLength = out.readableBytes();
62 int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
63 if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
66 for (int i = 1; i <= chunkCount; i++) {
67 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
68 int exptHeaderLength = NetconfMessageConstants.MAX_CHUNK_SIZE;
69 if (i == chunkCount) {
70 exptHeaderLength = msgLength - (NetconfMessageConstants.MAX_CHUNK_SIZE * (i - 1));
71 byte[] eom = new byte[NetconfMessageConstants.endOfChunk.length];
72 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfChunk.length,
74 assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
77 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
78 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
79 recievedOutbound.getBytes(0, header);
80 NetconfMessageHeader messageHeader = new NetconfMessageHeader();
81 messageHeader.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 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
94 public void testEOMFramingMechanismOnPipeline() throws Exception {
95 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
96 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
97 new NetconfMessageToXMLEncoder(), new NetconfMessageAggregator(
98 FramingMechanism.EOM), new NetconfXMLToMessageDecoder());
100 testChunkChannel.writeOutbound(this.msg);
101 ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
103 byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
104 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
105 assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
107 testChunkChannel.writeInbound(recievedOutbound);
108 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
109 assertNotNull(receivedMessage);
110 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));