Remove use of thread-local input 91/82391/9
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 3 Jun 2019 19:37:43 +0000 (21:37 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 7 Jun 2019 07:40:39 +0000 (07:40 +0000)
All callers are only called from top-level constructs, thus
not having a relationship with the context in which they are
deserialized.

JIRA: CONTROLLER-1888
Change-Id: I856f9109480fb83475a20ce4430d02a1dd055522
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/AbstractRead.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadDataReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/RegisterDataTreeChangeListener.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/WriteModification.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/MetadataShardDataTreeSnapshot.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/RpcResponse.java

index 4d01702..076e96c 100644 (file)
@@ -7,11 +7,10 @@
  */
 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
-import java.io.ByteArrayInputStream;
 import java.io.DataInput;
-import java.io.DataInputStream;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -23,59 +22,20 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
  * @author Thomas Pantelis
  */
 public final class SerializationUtils {
-    public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
-
     private SerializationUtils() {
+
     }
 
+    @FunctionalInterface
     public interface Applier<T> {
         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
     }
 
-    private static NormalizedNodeDataInput streamReader(final DataInput in) throws IOException {
-        NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
-        if (streamReader == null) {
-            streamReader = NormalizedNodeInputOutput.newDataInput(in);
-        }
-
-        return streamReader;
-    }
-
-    public static <T> void deserializePathAndNode(final DataInput in, final T instance, final Applier<T> applier) {
-        try {
-            NormalizedNodeDataInput streamReader = streamReader(in);
-            NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
-            YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
-            applier.apply(instance, path, node);
-        } catch (IOException e) {
-            throw new IllegalArgumentException("Error deserializing path and Node", e);
-        }
-    }
-
-    private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(final DataInput in) throws IOException {
-        boolean present = in.readBoolean();
-        if (present) {
-            NormalizedNodeDataInput streamReader = streamReader(in);
-            return streamReader.readNormalizedNode();
-        }
-
-        return null;
-    }
-
-    public static NormalizedNode<?, ?> deserializeNormalizedNode(final DataInput in) {
-        try {
-            return tryDeserializeNormalizedNode(in);
-        } catch (IOException e) {
-            throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
-        }
-    }
-
-    public static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) {
-        try {
-            return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
-        } catch (IOException e) {
-            throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
+    public static Optional<NormalizedNode<?, ?>> readNormalizedNode(final DataInput in) throws IOException {
+        if (!in.readBoolean()) {
+            return Optional.empty();
         }
+        return Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode());
     }
 
     public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
@@ -91,6 +51,10 @@ public final class SerializationUtils {
         }
     }
 
+    public static YangInstanceIdentifier readPath(final DataInput in) throws IOException {
+        return NormalizedNodeInputOutput.newDataInput(in).readYangInstanceIdentifier();
+    }
+
     public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
             throws IOException {
         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
@@ -98,6 +62,14 @@ public final class SerializationUtils {
         }
     }
 
+    public static <T> void readNodeAndPath(final DataInput in, final T instance, final Applier<T> applier)
+            throws IOException {
+        final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
+        NormalizedNode<?, ?> node = stream.readNormalizedNode();
+        YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
+        applier.apply(instance, path, node);
+    }
+
     public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
             final NormalizedNode<?, ?> node) throws IOException {
         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
@@ -106,6 +78,14 @@ public final class SerializationUtils {
         }
     }
 
+    public static <T> void readPathAndNode(final DataInput in, final T instance, final Applier<T> applier)
+            throws IOException {
+        final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
+        YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
+        NormalizedNode<?, ?> node = stream.readNormalizedNode();
+        applier.apply(instance, path, node);
+    }
+
     public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path,
             final NormalizedNode<?, ?> node) throws IOException {
         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
@@ -113,13 +93,4 @@ public final class SerializationUtils {
             stream.writeNormalizedNode(node);
         }
     }
-
-    public static YangInstanceIdentifier deserializePath(final DataInput in) {
-        try {
-            NormalizedNodeDataInput streamReader = streamReader(in);
-            return streamReader.readYangInstanceIdentifier();
-        } catch (IOException e) {
-            throw new IllegalArgumentException("Error deserializing path", e);
-        }
-    }
 }
index 50bfc09..1ddabe8 100644 (file)
@@ -51,7 +51,7 @@ public class SerializationUtilsTest {
     public void testSerializeDeserializeNodes() throws IOException {
         final NormalizedNode<?, ?> normalizedNode = createNormalizedNode();
         final byte[] bytes = serializeNormalizedNode(normalizedNode);
-        Assert.assertEquals(normalizedNode, SerializationUtils.deserializeNormalizedNode(bytes));
+        Assert.assertEquals(normalizedNode, deserializeNormalizedNode(bytes));
     }
 
     @Test
@@ -64,7 +64,7 @@ public class SerializationUtilsTest {
                 .withValue(new DOMSource(parse))
                 .build();
         final byte[] bytes = serializeNormalizedNode(anyXmlNode);
-        final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes);
+        final NormalizedNode<?, ?> deserialized = deserializeNormalizedNode(bytes);
         final DOMSource value = (DOMSource) deserialized.getValue();
         final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(),
                 value.getNode().getOwnerDocument());
