2 * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc.messages;
10 import static java.util.Objects.requireNonNull;
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;
28 public final class ExecuteRpc implements Serializable {
29 private static final long serialVersionUID = 1128904894827335676L;
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;
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;
42 public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode<?, ?> input) {
43 return new ExecuteRpc(input, rpc.getType().getLastComponent());
46 public @Nullable NormalizedNode<?, ?> getInputNormalizedNode() {
47 return inputNormalizedNode;
50 public @NonNull QName getRpc() {
54 private Object writeReplace() {
55 return new Proxy(this);
59 public String toString() {
60 return MoreObjects.toStringHelper(this)
62 .add("normalizedNode", inputNormalizedNode)
66 private static class Proxy implements Externalizable {
67 private static final long serialVersionUID = 1L;
69 private ExecuteRpc executeRpc;
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")
77 Proxy(final ExecuteRpc executeRpc) {
78 this.executeRpc = executeRpc;
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());
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);
96 private Object readResolve() {