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