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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import static org.junit.Assert.assertEquals;
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.Optional;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMSource;
24 import javax.xml.transform.stream.StreamResult;
25 import org.apache.commons.lang.SerializationUtils;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.datastore.util.TestModel;
29 import org.opendaylight.yangtools.yang.common.Empty;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
44 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
52 import org.w3c.dom.Node;
53 import org.xml.sax.InputSource;
55 public class NormalizedNodeStreamReaderWriterTest {
58 public void testNormalizedNodeStreaming() throws IOException {
60 ByteArrayOutputStream bos = new ByteArrayOutputStream();
61 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
63 NormalizedNode<?, ?> testContainer = createTestContainer();
64 nnout.writeNormalizedNode(testContainer);
66 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
67 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
68 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
69 ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
70 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
71 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
73 ContainerNode toasterContainer = Builders.containerBuilder()
74 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
75 nnout.writeNormalizedNode(toasterContainer);
77 final byte[] bytes = bos.toByteArray();
78 assertEquals(1049950, bytes.length);
80 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
82 NormalizedNode<?, ?> node = nnin.readNormalizedNode();
83 Assert.assertEquals(testContainer, node);
85 node = nnin.readNormalizedNode();
86 Assert.assertEquals(toasterContainer, node);
89 private static NormalizedNode<?, ?> createTestContainer() {
90 byte[] bytes1 = {1, 2, 3};
91 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
92 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
95 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
96 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
98 return TestModel.createBaseTestContainerBuilder()
99 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
100 new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
101 .withChild(entry1).withChild(entry2).build())
102 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
103 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
104 .withChild(Builders.orderedMapBuilder()
105 .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
106 .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
107 TestModel.ID_QNAME, 11)).build()).build();
111 public void testYangInstanceIdentifierStreaming() throws IOException {
112 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
113 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
114 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
116 ByteArrayOutputStream bos = new ByteArrayOutputStream();
117 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
119 nnout.writeYangInstanceIdentifier(path);
121 final byte[] bytes = bos.toByteArray();
122 assertEquals(161, bytes.length);
124 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
126 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
127 Assert.assertEquals(path, newPath);
131 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
133 ByteArrayOutputStream bos = new ByteArrayOutputStream();
134 NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
135 ByteStreams.newDataOutput(bos));
137 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
138 writer.writeNormalizedNode(testContainer);
140 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
141 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
142 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
144 writer.writeYangInstanceIdentifier(path);
146 final byte[] bytes = bos.toByteArray();
147 assertEquals(1163, bytes.length);
149 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
151 NormalizedNode<?,?> node = reader.readNormalizedNode();
152 Assert.assertEquals(testContainer, node);
154 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
155 Assert.assertEquals(path, newPath);
160 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
161 public void testInvalidNormalizedNodeStream() throws IOException {
162 byte[] invalidBytes = {1, 2, 3};
163 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
164 ByteStreams.newDataInput(invalidBytes));
166 reader.readNormalizedNode();
169 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
170 public void testInvalidYangInstanceIdentifierStream() throws IOException {
171 byte[] invalidBytes = {1,2,3};
172 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
173 ByteStreams.newDataInput(invalidBytes));
175 reader.readYangInstanceIdentifier();
179 public void testWithSerializable() {
180 NormalizedNode<?, ?> input = TestModel.createTestContainer();
181 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
182 SampleNormalizedNodeSerializable clone =
183 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
185 Assert.assertEquals(input, clone.getInput());
189 public void testAnyXmlStreaming() throws Exception {
190 String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
191 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
192 factory.setNamespaceAware(true);
194 Node xmlNode = factory.newDocumentBuilder().parse(
195 new InputSource(new StringReader(xml))).getDocumentElement();
197 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
199 NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
200 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
201 Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
202 .withValue(new DOMSource(xmlNode)).build()).build();
204 ByteArrayOutputStream bos = new ByteArrayOutputStream();
205 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
207 nnout.writeNormalizedNode(anyXmlContainer);
209 final byte[] bytes = bos.toByteArray();
210 assertEquals(235, bytes.length);
212 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
214 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
216 Optional<DataContainerChild<? extends PathArgument, ?>> child =
217 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
218 assertEquals("AnyXml child present", true, child.isPresent());
220 StreamResult xmlOutput = new StreamResult(new StringWriter());
221 Transformer transformer = TransformerFactory.newInstance().newTransformer();
222 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
223 transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
225 assertEquals("XML", xml, xmlOutput.getWriter().toString());
226 assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
230 public void testSchemaPathSerialization() throws IOException {
231 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
233 ByteArrayOutputStream bos = new ByteArrayOutputStream();
234 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
235 nnout.writeSchemaPath(expected);
238 final byte[] bytes = bos.toByteArray();
239 assertEquals(100, bytes.length);
241 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
242 assertEquals(expected, nnin.readSchemaPath());
246 public void testWritePathArgument() throws IOException {
247 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
249 ByteArrayOutputStream bos = new ByteArrayOutputStream();
251 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
252 nnout.writePathArgument(expected);
255 final byte[] bytes = bos.toByteArray();
256 assertEquals(105, bytes.length);
258 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
259 assertEquals(expected, nnin.readPathArgument());
263 * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
267 public void testHugeEntries() throws IOException {
268 final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder()
269 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
270 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
271 Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
273 for (int i = 0; i < 100_000; ++i) {
274 final String key = "xyzzy" + i;
275 mapBuilder.addChild(entryBuilder
276 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
277 TestModel.CHILD_NAME_QNAME, key))
278 .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
282 final MapNode expected = mapBuilder.build();
283 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
285 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
286 nnout.writeNormalizedNode(expected);
289 final byte[] bytes = bos.toByteArray();
290 assertEquals(5_577_993, bytes.length);
292 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
293 assertEquals(expected, nnin.readNormalizedNode());
296 private static String largeString(final int pow) {
297 StringBuilder sb = new StringBuilder("X");
298 for (int i = 0; i < pow; i++) {
301 return sb.toString();