Use ReusableNormalizedNodeReceiver 65/84765/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Sep 2019 05:12:32 +0000 (07:12 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Oct 2019 10:19:14 +0000 (12:19 +0200)
This simplifies some APIs by using ReusableStreamReceiver, as it
is an API-level construct rather than a particular implementation.

Change-Id: Id9d14d512ac7150a3e71c104577546e219cd86d0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/cds-access-api/src/main/java/org/opendaylight/controller/cluster/access/commands/TransactionModification.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ForwardingNormalizedNodeDataInput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeDataInput.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/MergeModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/MutableCompositeModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/WriteModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/CommitTransactionPayload.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/DataTreeCandidateInputOutput.java

index e4168d5..a67886b 100644 (file)
@@ -15,13 +15,13 @@ import java.io.IOException;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 
 /**
  * An individual modification of a transaction's state. This class and its subclasses are not serializable, but rather
  * expose {@link #writeTo(NormalizedNodeDataOutput)} and
- * {@link #readFrom(NormalizedNodeDataInput, ReusableImmutableNormalizedNodeStreamWriter)} methods for explicit
- * serialization. The reason for this is that they are usually transmitted in bulk, hence it is advantageous to reuse
+ * {@link #readFrom(NormalizedNodeDataInput, ReusableStreamReceiver)} methods for explicit serialization. The reason for
+ * this is that they are usually transmitted in bulk, hence it is advantageous to reuse
  * a {@link NormalizedNodeDataOutput} instance to achieve better compression.
  *
  * @author Robert Varga
@@ -54,8 +54,8 @@ public abstract class TransactionModification {
         out.writeYangInstanceIdentifier(path);
     }
 
-    static TransactionModification readFrom(final NormalizedNodeDataInput in,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
+    static TransactionModification readFrom(final NormalizedNodeDataInput in, final ReusableStreamReceiver writer)
+            throws IOException {
         final byte type = in.readByte();
         switch (type) {
             case TYPE_DELETE:
index a1a0d69..72073c6 100644 (file)
@@ -14,7 +14,7 @@ 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.data.api.schema.stream.NormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 abstract class ForwardingNormalizedNodeDataInput extends ForwardingDataInput implements NormalizedNodeDataInput {
@@ -33,9 +33,8 @@ abstract class ForwardingNormalizedNodeDataInput extends ForwardingDataInput imp
     }
 
     @Override
-    public final NormalizedNode<?, ?> readNormalizedNode(final ReusableImmutableNormalizedNodeStreamWriter writer)
-            throws IOException {
-        return delegate().readNormalizedNode(writer);
+    public final NormalizedNode<?, ?> readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
+        return delegate().readNormalizedNode(receiver);
     }
 
     @Override
index 57d41d5..8d05061 100644 (file)
@@ -17,8 +17,7 @@ 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.data.api.schema.stream.NormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
@@ -46,28 +45,23 @@ public interface NormalizedNodeDataInput extends DataInput {
      * @throws IllegalStateException if the dictionary has been detached
      */
     default NormalizedNode<?, ?> readNormalizedNode() throws IOException {
-        final NormalizedNodeResult result = new NormalizedNodeResult();
-        try (NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(result)) {
-            streamNormalizedNode(writer);
-        }
-        return result.getResult();
+        return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
     }
 
     /**
      * Read a normalized node from the reader, using specified writer to construct the result.
      *
-     * @param writer Reusable writer to
+     * @param receiver Reusable receiver to, expected to be reset
      * @return Next node from the stream, or null if end of stream has been reached.
      * @throws IOException if an error occurs
      * @throws IllegalStateException if the dictionary has been detached
      */
-    default NormalizedNode<?, ?> readNormalizedNode(final ReusableImmutableNormalizedNodeStreamWriter writer)
-            throws IOException {
+    default NormalizedNode<?, ?> readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
         try {
-            streamNormalizedNode(writer);
-            return writer.getResult();
+            streamNormalizedNode(receiver);
+            return receiver.getResult();
         } finally {
-            writer.reset();
+            receiver.reset();
         }
     }
 
index 7f2c3f2..b2007ed 100644 (file)
@@ -5,7 +5,6 @@
  * 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.modification;
 
 import java.io.IOException;
@@ -14,8 +13,8 @@ import org.opendaylight.controller.cluster.datastore.node.utils.stream.Normalize
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
-import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 
 /**
  * MergeModification stores all the parameters required to merge data into the specified path.
@@ -55,8 +54,8 @@ public class MergeModification extends WriteModification {
     }
 
     public static MergeModification fromStream(final NormalizedNodeDataInput in, final short version,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
-        final NormalizedNode<?, ?> node = in.readNormalizedNode(writer);
+            final ReusableStreamReceiver receiver) throws IOException {
+        final NormalizedNode<?, ?> node = in.readNormalizedNode(receiver);
         final YangInstanceIdentifier path = in.readYangInstanceIdentifier();
         return new MergeModification(version, path, node);
     }
index d4abc04..d974d7e 100644 (file)
@@ -22,6 +22,7 @@ import org.opendaylight.controller.cluster.datastore.node.utils.stream.Normalize
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 
@@ -92,18 +93,17 @@ public class MutableCompositeModification extends VersionedExternalizableMessage
         int size = in.readInt();
         if (size > 0) {
             final NormalizedNodeDataInput input = NormalizedNodeInputOutput.newDataInputWithoutValidation(in);
-            final ReusableImmutableNormalizedNodeStreamWriter writer =
-                    ReusableImmutableNormalizedNodeStreamWriter.create();
+            final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
 
             for (int i = 0; i < size; i++) {
                 byte type = in.readByte();
                 switch (type) {
                     case Modification.WRITE:
-                        modifications.add(WriteModification.fromStream(input, getVersion(), writer));
+                        modifications.add(WriteModification.fromStream(input, getVersion(), receiver));
                         break;
 
                     case Modification.MERGE:
-                        modifications.add(MergeModification.fromStream(input, getVersion(), writer));
+                        modifications.add(MergeModification.fromStream(input, getVersion(), receiver));
                         break;
 
                     case Modification.DELETE:
index 609503f..4746952 100644 (file)
@@ -18,8 +18,8 @@ import org.opendaylight.controller.cluster.datastore.node.utils.stream.Serializa
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
-import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 
 /**
  * WriteModification stores all the parameters required to write data to the specified path.
@@ -80,8 +80,8 @@ public class WriteModification extends AbstractModification {
     }
 
     public static WriteModification fromStream(final NormalizedNodeDataInput in, final short version,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
-        final NormalizedNode<?, ?> node = in.readNormalizedNode(writer);
+            final ReusableStreamReceiver receiver) throws IOException {
+        final NormalizedNode<?, ?> node = in.readNormalizedNode(receiver);
         final YangInstanceIdentifier path = in.readYangInstanceIdentifier();
         return new WriteModification(version, path, node);
     }
index aaa8641..fb66581 100644 (file)
@@ -23,6 +23,7 @@ import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Map.Entry;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 import org.slf4j.Logger;
@@ -102,11 +103,11 @@ public final class CommitTransactionPayload extends Payload implements Serializa
         return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
     }
 
-    public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
+    public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(final ReusableStreamReceiver receiver)
+            throws IOException {
         final DataInput in = ByteStreams.newDataInput(serialized);
         return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
-                DataTreeCandidateInputOutput.readDataTreeCandidate(in, writer));
+                DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
     }
 
     @Override
index 30bbca2..53894de 100644 (file)
@@ -19,12 +19,12 @@ import org.opendaylight.controller.cluster.datastore.node.utils.stream.Normalize
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
 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.stream.ReusableStreamReceiver;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
-import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,12 +48,10 @@ public final class DataTreeCandidateInputOutput {
         throw new UnsupportedOperationException();
     }
 
-    private static DataTreeCandidateNode readModifiedNode(final ModificationType type,
-            final NormalizedNodeDataInput in, final ReusableImmutableNormalizedNodeStreamWriter writer)
-                    throws IOException {
-
+    private static DataTreeCandidateNode readModifiedNode(final ModificationType type, final NormalizedNodeDataInput in,
+            final ReusableStreamReceiver receiver) throws IOException {
         final PathArgument identifier = in.readPathArgument();
-        final Collection<DataTreeCandidateNode> children = readChildren(in, writer);
+        final Collection<DataTreeCandidateNode> children = readChildren(in, receiver);
         if (children.isEmpty()) {
             LOG.debug("Modified node {} does not have any children, not instantiating it", identifier);
             return null;
@@ -63,7 +61,7 @@ public final class DataTreeCandidateInputOutput {
     }
 
     private static Collection<DataTreeCandidateNode> readChildren(final NormalizedNodeDataInput in,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
+            final ReusableStreamReceiver receiver) throws IOException {
         final int size = in.readInt();
         if (size == 0) {
             return ImmutableList.of();
@@ -71,7 +69,7 @@ public final class DataTreeCandidateInputOutput {
 
         final Collection<DataTreeCandidateNode> ret = new ArrayList<>(size);
         for (int i = 0; i < size; ++i) {
-            final DataTreeCandidateNode child = readNode(in, writer);
+            final DataTreeCandidateNode child = readNode(in, receiver);
             if (child != null) {
                 ret.add(child);
             }
@@ -80,28 +78,28 @@ public final class DataTreeCandidateInputOutput {
     }
 
     private static DataTreeCandidateNode readNode(final NormalizedNodeDataInput in,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
+            final ReusableStreamReceiver receiver) throws IOException {
         final byte type = in.readByte();
         switch (type) {
             case APPEARED:
-                return readModifiedNode(ModificationType.APPEARED, in, writer);
+                return readModifiedNode(ModificationType.APPEARED, in, receiver);
             case DELETE:
                 return DeletedDataTreeCandidateNode.create(in.readPathArgument());
             case DISAPPEARED:
-                return readModifiedNode(ModificationType.DISAPPEARED, in, writer);
+                return readModifiedNode(ModificationType.DISAPPEARED, in, receiver);
             case SUBTREE_MODIFIED:
-                return readModifiedNode(ModificationType.SUBTREE_MODIFIED, in, writer);
+                return readModifiedNode(ModificationType.SUBTREE_MODIFIED, in, receiver);
             case UNMODIFIED:
                 return null;
             case WRITE:
-                return DataTreeCandidateNodes.written(in.readNormalizedNode(writer));
+                return DataTreeCandidateNodes.written(in.readNormalizedNode(receiver));
             default:
                 throw new IllegalArgumentException("Unhandled node type " + type);
         }
     }
 
-    public static DataTreeCandidate readDataTreeCandidate(final DataInput in,
-            final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
+    public static DataTreeCandidate readDataTreeCandidate(final DataInput in, final ReusableStreamReceiver receiver)
+            throws IOException {
         final NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(in);
         final YangInstanceIdentifier rootPath = reader.readYangInstanceIdentifier();
         final byte type = reader.readByte();
@@ -110,21 +108,21 @@ public final class DataTreeCandidateInputOutput {
         switch (type) {
             case APPEARED:
                 rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.APPEARED,
-                    readChildren(reader, writer));
+                    readChildren(reader, receiver));
                 break;
             case DELETE:
                 rootNode = DeletedDataTreeCandidateNode.create();
                 break;
             case DISAPPEARED:
                 rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.DISAPPEARED,
-                    readChildren(reader, writer));
+                    readChildren(reader, receiver));
                 break;
             case SUBTREE_MODIFIED:
                 rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.SUBTREE_MODIFIED,
-                        readChildren(reader, writer));
+                        readChildren(reader, receiver));
                 break;
             case WRITE:
-                rootNode = DataTreeCandidateNodes.written(reader.readNormalizedNode(writer));
+                rootNode = DataTreeCandidateNodes.written(reader.readNormalizedNode(receiver));
                 break;
             case UNMODIFIED:
                 rootNode = AbstractDataTreeCandidateNode.createUnmodified();

©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.