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.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;
26 public final class ExecuteRpc implements Serializable {
27 private static final long serialVersionUID = 1128904894827335676L;
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;
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;
40 public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode<?, ?> input) {
41 return new ExecuteRpc(input, rpc.getType().getLastComponent());
44 public @Nullable NormalizedNode<?, ?> getInputNormalizedNode() {
45 return inputNormalizedNode;
48 public @NonNull QName getRpc() {
52 private Object writeReplace() {
53 return new Proxy(this);
57 public String toString() {
58 return MoreObjects.toStringHelper(this)
60 .add("normalizedNode", inputNormalizedNode)
64 private static class Proxy implements Externalizable {
65 private static final long serialVersionUID = 1L;
67 private ExecuteRpc executeRpc;
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")
75 Proxy(final ExecuteRpc executeRpc) {
76 this.executeRpc = executeRpc;
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());
87 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
88 // FIXME: QName is a WritableObject
89 QName qname = (QName) in.readObject();
90 executeRpc = new ExecuteRpc(SerializationUtils.readNormalizedNode(in).orElse(null), qname);
93 private Object readResolve() {