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 @Deprecated(forRemoval = true)
59 public class NormalizedNodeStreamReaderWriterTest {
62 public void testNormalizedNodeStreaming() throws IOException {
64 ByteArrayOutputStream bos = new ByteArrayOutputStream();
65 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
67 NormalizedNode<?, ?> testContainer = createTestContainer();
68 nnout.writeNormalizedNode(testContainer);
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();
77 ContainerNode toasterContainer = Builders.containerBuilder()
78 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
79 nnout.writeNormalizedNode(toasterContainer);
81 final byte[] bytes = bos.toByteArray();
82 assertEquals(1049618, bytes.length);
84 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
86 NormalizedNode<?, ?> node = nnin.readNormalizedNode();
87 Assert.assertEquals(testContainer, node);
89 node = nnin.readNormalizedNode();
90 Assert.assertEquals(toasterContainer, node);
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();
99 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
100 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
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();
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();
120 ByteArrayOutputStream bos = new ByteArrayOutputStream();
121 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
123 nnout.writeYangInstanceIdentifier(path);
125 final byte[] bytes = bos.toByteArray();
126 assertEquals(139, bytes.length);
128 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
130 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
131 Assert.assertEquals(path, newPath);
135 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
137 ByteArrayOutputStream bos = new ByteArrayOutputStream();
138 NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
139 ByteStreams.newDataOutput(bos));
141 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
142 writer.writeNormalizedNode(testContainer);
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();
148 writer.writeYangInstanceIdentifier(path);
150 final byte[] bytes = bos.toByteArray();
151 assertEquals(825, bytes.length);
153 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
155 NormalizedNode<?,?> node = reader.readNormalizedNode();
156 Assert.assertEquals(testContainer, node);
158 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
159 Assert.assertEquals(path, newPath);
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));
170 reader.readNormalizedNode();
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));
179 reader.readYangInstanceIdentifier();
183 public void testWithSerializable() {
184 NormalizedNode<?, ?> input = TestModel.createTestContainer();
185 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
186 SampleNormalizedNodeSerializable clone =
187 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
189 Assert.assertEquals(input, clone.getInput());
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);
198 Node xmlNode = factory.newDocumentBuilder().parse(
199 new InputSource(new StringReader(xml))).getDocumentElement();
201 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
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();
208 ByteArrayOutputStream bos = new ByteArrayOutputStream();
209 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
211 nnout.writeNormalizedNode(anyXmlContainer);
213 final byte[] bytes = bos.toByteArray();
214 assertEquals(229, bytes.length);
216 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
218 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
220 Optional<DataContainerChild<? extends PathArgument, ?>> child =
221 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
222 assertEquals("AnyXml child present", true, child.isPresent());
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);
229 assertEquals("XML", xml, xmlOutput.getWriter().toString());
230 assertEquals("http://www.w3.org/TR/html4/",
231 ((DOMSourceAnyxmlNode)child.get()).getValue().getNode().getNamespaceURI());
235 public void testSchemaPathSerialization() throws IOException {
236 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
238 ByteArrayOutputStream bos = new ByteArrayOutputStream();
239 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
240 nnout.writeSchemaPath(expected);
243 final byte[] bytes = bos.toByteArray();
244 assertEquals(99, bytes.length);
246 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
247 assertEquals(expected, nnin.readSchemaPath());
251 public void testWritePathArgument() throws IOException {
252 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
254 ByteArrayOutputStream bos = new ByteArrayOutputStream();
256 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
257 nnout.writePathArgument(expected);
260 final byte[] bytes = bos.toByteArray();
261 assertEquals(103, bytes.length);
263 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
264 assertEquals(expected, nnin.readPathArgument());
268 * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
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));
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))
287 final MapNode expected = mapBuilder.build();
288 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
290 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
291 nnout.writeNormalizedNode(expected);
294 final byte[] bytes = bos.toByteArray();
295 assertEquals(2_289_103, bytes.length);
297 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
298 assertEquals(expected, nnin.readNormalizedNode());
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));
308 for (int i = 0; i < qnames.size(); ++i) {
309 final AugmentationIdentifier expected = AugmentationIdentifier.create(
310 ImmutableSet.copyOf(qnames.subList(0, i)));
312 ByteArrayOutputStream bos = new ByteArrayOutputStream();
314 try (NormalizedNodeDataOutput nnout =
315 NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos),
316 NormalizedNodeStreamVersion.SODIUM_SR1)) {
317 nnout.writePathArgument(expected);
320 final byte[] bytes = bos.toByteArray();
322 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
323 PathArgument arg = nnin.readPathArgument();
324 assertEquals(expected, arg);
328 private static String largeString(final int pow) {
329 StringBuilder sb = new StringBuilder("X");
330 for (int i = 0; i < pow; i++) {
333 return sb.toString();