Reworked Netconf framing mechanism, added chunked framing mechanism.
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / MessageParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.impl;
10
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
17 import java.util.Queue;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.channel.embedded.EmbeddedChannel;
21
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;
34
35 public class MessageParserTest {
36
37     private NetconfMessage msg;
38     private NetconfMessageFactory msgFactory = new NetconfMessageFactory();
39
40     @Before
41     public void setUp() throws Exception {
42         this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
43     }
44
45     @Test
46     public void testChunkedFramingMechanismOnPipeline() throws Exception {
47         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
48                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
49                 new ProtocolMessageEncoder<NetconfMessage>(msgFactory),
50
51                 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
52                 new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
53
54         testChunkChannel.writeOutbound(this.msg);
55         Queue<Object> messages = testChunkChannel.outboundMessages();
56         assertFalse(messages.isEmpty());
57
58         int msgLength = msgFactory.put(this.msg).length;
59         int chunkCount = msgLength / NetconfMessageFactory.MAX_CHUNK_SIZE;
60         if ((msgLength % NetconfMessageFactory.MAX_CHUNK_SIZE) != 0) {
61             chunkCount++;
62         }
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,
70                         eom);
71                 assertArrayEquals(NetconfMessageFactory.endOfChunk, eom);
72             }
73
74             byte[] header = new byte[String.valueOf(exptHeaderLength).length() + NetconfMessageHeader.MIN_HEADER_LENGTH
75                     - 1];
76             recievedOutbound.getBytes(0, header);
77             NetconfMessageHeader messageHeader = new NetconfMessageHeader();
78             messageHeader.fromBytes(header);
79             assertEquals(exptHeaderLength, messageHeader.getLength());
80
81             testChunkChannel.writeInbound(recievedOutbound);
82         }
83         assertTrue(messages.isEmpty());
84
85         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
86         assertNotNull(receivedMessage);
87         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
88     }
89
90     @Test
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));
96
97         testChunkChannel.writeOutbound(this.msg);
98         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
99
100         byte[] eom = new byte[NetconfMessageFactory.endOfMessage.length];
101         recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageFactory.endOfMessage.length, eom);
102         assertArrayEquals(NetconfMessageFactory.endOfMessage, eom);
103
104         testChunkChannel.writeInbound(recievedOutbound);
105         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
106         assertNotNull(receivedMessage);
107         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
108     }
109 }