BUG-7594: Expand NormalizedNodeData{Input,Output} to handle SchemaPath 56/50656/6
authorRobert Varga <rovarga@cisco.com>
Thu, 19 Jan 2017 10:26:11 +0000 (11:26 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Mon, 23 Jan 2017 11:53:11 +0000 (11:53 +0000)
These utility classes are already dealing with QNames, so it makes
sense to expand their capabilities to include SchemaPath serialization.

Change-Id: Ibcb931f78959eb57f834cd2892511c4963638caa
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeDataInput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeDataOutput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java

index 706665b..76b83b7 100644 (file)
@@ -32,6 +32,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -347,6 +348,18 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
         }
     }
 
+    @Override
+    public void writeSchemaPath(final SchemaPath path) throws IOException {
+        ensureHeaderWritten();
+        output.writeBoolean(path.isAbsolute());
+
+        final Collection<QName> qnames = path.getPath();
+        output.writeInt(qnames.size());
+        for (QName qname : qnames) {
+            writeQName(qname);
+        }
+    }
+
     @Override
     public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
         ensureHeaderWritten();
index a37d565..7d793b1 100644 (file)
@@ -13,9 +13,11 @@ import java.io.IOException;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
- * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s and {@link PathArgument}s.
+ * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
+ * and {@link SchemaPath}s.
  */
 @Beta
 public interface NormalizedNodeDataInput extends DataInput {
@@ -31,4 +33,6 @@ public interface NormalizedNodeDataInput extends DataInput {
     YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
 
     PathArgument readPathArgument() throws IOException;
+
+    SchemaPath readSchemaPath() throws IOException;
 }
index 0c34224..7c42da1 100644 (file)
@@ -13,9 +13,11 @@ import java.io.IOException;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
- * Interface for emitting {@link NormalizedNode}s, {@link YangInstanceIdentifier}s and {@link PathArgument}s.
+ * Interface for emitting {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
+ * and {@link SchemaPath}s.
  */
 @Beta
 public interface NormalizedNodeDataOutput extends AutoCloseable, DataOutput {
@@ -25,6 +27,8 @@ public interface NormalizedNodeDataOutput extends AutoCloseable, DataOutput {
 
     void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException;
 
+    void writeSchemaPath(SchemaPath path) throws IOException;
+
     @Override
     void close() throws IOException;
 }
index ec04356..831c516 100644 (file)
@@ -17,6 +17,7 @@ import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -39,6 +40,7 @@ import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
@@ -351,6 +353,20 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         return new String(bytes, StandardCharsets.UTF_8);
     }
 
+    @Override
+    public SchemaPath readSchemaPath() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+
+        final boolean absolute = input.readBoolean();
+        final int size = input.readInt();
+        final Collection<QName> qnames = new ArrayList<>(size);
+        for (int i = 0; i < size; ++i) {
+            qnames.add(readQName());
+        }
+
+        return SchemaPath.create(qnames, absolute);
+    }
+
     @Override
     public YangInstanceIdentifier readYangInstanceIdentifier() throws IOException {
         readSignatureMarkerAndVersionIfNeeded();
index 825e123..40795d6 100644 (file)
@@ -42,21 +42,20 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableCo
 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.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.w3c.dom.Node;
 import org.xml.sax.InputSource;
 
 public class NormalizedNodeStreamReaderWriterTest {
 
-    @SuppressWarnings("deprecation")
     @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");
@@ -67,18 +66,16 @@ public class NormalizedNodeStreamReaderWriterTest {
 
         ContainerNode toasterContainer = Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
-        writer.writeNormalizedNode(toasterContainer);
+        nnout.writeNormalizedNode(toasterContainer);
 
-        NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
-            ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
+        NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+            bos.toByteArray()));
 
-        NormalizedNode<?,?> node = reader.readNormalizedNode();
+        NormalizedNode<?,?> node = nnin.readNormalizedNode();
         Assert.assertEquals(testContainer, node);
 
-        node = reader.readNormalizedNode();
+        node = nnin.readNormalizedNode();
         Assert.assertEquals(toasterContainer, node);
-
-        writer.close();
     }
 
     private static NormalizedNode<?, ?> createTestContainer() {
@@ -104,25 +101,22 @@ public class NormalizedNodeStreamReaderWriterTest {
                               TestModel.ID_QNAME, 11)).build()).build();
     }
 
-    @SuppressWarnings("deprecation")
     @Test
     public void testYangInstanceIdentifierStreaming() throws IOException  {
         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);
+        NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+            bos.toByteArray()));
 
-        writer.close();
+        YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
+        Assert.assertEquals(path, newPath);
     }
 
     @Test
@@ -179,10 +173,8 @@ public class NormalizedNodeStreamReaderWriterTest {
                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
 
         Assert.assertEquals(input, clone.getInput());
-
     }
 
-    @SuppressWarnings("deprecation")
     @Test
     public void testAnyXmlStreaming() throws Exception {
         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
@@ -199,16 +191,15 @@ 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));
 
-        writer.writeNormalizedNode(anyXmlContainer);
+        nnout.writeNormalizedNode(anyXmlContainer);
 
-        NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
-                ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
+        NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+            bos.toByteArray()));
 
-        ContainerNode deserialized = (ContainerNode)reader.readNormalizedNode();
+        ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
 
         Optional<DataContainerChild<? extends PathArgument, ?>> child =
                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
@@ -221,8 +212,20 @@ public class NormalizedNodeStreamReaderWriterTest {
 
         assertEquals("XML", xml, xmlOutput.getWriter().toString());
         assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
+    }
 
-        writer.close();
+    @Test
+    public void testSchemaPathSerialization() throws Exception {
+        final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
+        nnout.writeSchemaPath(expected);
+
+        NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+            bos.toByteArray()));
+        SchemaPath actual = nnin.readSchemaPath();
+        assertEquals(expected, actual);
     }
 
     private static String largeString(final int pow) {

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.