Partially revert "Adjust for RPCService methods changing"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ActionResponse.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc.messages;
9
10 import static java.util.Objects.requireNonNull;
11
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;
25
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;
29
30     private final transient @NonNull ImmutableList<@NonNull RpcError> errors;
31
32     public ActionResponse(final @NonNull Optional<ContainerNode> output, @NonNull final Collection<RpcError> errors) {
33         super(output.orElse(null));
34         this.errors = ImmutableList.copyOf(errors);
35     }
36
37     public @NonNull ImmutableList<@NonNull RpcError> getErrors() {
38         return errors;
39     }
40
41     @Override
42     Object writeReplace() {
43         return new Proxy(this);
44     }
45
46     private static class Proxy implements Externalizable {
47         private static final long serialVersionUID = 1L;
48
49         private ActionResponse actionResponse;
50
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")
54         public Proxy() {
55         }
56
57         Proxy(final ActionResponse actionResponse) {
58             this.actionResponse = requireNonNull(actionResponse);
59         }
60
61         @Override
62         public void writeExternal(final ObjectOutput out) throws IOException {
63             out.writeObject(actionResponse.getErrors());
64             SerializationUtils.writeNormalizedNode(out, actionResponse.getOutput());
65         }
66
67         @Override
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);
73         }
74
75         private Object readResolve() {
76             return actionResponse;
77         }
78     }
79 }