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.controller.cluster.datastore.node.utils.stream;
10 import static org.junit.Assert.assertEquals;
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;
58 public class NormalizedNodeStreamReaderWriterTest {
61 public void testNormalizedNodeStreaming() throws IOException {
63 ByteArrayOutputStream bos = new ByteArrayOutputStream();
64 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
66 NormalizedNode<?, ?> testContainer = createTestContainer();
67 nnout.writeNormalizedNode(testContainer);
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();
76 ContainerNode toasterContainer = Builders.containerBuilder()
77 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
78 nnout.writeNormalizedNode(toasterContainer);
80 final byte[] bytes = bos.toByteArray();
81 assertEquals(1049619, bytes.length);
83 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
85 NormalizedNode<?, ?> node = nnin.readNormalizedNode();
86 Assert.assertEquals(testContainer, node);
88 node = nnin.readNormalizedNode();
89 Assert.assertEquals(toasterContainer, node);
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();
98 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
99 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
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();
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();
119 ByteArrayOutputStream bos = new ByteArrayOutputStream();
120 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
122 nnout.writeYangInstanceIdentifier(path);
124 final byte[] bytes = bos.toByteArray();
125 assertEquals(139, bytes.length);
127 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
129 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
130 Assert.assertEquals(path, newPath);
134 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
136 ByteArrayOutputStream bos = new ByteArrayOutputStream();
137 NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
138 ByteStreams.newDataOutput(bos));
140 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
141 writer.writeNormalizedNode(testContainer);
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();
147 writer.writeYangInstanceIdentifier(path);
149 final byte[] bytes = bos.toByteArray();
150 assertEquals(826, bytes.length);
152 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
154 NormalizedNode<?,?> node = reader.readNormalizedNode();
155 Assert.assertEquals(testContainer, node);
157 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
158 Assert.assertEquals(path, newPath);
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));
169 reader.readNormalizedNode();
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));
178 reader.readYangInstanceIdentifier();
182 public void testWithSerializable() {
183 NormalizedNode<?, ?> input = TestModel.createTestContainer();
184 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
185 SampleNormalizedNodeSerializable clone =
186 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
188 Assert.assertEquals(input, clone.getInput());
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);
197 Node xmlNode = factory.newDocumentBuilder().parse(
198 new InputSource(new StringReader(xml))).getDocumentElement();
200 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
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();
207 ByteArrayOutputStream bos = new ByteArrayOutputStream();
208 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
210 nnout.writeNormalizedNode(anyXmlContainer);
212 final byte[] bytes = bos.toByteArray();
213 assertEquals(229, bytes.length);
215 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
217 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
219 Optional<DataContainerChild<? extends PathArgument, ?>> child =
220 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
221 assertEquals("AnyXml child present", true, child.isPresent());
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);
228 assertEquals("XML", xml, xmlOutput.getWriter().toString());
229 assertEquals("http://www.w3.org/TR/html4/",
230 ((DOMSourceAnyxmlNode)child.get()).getValue().getNode().getNamespaceURI());
234 public void testSchemaPathSerialization() throws IOException {
235 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
237 ByteArrayOutputStream bos = new ByteArrayOutputStream();
238 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
239 nnout.writeSchemaPath(expected);
242 final byte[] bytes = bos.toByteArray();
243 assertEquals(99, bytes.length);
245 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
246 assertEquals(expected, nnin.readSchemaPath());
250 public void testWritePathArgument() throws IOException {
251 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
253 ByteArrayOutputStream bos = new ByteArrayOutputStream();
255 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
256 nnout.writePathArgument(expected);
259 final byte[] bytes = bos.toByteArray();
260 assertEquals(103, bytes.length);
262 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
263 assertEquals(expected, nnin.readPathArgument());
267 * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
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));
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))
286 final MapNode expected = mapBuilder.build();
287 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
289 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
290 nnout.writeNormalizedNode(expected);
293 final byte[] bytes = bos.toByteArray();
294 assertEquals(2_289_103, bytes.length);
296 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
297 assertEquals(expected, nnin.readNormalizedNode());
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));
307 for (int i = 0; i < qnames.size(); ++i) {
308 final AugmentationIdentifier expected = AugmentationIdentifier.create(
309 ImmutableSet.copyOf(qnames.subList(0, i)));
311 ByteArrayOutputStream bos = new ByteArrayOutputStream();
313 try (NormalizedNodeDataOutput nnout =
314 NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos),
315 NormalizedNodeStreamVersion.SODIUM_SR1)) {
316 nnout.writePathArgument(expected);
319 final byte[] bytes = bos.toByteArray();
321 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
322 PathArgument arg = nnin.readPathArgument();
323 assertEquals(expected, arg);
327 private static String largeString(final int pow) {
328 StringBuilder sb = new StringBuilder("X");
329 for (int i = 0; i < pow; i++) {
332 return sb.toString();