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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.mdsal.dom.api.DOMRpcIdentifier;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 public final class ExecuteRpc implements Serializable {
26 private static final long serialVersionUID = 1128904894827335676L;
28 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
29 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
30 + "aren't serialized. FindBugs does not recognize this.")
31 private final NormalizedNode<?, ?> inputNormalizedNode;
32 private final QName rpc;
34 private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
35 this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
36 this.inputNormalizedNode = inputNormalizedNode;
39 public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
40 return new ExecuteRpc(input, rpc.getType().getLastComponent());
44 public NormalizedNode<?, ?> getInputNormalizedNode() {
45 return inputNormalizedNode;
49 public QName getRpc() {
53 private Object writeReplace() {
54 return new Proxy(this);
58 public String toString() {
59 return MoreObjects.toStringHelper(this)
61 .add("normalizedNode", inputNormalizedNode)
65 private static class Proxy implements Externalizable {
66 private static final long serialVersionUID = 1L;
68 private ExecuteRpc executeRpc;
70 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
71 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
72 @SuppressWarnings("checkstyle:RedundantModifier")
76 Proxy(ExecuteRpc executeRpc) {
77 this.executeRpc = executeRpc;
81 public void writeExternal(ObjectOutput out) throws IOException {
82 out.writeObject(executeRpc.getRpc());
83 SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
87 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
88 QName qname = (QName) in.readObject();
89 executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
92 private Object readResolve() {