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