Bump versions 9.0.4-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / RpcResponse.java
index cbfecb1918abc4849a3606efa84f780399b16411..bb308203ddd789fcf783c7ab6e8e0abadd2ca63a 100644 (file)
@@ -7,18 +7,68 @@
  */
 package org.opendaylight.controller.remote.rpc.messages;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.InvalidObjectException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
+public class RpcResponse extends AbstractResponse<ContainerNode> {
+    private static final long serialVersionUID = -4211279498688989245L;
 
-import java.io.Serializable;
+    public RpcResponse(final @Nullable ContainerNode output) {
+        super(output);
+    }
 
-public class RpcResponse implements Serializable {
-  private String resultCompositeNode;
+    @Override
+    Object writeReplace() {
+        return new Proxy(this);
+    }
 
-  public RpcResponse(String resultCompositeNode) {
-    this.resultCompositeNode = resultCompositeNode;
-  }
+    static @Nullable ContainerNode unmaskContainer(final Optional<NormalizedNode> optNode)
+            throws InvalidObjectException {
+        if (optNode.isEmpty()) {
+            return null;
+        }
+        final var node = optNode.orElseThrow();
+        if (node instanceof ContainerNode container) {
+            return container;
+        }
+        throw new InvalidObjectException("Unexpected data " + node.contract().getSimpleName());
+    }
 
-  public String getResultCompositeNode() {
-    return resultCompositeNode;
-  }
+    private static class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        private RpcResponse rpcResponse;
+
+        // 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 RpcResponse rpcResponse) {
+            this.rpcResponse = rpcResponse;
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            SerializationUtils.writeNormalizedNode(out, rpcResponse.getOutput());
+        }
+
+        @Override
+        public void readExternal(final ObjectInput in) throws IOException {
+            rpcResponse = new RpcResponse(unmaskContainer(SerializationUtils.readNormalizedNode(in)));
+        }
+
+        private Object readResolve() {
+            return rpcResponse;
+        }
+    }
 }