Add support for reusable streaming
[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.NormalizedNodeDataInput;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
24 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 public final class ExecuteRpc implements Serializable {
29     private static final long serialVersionUID = 1128904894827335676L;
30
31     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
32             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
33             + "aren't serialized. FindBugs does not recognize this.")
34     private final NormalizedNode<?, ?> inputNormalizedNode;
35     private final QName rpc;
36
37     private ExecuteRpc(final @Nullable NormalizedNode<?, ?> inputNormalizedNode, final @NonNull QName rpc) {
38         this.rpc = requireNonNull(rpc, "rpc Qname should not be null");
39         this.inputNormalizedNode = inputNormalizedNode;
40     }
41
42     public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode<?, ?> input) {
43         return new ExecuteRpc(input, rpc.getType().getLastComponent());
44     }
45
46     public @Nullable NormalizedNode<?, ?> getInputNormalizedNode() {
47         return inputNormalizedNode;
48     }
49
50     public @NonNull QName getRpc() {
51         return rpc;
52     }
53
54     private Object writeReplace() {
55         return new Proxy(this);
56     }
57
58     @Override
59     public String toString() {
60         return MoreObjects.toStringHelper(this)
61                 .add("rpc", rpc)
62                 .add("normalizedNode", inputNormalizedNode)
63                 .toString();
64     }
65
66     private static class Proxy implements Externalizable {
67         private static final long serialVersionUID = 1L;
68
69         private ExecuteRpc executeRpc;
70
71         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
72         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
73         @SuppressWarnings("checkstyle:RedundantModifier")
74         public Proxy() {
75         }
76
77         Proxy(final ExecuteRpc executeRpc) {
78             this.executeRpc = executeRpc;
79         }
80
81         @Override
82         public void writeExternal(final ObjectOutput out) throws IOException {
83             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
84                 stream.writeQName(executeRpc.getRpc());
85                 stream.writeOptionalNormalizedNode(executeRpc.getInputNormalizedNode());
86             }
87         }
88
89         @Override
90         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
91             final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
92             final QName qname = stream.readQName();
93             executeRpc = new ExecuteRpc(stream.readOptionalNormalizedNode().orElse(null), qname);
94         }
95
96         private Object readResolve() {
97             return executeRpc;
98         }
99     }
100 }