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.NetconfMessageFactory;
30 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
31 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
32 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
33 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
35 public class MessageParserTest {
37 private NetconfMessage msg;
38 private NetconfMessageFactory msgFactory = new NetconfMessageFactory();
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 ProtocolMessageEncoder<NetconfMessage>(msgFactory),
51 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
52 new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
54 testChunkChannel.writeOutbound(this.msg);
55 Queue<Object> messages = testChunkChannel.outboundMessages();
56 assertFalse(messages.isEmpty());
58 int msgLength = msgFactory.put(this.msg).length;
59 int chunkCount = msgLength / NetconfMessageFactory.MAX_CHUNK_SIZE;
60 if ((msgLength % NetconfMessageFactory.MAX_CHUNK_SIZE) != 0) {
63 for (int i = 1; i <= chunkCount; i++) {
64 ByteBuf recievedOutbound = (ByteBuf) messages.poll();
65 int exptHeaderLength = NetconfMessageFactory.MAX_CHUNK_SIZE;
66 if (i == chunkCount) {
67 exptHeaderLength = msgLength - (NetconfMessageFactory.MAX_CHUNK_SIZE * (i - 1));
68 byte[] eom = new byte[NetconfMessageFactory.endOfChunk.length];
69 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageFactory.endOfChunk.length,
71 assertArrayEquals(NetconfMessageFactory.endOfChunk, eom);
74 byte[] header = new byte[String.valueOf(exptHeaderLength).length() + NetconfMessageHeader.MIN_HEADER_LENGTH
76 recievedOutbound.getBytes(0, header);
77 NetconfMessageHeader messageHeader = new NetconfMessageHeader();
78 messageHeader.fromBytes(header);
79 assertEquals(exptHeaderLength, messageHeader.getLength());
81 testChunkChannel.writeInbound(recievedOutbound);
83 assertTrue(messages.isEmpty());
85 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
86 assertNotNull(receivedMessage);
87 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
91 public void testEOMFramingMechanismOnPipeline() throws Exception {
92 EmbeddedChannel testChunkChannel = new EmbeddedChannel(
93 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
94 new ProtocolMessageEncoder<NetconfMessage>(msgFactory), new NetconfMessageAggregator(
95 FramingMechanism.EOM), new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
97 testChunkChannel.writeOutbound(this.msg);
98 ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
100 byte[] eom = new byte[NetconfMessageFactory.endOfMessage.length];
101 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageFactory.endOfMessage.length, eom);
102 assertArrayEquals(NetconfMessageFactory.endOfMessage, eom);
104 testChunkChannel.writeInbound(recievedOutbound);
105 NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
106 assertNotNull(receivedMessage);
107 assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));