2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.binfmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
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;
67 @RunWith(Parameterized.class)
68 public class NormalizedNodeStreamReaderWriterTest {
69 public enum Unsigned implements Function<String, Number> {
72 public BigInteger apply(final String str) {
73 return new BigInteger(str);
78 public Uint64 apply(final String str) {
79 return Uint64.valueOf(str);
84 @Parameters(name = "{0} {1}")
85 public static Iterable<Object[]> data() {
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 });
100 public NormalizedNodeStreamVersion version;
102 public Unsigned uint64;
104 public int normalizedNodeStreamingSize;
106 public int hugeEntriesSize;
108 public int yiidStreamingSize;
110 public int nnYiidStreamingSize;
112 public int writePathArgumentSize;
114 public int anyxmlStreamingSize;
116 public int schemaPathSize;
119 public void testNormalizedNodeStreaming() throws IOException {
121 ByteArrayOutputStream bos = new ByteArrayOutputStream();
122 NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
124 NormalizedNode testContainer = createTestContainer();
125 nnout.writeNormalizedNode(testContainer);
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();
134 ContainerNode toasterContainer = Builders.containerBuilder()
135 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
136 nnout.writeNormalizedNode(toasterContainer);
138 final byte[] bytes = bos.toByteArray();
139 assertEquals(normalizedNodeStreamingSize, bytes.length);
141 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
143 assertEquals(testContainer, nnin.readNormalizedNode());
144 assertEquals(toasterContainer, nnin.readNormalizedNode());
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();
153 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
154 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
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();
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();
174 ByteArrayOutputStream bos = new ByteArrayOutputStream();
175 NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
177 nnout.writeYangInstanceIdentifier(path);
179 final byte[] bytes = bos.toByteArray();
180 assertEquals(yiidStreamingSize, bytes.length);
182 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
183 assertEquals(path, nnin.readYangInstanceIdentifier());
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)
194 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
195 try (NormalizedNodeDataOutput writer = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
196 writer.writeNormalizedNode(testContainer);
197 writer.writeYangInstanceIdentifier(path);
200 final byte[] bytes = bos.toByteArray();
201 assertEquals(nnYiidStreamingSize, bytes.length);
203 final NormalizedNodeDataInput reader = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
204 assertEquals(testContainer, reader.readNormalizedNode());
205 assertEquals(path, reader.readYangInstanceIdentifier());
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());
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());
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);
229 Node xmlNode = factory.newDocumentBuilder().parse(
230 new InputSource(new StringReader(xml))).getDocumentElement();
232 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
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();
239 ByteArrayOutputStream bos = new ByteArrayOutputStream();
240 NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
242 nnout.writeNormalizedNode(anyXmlContainer);
244 final byte[] bytes = bos.toByteArray();
245 assertEquals(anyxmlStreamingSize, bytes.length);
247 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
249 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
251 Optional<DataContainerChild> child = deserialized.findChildByArg(new NodeIdentifier(TestModel.ANY_XML_QNAME));
252 assertEquals("AnyXml child present", true, child.isPresent());
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);
259 assertEquals("XML", xml, xmlOutput.getWriter().toString());
260 assertEquals("http://www.w3.org/TR/html4/",
261 ((DOMSourceAnyxmlNode)child.get()).body().getNode().getNamespaceURI());
265 public void testSchemaPathSerialization() throws IOException {
266 final Absolute expected = Absolute.of(TestModel.ANY_XML_QNAME);
268 ByteArrayOutputStream bos = new ByteArrayOutputStream();
269 try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
270 nnout.writeSchemaNodeIdentifier(expected);
273 final byte[] bytes = bos.toByteArray();
274 assertEquals(schemaPathSize, bytes.length);
276 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
277 assertEquals(expected, nnin.readSchemaNodeIdentifier());
281 public void testWritePathArgument() throws IOException {
282 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
284 ByteArrayOutputStream bos = new ByteArrayOutputStream();
286 try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
287 nnout.writePathArgument(expected);
290 final byte[] bytes = bos.toByteArray();
291 assertEquals(writePathArgumentSize, bytes.length);
293 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
294 assertEquals(expected, nnin.readPathArgument());
298 * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
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));
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))
317 final SystemMapNode expected = mapBuilder.build();
318 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
320 try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
321 nnout.writeNormalizedNode(expected);
324 final byte[] bytes = bos.toByteArray();
325 assertEquals(hugeEntriesSize, bytes.length);
327 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
328 assertEquals(expected, nnin.readNormalizedNode());
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)));
338 for (int i = 0; i < qnames.size(); ++i) {
339 assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames.subList(0, i))));
342 for (int i = qnames.size(); i < 65536; ++i) {
343 qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
345 assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames)));
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);
354 final byte[] bytes = bos.toByteArray();
356 NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
357 PathArgument arg = nnin.readPathArgument();
358 assertEquals(expected, arg);
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);
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);
377 private static String largeString(final int pow) {
378 StringBuilder sb = new StringBuilder("X");
379 for (int i = 0; i < pow; i++) {
382 return sb.toString();