Teach sal-remoterpc-connector to route actions
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
index a5300a2722d27cfec820de6557b2bf94455a4edc..cfa0c8964f93d978043d30e3078619bf1fb94783 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,59 +7,39 @@
  */
 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 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.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+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.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
-public class ExecuteRpc implements Serializable {
+public final class ExecuteRpc extends AbstractExecute<@Nullable NormalizedNode<?, ?>> {
     private static final long serialVersionUID = 1128904894827335676L;
 
-    private final NormalizedNode<?, ?> inputNormalizedNode;
-    private final QName rpc;
-
-    private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
-        this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
-        this.inputNormalizedNode = inputNormalizedNode;
-    }
-
-    public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
-        return new ExecuteRpc(input, rpc.getType().getLastComponent());
-    }
-
-    @Nullable
-    public NormalizedNode<?, ?> getInputNormalizedNode() {
-        return inputNormalizedNode;
+    private ExecuteRpc(final @NonNull SchemaPath type, final @Nullable NormalizedNode<?, ?> input) {
+        super(type, input);
     }
 
-    @Nonnull
-    public QName getRpc() {
-        return rpc;
-    }
-
-    private Object writeReplace() {
-        return new Proxy(this);
+    public static @NonNull ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc,
+            final @Nullable NormalizedNode<?, ?> input) {
+        return new ExecuteRpc(rpc.getType(), input);
     }
 
     @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(this)
-                .add("rpc", rpc)
-                .add("normalizedNode", inputNormalizedNode)
-                .toString();
+    Object writeReplace() {
+        return new Proxy(this);
     }
 
-    private static class Proxy implements Externalizable {
+    private static final class Proxy implements Externalizable {
         private static final long serialVersionUID = 1L;
 
         private ExecuteRpc executeRpc;
@@ -68,22 +48,27 @@ public class ExecuteRpc implements Serializable {
         // 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;
+        Proxy(final ExecuteRpc executeRpc) {
+            this.executeRpc = requireNonNull(executeRpc);
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
-            out.writeObject(executeRpc.getRpc());
-            SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
+                stream.writeQName(executeRpc.getType().getLastComponent());
+                stream.writeOptionalNormalizedNode(executeRpc.getInput());
+            }
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-            QName qname = (QName) in.readObject();
-            executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
+        public void readExternal(final ObjectInput in) throws IOException {
+            final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
+            final SchemaPath type = SchemaPath.ROOT.createChild(stream.readQName());
+            final NormalizedNode<?, ?> input = stream.readOptionalNormalizedNode().orElse(null);
+            executeRpc = new ExecuteRpc(type, input);
         }
 
         private Object readResolve() {