Use careful byte-masking/shifting in Mg Input
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeStreamReaderWriterTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.controller.cluster.datastore.node.utils.stream;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.io.ByteStreams;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Optional;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.transform.OutputKeys;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import org.apache.commons.lang.SerializationUtils;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.opendaylight.controller.cluster.datastore.util.TestModel;
31 import org.opendaylight.yangtools.yang.common.Empty;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
47 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 import org.w3c.dom.Node;
56 import org.xml.sax.InputSource;
57
58 public class NormalizedNodeStreamReaderWriterTest {
59
60     @Test
61     public void testNormalizedNodeStreaming() throws IOException {
62
63         ByteArrayOutputStream bos = new ByteArrayOutputStream();
64         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
65
66         NormalizedNode<?, ?> testContainer = createTestContainer();
67         nnout.writeNormalizedNode(testContainer);
68
69         QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
70         QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
71         QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
72         ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
73                 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
74                 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
75
76         ContainerNode toasterContainer = Builders.containerBuilder()
77                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
78         nnout.writeNormalizedNode(toasterContainer);
79
80         final byte[] bytes = bos.toByteArray();
81         assertEquals(1049619, bytes.length);
82
83         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
84
85         NormalizedNode<?, ?> node = nnin.readNormalizedNode();
86         Assert.assertEquals(testContainer, node);
87
88         node = nnin.readNormalizedNode();
89         Assert.assertEquals(toasterContainer, node);
90     }
91
92     private static NormalizedNode<?, ?> createTestContainer() {
93         byte[] bytes1 = {1, 2, 3};
94         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
95                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
96
97         byte[] bytes2 = {};
98         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
99                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
100
101         return TestModel.createBaseTestContainerBuilder()
102                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
103                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
104                         .withChild(entry1).withChild(entry2).build())
105                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
106                 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
107                 .withChild(Builders.orderedMapBuilder()
108                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
109                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
110                               TestModel.ID_QNAME, 11)).build()).build();
111     }
112
113     @Test
114     public void testYangInstanceIdentifierStreaming() throws IOException  {
115         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
116                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
117                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
118
119         ByteArrayOutputStream bos = new ByteArrayOutputStream();
120         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
121
122         nnout.writeYangInstanceIdentifier(path);
123
124         final byte[] bytes = bos.toByteArray();
125         assertEquals(139, bytes.length);
126
127         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
128
129         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
130         Assert.assertEquals(path, newPath);
131     }
132
133     @Test
134     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
135
136         ByteArrayOutputStream bos = new ByteArrayOutputStream();
137         NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
138             ByteStreams.newDataOutput(bos));
139
140         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
141         writer.writeNormalizedNode(testContainer);
142
143         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
144                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
145                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
146
147         writer.writeYangInstanceIdentifier(path);
148
149         final byte[] bytes = bos.toByteArray();
150         assertEquals(826, bytes.length);
151
152         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
153
154         NormalizedNode<?,?> node = reader.readNormalizedNode();
155         Assert.assertEquals(testContainer, node);
156
157         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
158         Assert.assertEquals(path, newPath);
159
160         writer.close();
161     }
162
163     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
164     public void testInvalidNormalizedNodeStream() throws IOException {
165         byte[] invalidBytes = {1, 2, 3};
166         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
167                 ByteStreams.newDataInput(invalidBytes));
168
169         reader.readNormalizedNode();
170     }
171
172     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
173     public void testInvalidYangInstanceIdentifierStream() throws IOException {
174         byte[] invalidBytes = {1,2,3};
175         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
176             ByteStreams.newDataInput(invalidBytes));
177
178         reader.readYangInstanceIdentifier();
179     }
180
181     @Test
182     public void testWithSerializable() {
183         NormalizedNode<?, ?> input = TestModel.createTestContainer();
184         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
185         SampleNormalizedNodeSerializable clone =
186                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
187
188         Assert.assertEquals(input, clone.getInput());
189     }
190
191     @Test
192     public void testAnyXmlStreaming() throws Exception {
193         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
194         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
195         factory.setNamespaceAware(true);
196
197         Node xmlNode = factory.newDocumentBuilder().parse(
198                 new InputSource(new StringReader(xml))).getDocumentElement();
199
200         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
201
202         NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
203                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
204                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
205                             .withValue(new DOMSource(xmlNode)).build()).build();
206
207         ByteArrayOutputStream bos = new ByteArrayOutputStream();
208         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
209
210         nnout.writeNormalizedNode(anyXmlContainer);
211
212         final byte[] bytes = bos.toByteArray();
213         assertEquals(229, bytes.length);
214
215         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
216
217         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
218
219         Optional<DataContainerChild<? extends PathArgument, ?>> child =
220                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
221         assertEquals("AnyXml child present", true, child.isPresent());
222
223         StreamResult xmlOutput = new StreamResult(new StringWriter());
224         Transformer transformer = TransformerFactory.newInstance().newTransformer();
225         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
226         transformer.transform(((DOMSourceAnyxmlNode)child.get()).getValue(), xmlOutput);
227
228         assertEquals("XML", xml, xmlOutput.getWriter().toString());
229         assertEquals("http://www.w3.org/TR/html4/",
230             ((DOMSourceAnyxmlNode)child.get()).getValue().getNode().getNamespaceURI());
231     }
232
233     @Test
234     public void testSchemaPathSerialization() throws IOException {
235         final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
236
237         ByteArrayOutputStream bos = new ByteArrayOutputStream();
238         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
239             nnout.writeSchemaPath(expected);
240         }
241
242         final byte[] bytes = bos.toByteArray();
243         assertEquals(99, bytes.length);
244
245         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
246         assertEquals(expected, nnin.readSchemaPath());
247     }
248
249     @Test
250     public void testWritePathArgument() throws IOException {
251         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
252
253         ByteArrayOutputStream bos = new ByteArrayOutputStream();
254
255         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
256             nnout.writePathArgument(expected);
257         }
258
259         final byte[] bytes = bos.toByteArray();
260         assertEquals(103, bytes.length);
261
262         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
263         assertEquals(expected, nnin.readPathArgument());
264     }
265
266     /*
267      * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
268      * and an integer.
269      */
270     @Test
271     public void testHugeEntries() throws IOException {
272         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder()
273                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
274         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
275                 Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
276
277         for (int i = 0; i < 100_000; ++i) {
278             final String key = "xyzzy" + i;
279             mapBuilder.addChild(entryBuilder
280                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
281                     TestModel.CHILD_NAME_QNAME, key))
282                 .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
283                 .build());
284         }
285
286         final MapNode expected = mapBuilder.build();
287         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
288
289         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
290             nnout.writeNormalizedNode(expected);
291         }
292
293         final byte[] bytes = bos.toByteArray();
294         assertEquals(2_289_103, bytes.length);
295
296         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
297         assertEquals(expected, nnin.readNormalizedNode());
298     }
299
300     @Test
301     public void testAugmentationIdentifier() throws IOException {
302         final List<QName> qnames = new ArrayList<>();
303         for (int i = 0; i < 257; ++i) {
304             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + i));
305         }
306
307         for (int i = 0; i < qnames.size(); ++i) {
308             final AugmentationIdentifier expected = AugmentationIdentifier.create(
309                 ImmutableSet.copyOf(qnames.subList(0, i)));
310
311             ByteArrayOutputStream bos = new ByteArrayOutputStream();
312
313             try (NormalizedNodeDataOutput nnout =
314                     NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos),
315                         NormalizedNodeStreamVersion.SODIUM_SR1)) {
316                 nnout.writePathArgument(expected);
317             }
318
319             final byte[] bytes = bos.toByteArray();
320
321             NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
322             PathArgument arg = nnin.readPathArgument();
323             assertEquals(expected, arg);
324         }
325     }
326
327     private static String largeString(final int pow) {
328         StringBuilder sb = new StringBuilder("X");
329         for (int i = 0; i < pow; i++) {
330             sb.append(sb);
331         }
332         return sb.toString();
333     }
334 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.