2 * Copyright (c) 2019 Nordix Foundation. 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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.Collection;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 @SuppressFBWarnings({"SE_TRANSIENT_FIELD_NOT_RESTORED", "DMI_NONSERIALIZABLE_OBJECT_WRITTEN"})
27 public class ActionResponse extends AbstractResponse<ContainerNode> {
28 private static final long serialVersionUID = 1L;
30 private final transient @NonNull ImmutableList<@NonNull RpcError> errors;
32 public ActionResponse(final @NonNull Optional<ContainerNode> output, @NonNull final Collection<RpcError> errors) {
33 super(output.orElse(null));
34 this.errors = ImmutableList.copyOf(errors);
37 public @NonNull ImmutableList<@NonNull RpcError> getErrors() {
42 Object writeReplace() {
43 return new Proxy(this);
46 private static class Proxy implements Externalizable {
47 private static final long serialVersionUID = 1L;
49 private ActionResponse actionResponse;
51 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
52 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
53 @SuppressWarnings("checkstyle:RedundantModifier")
57 Proxy(final ActionResponse actionResponse) {
58 this.actionResponse = requireNonNull(actionResponse);
62 public void writeExternal(final ObjectOutput out) throws IOException {
63 out.writeObject(actionResponse.getErrors());
64 SerializationUtils.writeNormalizedNode(out, actionResponse.getOutput());
68 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
69 @SuppressWarnings("unchecked")
70 final ImmutableList<RpcError> errors = (ImmutableList<RpcError>) in.readObject();
71 final Optional<NormalizedNode<?, ?>> output = SerializationUtils.readNormalizedNode(in);
72 actionResponse = new ActionResponse(output.map(ContainerNode.class::cast), errors);
75 private Object readResolve() {
76 return actionResponse;