Rename SODIUM versions to NEON_SR2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / DataTreeCandidateInputOutput.java
index b8cf731b9460be2d8444449c2eb57c910d16dfea..35d9998cd6cda84db2a6d9a7a41c1d729d823b1b 100644 (file)
@@ -8,16 +8,15 @@
 package org.opendaylight.controller.cluster.datastore.persisted;
 
 import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableList;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
-import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
 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.tree.DataTreeCandidate;
@@ -56,25 +55,25 @@ public final class DataTreeCandidateInputOutput {
         if (children.isEmpty()) {
             LOG.debug("Modified node {} does not have any children, not instantiating it", identifier);
             return null;
-        } else {
-            return ModifiedDataTreeCandidateNode.create(identifier, type, children);
         }
+
+        return ModifiedDataTreeCandidateNode.create(identifier, type, children);
     }
 
     private static Collection<DataTreeCandidateNode> readChildren(final NormalizedNodeDataInput in) throws IOException {
         final int size = in.readInt();
-        if (size != 0) {
-            final Collection<DataTreeCandidateNode> ret = new ArrayList<>(size);
-            for (int i = 0; i < size; ++i) {
-                final DataTreeCandidateNode child = readNode(in);
-                if (child != null) {
-                    ret.add(child);
-                }
+        if (size == 0) {
+            return ImmutableList.of();
+        }
+
+        final Collection<DataTreeCandidateNode> ret = new ArrayList<>(size);
+        for (int i = 0; i < size; ++i) {
+            final DataTreeCandidateNode child = readNode(in);
+            if (child != null) {
+                ret.add(child);
             }
-            return ret;
-        } else {
-            return Collections.emptyList();
         }
+        return ret;
     }
 
     private static DataTreeCandidateNode readNode(final NormalizedNodeDataInput in) throws IOException {
@@ -91,14 +90,14 @@ public final class DataTreeCandidateInputOutput {
             case UNMODIFIED:
                 return null;
             case WRITE:
-                return DataTreeCandidateNodes.fromNormalizedNode(in.readNormalizedNode());
+                return DataTreeCandidateNodes.written(in.readNormalizedNode());
             default:
                 throw new IllegalArgumentException("Unhandled node type " + type);
         }
     }
 
     public static DataTreeCandidate readDataTreeCandidate(final DataInput in) throws IOException {
-        final NormalizedNodeDataInput reader = new NormalizedNodeInputStreamReader(in);
+        final NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(in);
         final YangInstanceIdentifier rootPath = reader.readYangInstanceIdentifier();
         final byte type = reader.readByte();
 
@@ -118,7 +117,7 @@ public final class DataTreeCandidateInputOutput {
                         readChildren(reader));
                 break;
             case WRITE:
-                rootNode = DataTreeCandidateNodes.fromNormalizedNode(reader.readNormalizedNode());
+                rootNode = DataTreeCandidateNodes.written(reader.readNormalizedNode());
                 break;
             case UNMODIFIED:
                 rootNode = AbstractDataTreeCandidateNode.createUnmodified();
@@ -130,7 +129,6 @@ public final class DataTreeCandidateInputOutput {
         return DataTreeCandidates.newDataTreeCandidate(rootPath, rootNode);
     }
 
-
     private static void writeChildren(final NormalizedNodeDataOutput out,
             final Collection<DataTreeCandidateNode> children) throws IOException {
         out.writeInt(children.size());
@@ -169,12 +167,14 @@ public final class DataTreeCandidateInputOutput {
                 out.writeByte(UNMODIFIED);
                 break;
             default:
-                throw new IllegalArgumentException("Unhandled node type " + node.getModificationType());
+                throwUnhandledNodeType(node);
         }
     }
 
-    public static void writeDataTreeCandidate(final DataOutput out, DataTreeCandidate candidate) throws IOException {
-        try (final NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(out)) {
+    public static void writeDataTreeCandidate(final DataOutput out, final DataTreeCandidate candidate)
+            throws IOException {
+        try (NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(out,
+                PayloadVersion.current().getStreamVersion())) {
             writer.writeYangInstanceIdentifier(candidate.getRootPath());
 
             final DataTreeCandidateNode node = candidate.getRootNode();
@@ -202,8 +202,12 @@ public final class DataTreeCandidateInputOutput {
                     writer.writeNormalizedNode(node.getDataAfter().get());
                     break;
                 default:
-                    throw new IllegalArgumentException("Unhandled node type " + node.getModificationType());
+                    throwUnhandledNodeType(node);
             }
         }
     }
+
+    private static void throwUnhandledNodeType(final DataTreeCandidateNode node) {
+        throw new IllegalArgumentException("Unhandled node type " + node.getModificationType());
+    }
 }