Fix FindBugs warnings in sal-remoterpc-connector and enable enforcement
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
index 5d780be641e2cce6d2b48a2628cd675f9a24c8ee..4824cd71cc3fe4a2b6324a2b0ae0ce2cd7b238ec 100644 (file)
@@ -8,29 +8,90 @@
 package org.opendaylight.controller.remote.rpc.messages;
 
 
+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 javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
 public class ExecuteRpc implements Serializable {
     private static final long serialVersionUID = 1128904894827335676L;
 
-    private final String inputCompositeNode;
+    @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;
 
-    public ExecuteRpc(final String inputCompositeNode, final QName rpc) {
-        Preconditions.checkNotNull(inputCompositeNode, "Composite Node input string should be present");
-        Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
+    private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
+        this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
+        this.inputNormalizedNode = inputNormalizedNode;
+    }
 
-        this.inputCompositeNode = inputCompositeNode;
-        this.rpc = rpc;
+    public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
+        return new ExecuteRpc(input, rpc.getType().getLastComponent());
     }
 
-    public String getInputCompositeNode() {
-        return inputCompositeNode;
+    @Nullable
+    public NormalizedNode<?, ?> getInputNormalizedNode() {
+        return inputNormalizedNode;
     }
 
+    @Nonnull
     public QName getRpc() {
         return rpc;
     }
+
+    private Object writeReplace() {
+        return new Proxy(this);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("rpc", rpc)
+                .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(ExecuteRpc executeRpc) {
+            this.executeRpc = executeRpc;
+        }
+
+        @Override
+        public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeObject(executeRpc.getRpc());
+            SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
+        }
+
+        @Override
+        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+            QName qname = (QName) in.readObject();
+            executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
+        }
+
+        private Object readResolve() {
+            return executeRpc;
+        }
+    }
 }