Fix checkstyle reported by odlparent-3.0.0
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
1 /*
2  * Copyright (c) 2014, 2017 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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 public final class ExecuteRpc implements Serializable {
26     private static final long serialVersionUID = 1128904894827335676L;
27
28     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
29             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
30             + "aren't serialized. FindBugs does not recognize this.")
31     private final NormalizedNode<?, ?> inputNormalizedNode;
32     private final QName rpc;
33
34     private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
35         this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
36         this.inputNormalizedNode = inputNormalizedNode;
37     }
38
39     public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
40         return new ExecuteRpc(input, rpc.getType().getLastComponent());
41     }
42
43     @Nullable
44     public NormalizedNode<?, ?> getInputNormalizedNode() {
45         return inputNormalizedNode;
46     }
47
48     @Nonnull
49     public QName getRpc() {
50         return rpc;
51     }
52
53     private Object writeReplace() {
54         return new Proxy(this);
55     }
56
57     @Override
58     public String toString() {
59         return MoreObjects.toStringHelper(this)
60                 .add("rpc", rpc)
61                 .add("normalizedNode", inputNormalizedNode)
62                 .toString();
63     }
64
65     private static class Proxy implements Externalizable {
66         private static final long serialVersionUID = 1L;
67
68         private ExecuteRpc executeRpc;
69
70         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
71         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
72         @SuppressWarnings("checkstyle:RedundantModifier")
73         public Proxy() {
74         }
75
76         Proxy(ExecuteRpc executeRpc) {
77             this.executeRpc = executeRpc;
78         }
79
80         @Override
81         public void writeExternal(ObjectOutput out) throws IOException {
82             out.writeObject(executeRpc.getRpc());
83             SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
84         }
85
86         @Override
87         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
88             QName qname = (QName) in.readObject();
89             executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
90         }
91
92         private Object readResolve() {
93             return executeRpc;
94         }
95     }
96 }