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