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