Remove use of thread-local output
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
index 55f87851cc7b8168cbd627ad3d66768475f6ce91..e32ef3dc0df6c5bae9ffd808d7c3fe2494437e7d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,50 +7,50 @@
  */
 package org.opendaylight.controller.remote.rpc.messages;
 
+import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.io.Serializable;
-import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
-import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
-import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-/**
- * @author tony
- *
- */
-public class ExecuteRpc implements Serializable {
+public final class ExecuteRpc implements Serializable {
     private static final long serialVersionUID = 1128904894827335676L;
 
-    private final NormalizedNodeMessages.Node inputNormalizedNode;
+    @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
+            + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
+            + "aren't serialized. FindBugs does not recognize this.")
+    private final NormalizedNode<?, ?> inputNormalizedNode;
     private final QName rpc;
 
-    private ExecuteRpc(final NormalizedNodeMessages.Node inputNormalizedNode, final QName rpc) {
-        Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
-
+    private ExecuteRpc(final @Nullable NormalizedNode<?, ?> inputNormalizedNode, final @NonNull QName rpc) {
+        this.rpc = requireNonNull(rpc, "rpc Qname should not be null");
         this.inputNormalizedNode = inputNormalizedNode;
-        this.rpc = rpc;
     }
 
-    public NormalizedNodeMessages.Node getInputNormalizedNode() {
+    public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode<?, ?> input) {
+        return new ExecuteRpc(input, rpc.getType().getLastComponent());
+    }
+
+    public @Nullable NormalizedNode<?, ?> getInputNormalizedNode() {
         return inputNormalizedNode;
     }
 
-    public QName getRpc() {
+    public @NonNull QName getRpc() {
         return rpc;
     }
 
-    public static ExecuteRpc from(final DOMRpcIdentifier rpc, final NormalizedNode<?, ?> input) {
-        final Node serializedInput;
-        if(input != null) {
-            serializedInput = NormalizedNodeSerializer.serialize(input);
-        } else {
-            serializedInput = null;
-        }
-        return new ExecuteRpc(serializedInput, rpc.getType().getLastComponent());
+    private Object writeReplace() {
+        return new Proxy(this);
     }
 
     @Override
@@ -60,4 +60,37 @@ public class ExecuteRpc implements Serializable {
                 .add("normalizedNode", inputNormalizedNode)
                 .toString();
     }
+
+    private static class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        private ExecuteRpc executeRpc;
+
+        // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+        // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
+        public Proxy() {
+        }
+
+        Proxy(final ExecuteRpc executeRpc) {
+            this.executeRpc = executeRpc;
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            // FIXME: QName is a WritableObject
+            out.writeObject(executeRpc.getRpc());
+            SerializationUtils.writeNormalizedNode(out, executeRpc.getInputNormalizedNode());
+        }
+
+        @Override
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+            QName qname = (QName) in.readObject();
+            executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
+        }
+
+        private Object readResolve() {
+            return executeRpc;
+        }
+    }
 }