@@ -83,7 +83,7 @@ public class SerializationUtilsTest {
                 .build();
         SerializationUtils.writePath(out, path);
         final YangInstanceIdentifier deserialized =
-                SerializationUtils.deserializePath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));
+                SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));
         Assert.assertEquals(path, deserialized);
     }
 
@@ -96,7 +96,7 @@ public class SerializationUtilsTest {
         SerializationUtils.writeNodeAndPath(out, path, node);
         final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
         final AtomicBoolean applierCalled = new AtomicBoolean(false);
-        SerializationUtils.deserializePathAndNode(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
+        SerializationUtils.readNodeAndPath(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
             Assert.assertEquals(path, deserializedPath);
             Assert.assertEquals(node, deserializedNode);
             applierCalled.set(true);
@@ -104,6 +104,10 @@ public class SerializationUtilsTest {
         Assert.assertTrue(applierCalled.get());
     }
 
+    private static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) throws IOException {
+        return SerializationUtils.readNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes))).get();
+    }
+
     private static byte[] serializeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         SerializationUtils.writeNormalizedNode(new DataOutputStream(bos), node);
index 5b92545..acd35d2 100644 (file)
@@ -41,9 +41,9 @@ public abstract class AbstractRead<T> extends VersionedExternalizableMessage {
     }
 
     @Override
-    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+    public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
-        path = SerializationUtils.deserializePath(in);
+        path = SerializationUtils.readPath(in);
     }
 
     @Override
index 93bf2ec..5c00cf1 100644 (file)
@@ -34,7 +34,7 @@ public class ReadDataReply extends VersionedExternalizableMessage {
     @Override
     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
-        normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
+        normalizedNode = SerializationUtils.readNormalizedNode(in).orElse(null);
     }
 
     @Override
index 83542a0..cbe9dbf 100644 (file)
@@ -62,7 +62,7 @@ public final class RegisterDataTreeChangeListener implements Externalizable {
     @Override
     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         dataTreeChangeListenerPath = (ActorRef) in.readObject();
-        path = SerializationUtils.deserializePath(in);
+        path = SerializationUtils.readPath(in);
         registerOnAllInstances = in.readBoolean();
     }
 
index d34a443..2307285 100644 (file)
@@ -57,8 +57,8 @@ public class DeleteModification extends AbstractModification {
     }
 
     @Override
-    public void readExternal(final ObjectInput in) {
-        setPath(SerializationUtils.deserializePath(in));
+    public void readExternal(final ObjectInput in) throws IOException {
+        setPath(SerializationUtils.readPath(in));
     }
 
     @Override
index 8261a5c..da1fb70 100644 (file)
@@ -15,7 +15,6 @@ import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 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.SerializationUtils;
-import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils.Applier;
 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;
@@ -67,8 +66,11 @@ public class WriteModification extends AbstractModification {
     }
 
     @Override
-    public void readExternal(final ObjectInput in) {
-        SerializationUtils.deserializePathAndNode(in, this, APPLIER);
+    public void readExternal(final ObjectInput in) throws IOException {
+        SerializationUtils.readNodeAndPath(in, this, (instance, path, node) -> {
+            instance.setPath(path);
+            instance.data = node;
+        });
     }
 
     @Override
@@ -89,9 +91,4 @@ public class WriteModification extends AbstractModification {
         out.writeNormalizedNode(data);
         out.writeYangInstanceIdentifier(getPath());
     }
-
-    private static final Applier<WriteModification> APPLIER = (instance, path, node) -> {
-        instance.setPath(path);
-        instance.data = node;
-    };
 }
index fa8877f..320ae61 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.controller.cluster.datastore.persisted;
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -80,7 +79,7 @@ public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardD
             }
 
             metadata = metaBuilder.build();
-            rootNode = Verify.verifyNotNull(SerializationUtils.deserializeNormalizedNode(in));
+            rootNode = SerializationUtils.readNormalizedNode(in).get();
         }
 
         private Object readResolve() {
index e32ef3d..23fdfdc 100644 (file)
@@ -85,8 +85,9 @@ public final class ExecuteRpc implements Serializable {
 
         @Override
         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+            // FIXME: QName is a WritableObject
             QName qname = (QName) in.readObject();
-            executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
+            executeRpc = new ExecuteRpc(SerializationUtils.readNormalizedNode(in).orElse(null), qname);
         }
 
         private Object readResolve() {
index 35e5611..6f1b44b 100644 (file)
@@ -58,8 +58,8 @@ public class RpcResponse implements Serializable {
         }
 
         @Override
-        public void readExternal(final ObjectInput in) {
-            rpcResponse = new RpcResponse(SerializationUtils.deserializeNormalizedNode(in));
+        public void readExternal(final ObjectInput in) throws IOException {
+            rpcResponse = new RpcResponse(SerializationUtils.readNormalizedNode(in).orElse(null));
         }
 
         private Object readResolve() {

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