2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.remote.rpc.messages;
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.InvalidObjectException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 public class RpcResponse extends AbstractResponse<ContainerNode> {
22 private static final long serialVersionUID = -4211279498688989245L;
24 public RpcResponse(final @Nullable ContainerNode output) {
29 Object writeReplace() {
30 return new Proxy(this);
33 static @Nullable ContainerNode unmaskContainer(final Optional<NormalizedNode> optNode)
34 throws InvalidObjectException {
35 if (optNode.isEmpty()) {
38 final var node = optNode.orElseThrow();
39 if (node instanceof ContainerNode container) {
42 throw new InvalidObjectException("Unexpected data " + node.contract().getSimpleName());
45 private static class Proxy implements Externalizable {
46 private static final long serialVersionUID = 1L;
48 private RpcResponse rpcResponse;
50 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
51 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
52 @SuppressWarnings("checkstyle:RedundantModifier")
56 Proxy(final RpcResponse rpcResponse) {
57 this.rpcResponse = rpcResponse;
61 public void writeExternal(final ObjectOutput out) throws IOException {
62 SerializationUtils.writeNormalizedNode(out, rpcResponse.getOutput());
66 public void readExternal(final ObjectInput in) throws IOException {
67 rpcResponse = new RpcResponse(unmaskContainer(SerializationUtils.readNormalizedNode(in)));
70 private Object readResolve() {