82c42dcb29fa59c0c9022737f087f92878a9b185
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / 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.netconf.impl;
10
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.embedded.EmbeddedChannel;
21 import java.nio.ByteBuffer;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Queue;
24 import org.custommonkey.xmlunit.XMLUnit;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
29 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
33 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
34 import org.opendaylight.netconf.util.messages.FramingMechanism;
35 import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
36 import org.opendaylight.netconf.util.test.XmlFileLoader;
37
38 public class MessageParserTest {
39
40     private NetconfMessage msg;
41
42     @Before
43     public void setUp() throws Exception {
44         this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
45     }
46
47     @Test
48     public void testChunkedFramingMechanismOnPipeline() throws Exception {
49         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
50                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
51                 new NetconfMessageToXMLEncoder(),
52
53                 new NetconfChunkAggregator(),
54                 new NetconfXMLToMessageDecoder());
55
56         testChunkChannel.writeOutbound(this.msg);
57         Queue<Object> messages = testChunkChannel.outboundMessages();
58         assertFalse(messages.isEmpty());
59
60         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
61         final ByteBuf out = Unpooled.buffer();
62         enc.encode(null, msg, out);
63         int msgLength = out.readableBytes();
64
65         int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
66         if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
67             chunkCount++;
68         }
69
70         byte[] endOfChunkBytes = NetconfMessageConstants.END_OF_CHUNK.getBytes(StandardCharsets.UTF_8);
71         for (int i = 1; i <= chunkCount; i++) {
72             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
73             int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
74             if (i == chunkCount) {
75                 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
76                 byte[] eom = new byte[endOfChunkBytes.length];
77                 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
78                 assertArrayEquals(endOfChunkBytes, eom);
79             }
80
81             byte[] header = new byte[String.valueOf(exptHeaderLength).length()
82                     + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
83             recievedOutbound.getBytes(0, header);
84             assertEquals(exptHeaderLength, getHeaderLength(header));
85
86             testChunkChannel.writeInbound(recievedOutbound);
87         }
88         assertTrue(messages.isEmpty());
89
90         NetconfMessage receivedMessage = testChunkChannel.readInbound();
91         assertNotNull(receivedMessage);
92         XMLUnit.setIgnoreWhitespace(true);
93         assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
94     }
95
96     @Test
97     public void testEOMFramingMechanismOnPipeline() throws Exception {
98         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
99                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
100                 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
101
102         testChunkChannel.writeOutbound(this.msg);
103         ByteBuf recievedOutbound = testChunkChannel.readOutbound();
104
105         byte[] endOfMsgBytes = NetconfMessageConstants.END_OF_MESSAGE.getBytes(StandardCharsets.UTF_8);
106         byte[] eom = new byte[endOfMsgBytes.length];
107         recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
108         assertArrayEquals(endOfMsgBytes, eom);
109
110         testChunkChannel.writeInbound(recievedOutbound);
111         NetconfMessage receivedMessage = testChunkChannel.readInbound();
112         assertNotNull(receivedMessage);
113         XMLUnit.setIgnoreWhitespace(true);
114         assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
115     }
116
117     private static long getHeaderLength(final byte[] bytes) {
118         byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
119         return Long.parseLong(StandardCharsets.US_ASCII.decode(
120                 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());
121     }
122 }