Fix CS 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 / ExecuteRpc.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
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
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 class ExecuteRpc implements Serializable {
26     private static final long serialVersionUID = 1128904894827335676L;
27
28     private final NormalizedNode<?, ?> inputNormalizedNode;
29     private final QName rpc;
30
31     private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
32         this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
33         this.inputNormalizedNode = inputNormalizedNode;
34     }
35
36     public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
37         return new ExecuteRpc(input, rpc.getType().getLastComponent());
38     }
39
40     @Nullable
41     public NormalizedNode<?, ?> getInputNormalizedNode() {
42         return inputNormalizedNode;
43     }
44
45     @Nonnull
46     public QName getRpc() {
47         return rpc;
48     }
49
50     private Object writeReplace() {
51         return new Proxy(this);
52     }
53
54     @Override
55     public String toString() {
56         return MoreObjects.toStringHelper(this)
57                 .add("rpc", rpc)
58                 .add("normalizedNode", inputNormalizedNode)
59                 .toString();
60     }
61
62     private static class Proxy implements Externalizable {
63         private static final long serialVersionUID = 1L;
64
65         private ExecuteRpc executeRpc;
66
67         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
68         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
69         @SuppressWarnings("checkstyle:RedundantModifier")
70         public Proxy() {
71         }
72
73         Proxy(ExecuteRpc executeRpc) {
74             this.executeRpc = executeRpc;
75         }
76
77         @Override
78         public void writeExternal(ObjectOutput out) throws IOException {
79             out.writeObject(executeRpc.getRpc());
80             SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
81         }
82
83         @Override
84         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
85             QName qname = (QName) in.readObject();
86             executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
87         }
88
89         private Object readResolve() {
90             return executeRpc;
91         }
92     }
93 }