d1c0b066d7a0a5ef15399a0544deb380177b0b01
[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.ChunkedFramingMechanismEncoder;
26 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
27 import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
28 import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
29 import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
30 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
31 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
32 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
33 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
34 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
35
36 public class MessageParserTest {
37
38     private NetconfMessage msg;
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 NetconfMessageToXMLEncoder(),
50
51                 new NetconfChunkAggregator(),
52                 new NetconfXMLToMessageDecoder());
53
54         testChunkChannel.writeOutbound(this.msg);
55         Queue<Object> messages = testChunkChannel.outboundMessages();
56         assertFalse(messages.isEmpty());
57
58         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
59         final ByteBuf out = Unpooled.buffer();
60         enc.encode(null, msg, out);
61         int msgLength = out.readableBytes();
62
63         int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
64         if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
65             chunkCount++;
66         }
67         for (int i = 1; i <= chunkCount; i++) {
68             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
69             int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
70             if (i == chunkCount) {
71                 exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
72                 byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
73                 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
74                         eom);
75                 assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
76             }
77
78             byte[] header = new byte[String.valueOf(exptHeaderLength).length()
79                     + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
80             recievedOutbound.getBytes(0, header);
81             NetconfMessageHeader messageHeader = NetconfMessageHeader.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 NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
98
99         testChunkChannel.writeOutbound(this.msg);
100         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
101
102         byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
103         recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
104         assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
105
106         testChunkChannel.writeInbound(recievedOutbound);
107         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
108         assertNotNull(receivedMessage);
109         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
110     }
111 }