X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FNormalizedNodeStreamReaderWriterTest.java;h=951170c7a1608a06cbc3f569b193264694f8d3f4;hp=e2ce60e007cd1b46cbcb72dc9722c4dcd7095815;hb=683170bee30d6938fbaac9ad422d664259fc1bba;hpb=2cf4749c41aa32c6b77064fc1ae0e231adc4a5f4 diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java index e2ce60e007..951170c7a1 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java @@ -5,17 +5,19 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.datastore.node.utils.stream; import static org.junit.Assert.assertEquals; -import com.google.common.base.Optional; +import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; @@ -25,25 +27,31 @@ import javax.xml.transform.stream.StreamResult; import org.apache.commons.lang.SerializationUtils; import org.junit.Assert; import org.junit.Test; -import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec; import org.opendaylight.controller.cluster.datastore.util.TestModel; +import org.opendaylight.yangtools.yang.common.Empty; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; -import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.w3c.dom.Element; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.w3c.dom.Node; import org.xml.sax.InputSource; @@ -52,41 +60,37 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testNormalizedNodeStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = createTestContainer(); - writer.writeNormalizedNode(testContainer); + nnout.writeNormalizedNode(testContainer); QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster"); QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor"); QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description"); - ContainerNode toasterNode = Builders.containerBuilder(). - withNodeIdentifier(new NodeIdentifier(toaster)). - withChild(ImmutableNodes.leafNode(darknessFactor, "1000")). - withChild(ImmutableNodes.leafNode(description, largeString(20))) - .build(); + ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster)) + .withChild(ImmutableNodes.leafNode(darknessFactor, "1000")) + .withChild(ImmutableNodes.leafNode(description, largeString(20))).build(); - ContainerNode toasterContainer = Builders.containerBuilder(). - withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)). - withChild(toasterNode).build(); - writer.writeNormalizedNode(toasterContainer); + ContainerNode toasterContainer = Builders.containerBuilder() + .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build(); + nnout.writeNormalizedNode(toasterContainer); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(1049619, bytes.length); - NormalizedNode node = reader.readNormalizedNode(); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + + NormalizedNode node = nnin.readNormalizedNode(); Assert.assertEquals(testContainer, node); - node = reader.readNormalizedNode(); + node = nnin.readNormalizedNode(); Assert.assertEquals(toasterContainer, node); - - writer.close(); } private static NormalizedNode createTestContainer() { - byte[] bytes1 = {1,2,3}; + byte[] bytes1 = {1, 2, 3}; LeafSetEntryNode entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build(); @@ -94,59 +98,58 @@ public class NormalizedNodeStreamReaderWriterTest { LeafSetEntryNode entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build(); - LeafSetEntryNode entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( - new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, null)).withValue(null).build(); - - return TestModel.createBaseTestContainerBuilder(). - withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier( - new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)). - withChild(entry1).withChild(entry2).withChild(entry3).build()). - withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})). - withChild(Builders.orderedMapBuilder(). - withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME)). - withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME, - TestModel.ID_QNAME, 11)).build()). - build(); + return TestModel.createBaseTestContainerBuilder() + .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier( + new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)) + .withChild(entry1).withChild(entry2).build()) + .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4})) + .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance())) + .withChild(Builders.orderedMapBuilder() + .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME)) + .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME, + TestModel.ID_QNAME, 11)).build()).build(); } @Test public void testYangInstanceIdentifierStreaming() throws IOException { - YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH). - node(TestModel.OUTER_LIST_QNAME).nodeWithKey( + YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH) + .node(TestModel.OUTER_LIST_QNAME).nodeWithKey( TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build(); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = - new NormalizedNodeOutputStreamWriter(ByteStreams.newDataOutput(byteArrayOutputStream)); - writer.writeYangInstanceIdentifier(path); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + nnout.writeYangInstanceIdentifier(path); - YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier(); - Assert.assertEquals(path, newPath); + final byte[] bytes = bos.toByteArray(); + assertEquals(139, bytes.length); - writer.close(); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + + YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier(); + Assert.assertEquals(path, newPath); } @Test public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput( + ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build(); writer.writeNormalizedNode(testContainer); - YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH). - node(TestModel.OUTER_LIST_QNAME).nodeWithKey( + YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH) + .node(TestModel.OUTER_LIST_QNAME).nodeWithKey( TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build(); writer.writeYangInstanceIdentifier(path); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(826, bytes.length); + + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); NormalizedNode node = reader.readNormalizedNode(); Assert.assertEquals(testContainer, node); @@ -157,22 +160,20 @@ public class NormalizedNodeStreamReaderWriterTest { writer.close(); } - @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000) + @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000) public void testInvalidNormalizedNodeStream() throws IOException { - byte[] protobufBytes = new NormalizedNodeToNodeCodec(null).encode( - TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray(); - - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(protobufBytes)); + byte[] invalidBytes = {1, 2, 3}; + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( + ByteStreams.newDataInput(invalidBytes)); reader.readNormalizedNode(); } - @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000) + @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000) public void testInvalidYangInstanceIdentifierStream() throws IOException { - byte[] protobufBytes = {1,2,3}; - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(protobufBytes)); + byte[] invalidBytes = {1,2,3}; + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( + ByteStreams.newDataInput(invalidBytes)); reader.readYangInstanceIdentifier(); } @@ -180,11 +181,11 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testWithSerializable() { NormalizedNode input = TestModel.createTestContainer(); - SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input ); - SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable); + SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input); + SampleNormalizedNodeSerializable clone = + (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable); Assert.assertEquals(input, clone.getInput()); - } @Test @@ -203,16 +204,17 @@ public class NormalizedNodeStreamReaderWriterTest { Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)) .withValue(new DOMSource(xmlNode)).build()).build(); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); + + nnout.writeNormalizedNode(anyXmlContainer); - writer.writeNormalizedNode(anyXmlContainer); + final byte[] bytes = bos.toByteArray(); + assertEquals(229, bytes.length); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); - ContainerNode deserialized = (ContainerNode)reader.readNormalizedNode(); + ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode(); Optional> child = deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME)); @@ -221,21 +223,112 @@ public class NormalizedNodeStreamReaderWriterTest { StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput); + transformer.transform(((DOMSourceAnyxmlNode)child.get()).getValue(), xmlOutput); assertEquals("XML", xml, xmlOutput.getWriter().toString()); - assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI()); + assertEquals("http://www.w3.org/TR/html4/", + ((DOMSourceAnyxmlNode)child.get()).getValue().getNode().getNamespaceURI()); + } - writer.close(); + @Test + public void testSchemaPathSerialization() throws IOException { + final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writeSchemaPath(expected); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(99, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + assertEquals(expected, nnin.readSchemaPath()); + } + + @Test + public void testWritePathArgument() throws IOException { + final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writePathArgument(expected); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(103, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + assertEquals(expected, nnin.readPathArgument()); + } + + /* + * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string) + * and an integer. + */ + @Test + public void testHugeEntries() throws IOException { + final CollectionNodeBuilder mapBuilder = Builders.mapBuilder() + .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)); + final DataContainerNodeBuilder entryBuilder = + Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42)); + + for (int i = 0; i < 100_000; ++i) { + final String key = "xyzzy" + i; + mapBuilder.addChild(entryBuilder + .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME, + TestModel.CHILD_NAME_QNAME, key)) + .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key)) + .build()); + } + + final MapNode expected = mapBuilder.build(); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writeNormalizedNode(expected); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(2_289_103, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + assertEquals(expected, nnin.readNormalizedNode()); + } + + @Test + public void testAugmentationIdentifier() throws IOException { + final List qnames = new ArrayList<>(); + for (int i = 0; i < 257; ++i) { + qnames.add(QName.create(TestModel.TEST_QNAME, "a" + i)); + } + + for (int i = 0; i < qnames.size(); ++i) { + final AugmentationIdentifier expected = AugmentationIdentifier.create( + ImmutableSet.copyOf(qnames.subList(0, i))); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + try (NormalizedNodeDataOutput nnout = + NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos), + NormalizedNodeStreamVersion.SODIUM_SR1)) { + nnout.writePathArgument(expected); + } + + final byte[] bytes = bos.toByteArray(); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + PathArgument arg = nnin.readPathArgument(); + assertEquals(expected, arg); + } } - private static String largeString(final int pow){ - String s = "X"; - for(int i=0;i