Remove use of thread-local output
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
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.io.Serializable;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
22 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 public final class ExecuteRpc implements Serializable {
27     private static final long serialVersionUID = 1128904894827335676L;
28
29     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
30             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
31             + "aren't serialized. FindBugs does not recognize this.")
32     private final NormalizedNode<?, ?> inputNormalizedNode;
33     private final QName rpc;
34
35     private ExecuteRpc(final @Nullable NormalizedNode<?, ?> inputNormalizedNode, final @NonNull QName rpc) {
36         this.rpc = requireNonNull(rpc, "rpc Qname should not be null");
37         this.inputNormalizedNode = inputNormalizedNode;
38     }
39
40     public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode<?, ?> input) {
41         return new ExecuteRpc(input, rpc.getType().getLastComponent());
42     }
43
44     public @Nullable NormalizedNode<?, ?> getInputNormalizedNode() {
45         return inputNormalizedNode;
46     }
47
48     public @NonNull QName getRpc() {
49         return rpc;
50     }
51
52     private Object writeReplace() {
53         return new Proxy(this);
54     }
55
56     @Override
57     public String toString() {
58         return MoreObjects.toStringHelper(this)
59                 .add("rpc", rpc)
60                 .add("normalizedNode", inputNormalizedNode)
61                 .toString();
62     }
63
64     private static class Proxy implements Externalizable {
65         private static final long serialVersionUID = 1L;
66
67         private ExecuteRpc executeRpc;
68
69         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
70         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
71         @SuppressWarnings("checkstyle:RedundantModifier")
72         public Proxy() {
73         }
74
75         Proxy(final ExecuteRpc executeRpc) {
76             this.executeRpc = executeRpc;
77         }
78
79         @Override
80         public void writeExternal(final ObjectOutput out) throws IOException {
81             // FIXME: QName is a WritableObject
82             out.writeObject(executeRpc.getRpc());
83             SerializationUtils.writeNormalizedNode(out, executeRpc.getInputNormalizedNode());
84         }
85
86         @Override
87         public void readExternal(final 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 }