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;
17 import java.util.Queue;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.channel.embedded.EmbeddedChannel;
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.messages.FramingMechanism;
29 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
30 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
31 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
32 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
33 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
34 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
36 public class MessageParserTest {
38 private NetconfMessage msg;
39 private NetconfMessageFactory msgFactory = new NetconfMessageFactory();
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 ProtocolMessageEncoder<NetconfMessage>(msgFactory),
52 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
53 new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
55 testChunkChannel.writeOutbound(this.msg);
56 Queue<Object> messages = testChunkChannel.outboundMessages();
57 assertFalse(messages.isEmpty());
59 int msgLength = this.msgFactory.put(this.msg).length;
60 int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
61 if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
64 for (int i = 1; i <= chunkCount; i++) {
65 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
66 int exptHeaderLength = NetconfMessageConstants.MAX_CHUNK_SIZE;
67 if (i == chunkCount) {
68 exptHeaderLength = msgLength - (NetconfMessageConstants.MAX_CHUNK_SIZE * (i - 1));
69 byte[] eom = new byte[NetconfMessageConstants.endOfChunk.length];
70 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfChunk.length,
72 assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
75 byte[] header = new byte[String.valueOf(exptHeaderLength).length()
76 + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
77 recievedOutbound.getBytes(0, header);
78 NetconfMessageHeader messageHeader = new NetconfMessageHeader();
79 messageHeader.fromBytes(header);
80 assertEquals(exptHeaderLength, messageHeader.getLength());
82 testChunkChannel.writeInbound(recievedOutbound);
84 assertTrue(messages.isEmpty());
86 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
87 assertNotNull(receivedMessage);
88 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
92 public void testEOMFramingMechanismOnPipeline() throws Exception {
93 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
94 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
95 new ProtocolMessageEncoder<NetconfMessage>(msgFactory), new NetconfMessageAggregator(
96 FramingMechanism.EOM), new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
98 testChunkChannel.writeOutbound(this.msg);
99 ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
101 byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
102 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
103 assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
105 testChunkChannel.writeInbound(recievedOutbound);
106 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
107 assertNotNull(receivedMessage);
108 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));