Merge "Fix warnings reported in toaster"
[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 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import io.netty.channel.embedded.EmbeddedChannel;
19
20 import java.util.Queue;
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.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;
34
35 public class MessageParserTest {
36
37     private NetconfMessage msg;
38
39     @Before
40     public void setUp() throws Exception {
41         this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
42     }
43
44     @Test
45     public void testChunkedFramingMechanismOnPipeline() throws Exception {
46         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
47                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
48                 new NetconfMessageToXMLEncoder(),
49
50                 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
51                 new NetconfXMLToMessageDecoder());
52
53         testChunkChannel.writeOutbound(this.msg);
54         Queue<Object> messages = testChunkChannel.outboundMessages();
55         assertFalse(messages.isEmpty());
56
57         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
58         final ByteBuf out = Unpooled.buffer();
59         enc.encode(null, msg, out);
60         int msgLength = out.readableBytes();
61
62         int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
63         if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
64             chunkCount++;
65         }
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,
73                         eom);
74                 assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
75             }
76
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());
83
84             testChunkChannel.writeInbound(recievedOutbound);
85         }
86         assertTrue(messages.isEmpty());
87
88         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
89         assertNotNull(receivedMessage);
90         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
91     }
92
93     @Test
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());
99
100         testChunkChannel.writeOutbound(this.msg);
101         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
102
103         byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
104         recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
105         assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
106
107         testChunkChannel.writeInbound(recievedOutbound);
108         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
109         assertNotNull(receivedMessage);
110         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
111     }
112 }