Revert "Move SchemaNodeIdentifier to yang-common"
[yangtools.git] / codec / yang-data-codec-binfmt / src / test / java / org / opendaylight / yangtools / yang / data / codec / binfmt / 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.yangtools.yang.data.codec.binfmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.io.ByteStreams;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.io.Serializable;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.math.BigInteger;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.function.Function;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.Parameterized;
37 import org.junit.runners.Parameterized.Parameter;
38 import org.junit.runners.Parameterized.Parameters;
39 import org.opendaylight.yangtools.yang.common.Empty;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
51 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
58 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
62 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
64 import org.w3c.dom.Node;
65 import org.xml.sax.InputSource;
66
67 @RunWith(Parameterized.class)
68 public class NormalizedNodeStreamReaderWriterTest {
69     public enum Unsigned implements Function<String, Number> {
70         BIG_INTEGER {
71             @Override
72             public BigInteger apply(final String str) {
73                 return new BigInteger(str);
74             }
75         },
76         UINT64 {
77             @Override
78             public Uint64 apply(final String str) {
79                 return Uint64.valueOf(str);
80             }
81         };
82     }
83
84     @Parameters(name = "{0} {1}")
85     public static Iterable<Object[]> data() {
86         return List.of(
87             new Object[] { NormalizedNodeStreamVersion.LITHIUM,    Unsigned.BIG_INTEGER,
88                 1_050_286, 9_577_973, 171, 1_553, 103, 237,  98 },
89             new Object[] { NormalizedNodeStreamVersion.NEON_SR2,   Unsigned.BIG_INTEGER,
90                 1_049_950, 5_577_993, 161, 1_163, 105, 235, 100 },
91             new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.BIG_INTEGER,
92                 1_049_619, 2_289_103, 139,   826, 103, 229,  99 },
93             new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.UINT64,
94                 1_049_618, 2_289_103, 139,   825, 103, 229,  99 },
95             new Object[] { NormalizedNodeStreamVersion.MAGNESIUM,  Unsigned.UINT64,
96                 1_049_618, 2_289_103, 139,   825, 103, 229,  99 });
97     }
98
99     @Parameter(0)
100     public NormalizedNodeStreamVersion version;
101     @Parameter(1)
102     public Unsigned uint64;
103     @Parameter(2)
104     public int normalizedNodeStreamingSize;
105     @Parameter(3)
106     public int hugeEntriesSize;
107     @Parameter(4)
108     public int yiidStreamingSize;
109     @Parameter(5)
110     public int nnYiidStreamingSize;
111     @Parameter(6)
112     public int writePathArgumentSize;
113     @Parameter(7)
114     public int anyxmlStreamingSize;
115     @Parameter(8)
116     public int schemaPathSize;
117
118     @Test
119     public void testNormalizedNodeStreaming() throws IOException {
120
121         ByteArrayOutputStream bos = new ByteArrayOutputStream();
122         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
123
124         NormalizedNode testContainer = createTestContainer();
125         nnout.writeNormalizedNode(testContainer);
126
127         QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
128         QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
129         QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
130         ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
131                 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
132                 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
133
134         ContainerNode toasterContainer = Builders.containerBuilder()
135                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
136         nnout.writeNormalizedNode(toasterContainer);
137
138         final byte[] bytes = bos.toByteArray();
139         assertEquals(normalizedNodeStreamingSize, bytes.length);
140
141         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
142
143         assertEquals(testContainer, nnin.readNormalizedNode());
144         assertEquals(toasterContainer, nnin.readNormalizedNode());
145     }
146
147     private NormalizedNode createTestContainer() {
148         byte[] bytes1 = {1, 2, 3};
149         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
150                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
151
152         byte[] bytes2 = {};
153         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
154                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
155
156         return TestModel.createBaseTestContainerBuilder(uint64)
157                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
158                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
159                         .withChild(entry1).withChild(entry2).build())
160                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
161                 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
162                 .withChild(Builders.orderedMapBuilder()
163                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
164                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
165                               TestModel.ID_QNAME, 11)).build()).build();
166     }
167
168     @Test
169     public void testYangInstanceIdentifierStreaming() throws IOException  {
170         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
171                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
172                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
173
174         ByteArrayOutputStream bos = new ByteArrayOutputStream();
175         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
176
177         nnout.writeYangInstanceIdentifier(path);
178
179         final byte[] bytes = bos.toByteArray();
180         assertEquals(yiidStreamingSize, bytes.length);
181
182         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
183         assertEquals(path, nnin.readYangInstanceIdentifier());
184     }
185
186     @Test
187     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
188         final NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder(uint64).build();
189         final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
190             .node(TestModel.OUTER_LIST_QNAME)
191             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10)
192             .build();
193
194         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
195         try (NormalizedNodeDataOutput writer = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
196             writer.writeNormalizedNode(testContainer);
197             writer.writeYangInstanceIdentifier(path);
198         }
199
200         final byte[] bytes = bos.toByteArray();
201         assertEquals(nnYiidStreamingSize, bytes.length);
202
203         final NormalizedNodeDataInput reader = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
204         assertEquals(testContainer, reader.readNormalizedNode());
205         assertEquals(path, reader.readYangInstanceIdentifier());
206     }
207
208     @Test
209     public void testInvalidNormalizedNodeStream() throws IOException {
210         final InvalidNormalizedNodeStreamException ex = assertThrows(InvalidNormalizedNodeStreamException.class,
211             () -> NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(new byte[] { 1, 2, 3})));
212         assertEquals("Invalid signature marker: 1", ex.getMessage());
213     }
214
215     @Test
216     public void testWithSerializable() {
217         NormalizedNode input = TestModel.createTestContainer(uint64);
218         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(version, input);
219         SampleNormalizedNodeSerializable clone = clone(serializable);
220         assertEquals(input, clone.getInput());
221     }
222
223     @Test
224     public void testAnyXmlStreaming() throws Exception {
225         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
226         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
227         factory.setNamespaceAware(true);
228
229         Node xmlNode = factory.newDocumentBuilder().parse(
230                 new InputSource(new StringReader(xml))).getDocumentElement();
231
232         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
233
234         NormalizedNode anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
235                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
236                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
237                             .withValue(new DOMSource(xmlNode)).build()).build();
238
239         ByteArrayOutputStream bos = new ByteArrayOutputStream();
240         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
241
242         nnout.writeNormalizedNode(anyXmlContainer);
243
244         final byte[] bytes = bos.toByteArray();
245         assertEquals(anyxmlStreamingSize, bytes.length);
246
247         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
248
249         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
250
251         Optional<DataContainerChild> child = deserialized.findChildByArg(new NodeIdentifier(TestModel.ANY_XML_QNAME));
252         assertEquals("AnyXml child present", true, child.isPresent());
253
254         StreamResult xmlOutput = new StreamResult(new StringWriter());
255         Transformer transformer = TransformerFactory.newInstance().newTransformer();
256         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
257         transformer.transform(((DOMSourceAnyxmlNode)child.get()).body(), xmlOutput);
258
259         assertEquals("XML", xml, xmlOutput.getWriter().toString());
260         assertEquals("http://www.w3.org/TR/html4/",
261             ((DOMSourceAnyxmlNode)child.get()).body().getNode().getNamespaceURI());
262     }
263
264     @Test
265     public void testSchemaPathSerialization() throws IOException {
266         final Absolute expected = Absolute.of(TestModel.ANY_XML_QNAME);
267
268         ByteArrayOutputStream bos = new ByteArrayOutputStream();
269         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
270             nnout.writeSchemaNodeIdentifier(expected);
271         }
272
273         final byte[] bytes = bos.toByteArray();
274         assertEquals(schemaPathSize, bytes.length);
275
276         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
277         assertEquals(expected, nnin.readSchemaNodeIdentifier());
278     }
279
280     @Test
281     public void testWritePathArgument() throws IOException {
282         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
283
284         ByteArrayOutputStream bos = new ByteArrayOutputStream();
285
286         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
287             nnout.writePathArgument(expected);
288         }
289
290         final byte[] bytes = bos.toByteArray();
291         assertEquals(writePathArgumentSize, bytes.length);
292
293         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
294         assertEquals(expected, nnin.readPathArgument());
295     }
296
297     /*
298      * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
299      * and an integer.
300      */
301     @Test
302     public void testHugeEntries() throws IOException {
303         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapBuilder = Builders.mapBuilder()
304                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
305         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
306                 Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
307
308         for (int i = 0; i < 100_000; ++i) {
309             final String key = "xyzzy" + i;
310             mapBuilder.addChild(entryBuilder
311                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
312                     TestModel.CHILD_NAME_QNAME, key))
313                 .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
314                 .build());
315         }
316
317         final SystemMapNode expected = mapBuilder.build();
318         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
319
320         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
321             nnout.writeNormalizedNode(expected);
322         }
323
324         final byte[] bytes = bos.toByteArray();
325         assertEquals(hugeEntriesSize, bytes.length);
326
327         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
328         assertEquals(expected, nnin.readNormalizedNode());
329     }
330
331     @Test
332     public void testAugmentationIdentifier() throws IOException {
333         final List<QName> qnames = new ArrayList<>();
334         for (int i = 0; i < 257; ++i) {
335             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
336         }
337
338         for (int i = 0; i < qnames.size(); ++i) {
339             assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames.subList(0, i))));
340         }
341
342         for (int i = qnames.size(); i < 65536; ++i) {
343             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
344         }
345         assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames)));
346     }
347
348     private void assertAugmentationIdentifier(final AugmentationIdentifier expected) throws IOException {
349         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
350         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
351             nnout.writePathArgument(expected);
352         }
353
354         final byte[] bytes = bos.toByteArray();
355
356         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
357         PathArgument arg = nnin.readPathArgument();
358         assertEquals(expected, arg);
359     }
360
361     private static <T extends Serializable> T clone(final T obj) {
362         final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
363         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
364             oos.writeObject(obj);
365         } catch (IOException e) {
366             throw new AssertionError("Failed to serialize object", e);
367         }
368
369         final byte[] bytes = baos.toByteArray();
370         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
371             return (T) ois.readObject();
372         } catch (ClassNotFoundException | IOException e) {
373             throw new AssertionError("Failed to deserialize object", e);
374         }
375     }
376
377     private static String largeString(final int pow) {
378         StringBuilder sb = new StringBuilder("X");
379         for (int i = 0; i < pow; i++) {
380             sb.append(sb);
381         }
382         return sb.toString();
383     }
384 }