Reduce JSR305 proliferation
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / RpcResponse.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Externalizable;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.io.Serializable;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 public class RpcResponse implements Serializable {
20     private static final long serialVersionUID = -4211279498688989245L;
21
22     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
23             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
24             + "aren't serialized. FindBugs does not recognize this.")
25     private final NormalizedNode<?, ?> resultNormalizedNode;
26
27     public RpcResponse(final @Nullable NormalizedNode<?, ?> inputNormalizedNode) {
28         resultNormalizedNode = inputNormalizedNode;
29     }
30
31     public @Nullable NormalizedNode<?, ?> getResultNormalizedNode() {
32         return resultNormalizedNode;
33     }
34
35     private Object writeReplace() {
36         return new Proxy(this);
37     }
38
39     private static class Proxy implements Externalizable {
40         private static final long serialVersionUID = 1L;
41
42         private RpcResponse rpcResponse;
43
44         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
45         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
46         @SuppressWarnings("checkstyle:RedundantModifier")
47         public Proxy() {
48         }
49
50         Proxy(RpcResponse rpcResponse) {
51             this.rpcResponse = rpcResponse;
52         }
53
54         @Override
55         public void writeExternal(ObjectOutput out) {
56             SerializationUtils.serializeNormalizedNode(rpcResponse.getResultNormalizedNode(), out);
57         }
58
59         @Override
60         public void readExternal(ObjectInput in) {
61             rpcResponse = new RpcResponse(SerializationUtils.deserializeNormalizedNode(in));
62         }
63
64         private Object readResolve() {
65             return rpcResponse;
66         }
67     }
68 }