X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2Fmessages%2FExecuteRpc.java;h=c9fb52b6065daf841659e161868cd92b8a579726;hp=4bb5258b4005f21eabc62c2707f2d3edca59b229;hb=01d87e48543865be3d802c6b5497191623b0ae37;hpb=edf5bfcee83c750853253ccfd991ba7000f5f65b diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java index 4bb5258b40..c9fb52b606 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/messages/ExecuteRpc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -7,31 +7,94 @@ */ package org.opendaylight.controller.remote.rpc.messages; +import static java.util.Objects.requireNonNull; -import com.google.common.base.Preconditions; +import com.google.common.base.MoreObjects; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.Serializable; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput; +import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import java.io.Serializable; +public final class ExecuteRpc implements Serializable { + private static final long serialVersionUID = 1128904894827335676L; + + @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class " + + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class " + + "aren't serialized. FindBugs does not recognize this.") + private final NormalizedNode inputNormalizedNode; + private final QName rpc; + + private ExecuteRpc(final @Nullable NormalizedNode inputNormalizedNode, final @NonNull QName rpc) { + this.rpc = requireNonNull(rpc, "rpc Qname should not be null"); + this.inputNormalizedNode = inputNormalizedNode; + } + + public static ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc, final @Nullable NormalizedNode input) { + return new ExecuteRpc(input, rpc.getType().getLastComponent()); + } + + public @Nullable NormalizedNode getInputNormalizedNode() { + return inputNormalizedNode; + } + + public @NonNull QName getRpc() { + return rpc; + } + + private Object writeReplace() { + return new Proxy(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("rpc", rpc) + .add("normalizedNode", inputNormalizedNode) + .toString(); + } + + private static class Proxy implements Externalizable { + private static final long serialVersionUID = 1L; -public class ExecuteRpc implements Serializable { - private static final long serialVersionUID = 1L; + private ExecuteRpc executeRpc; - private final String inputCompositeNode; - private final QName rpc; + // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't + // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection. + @SuppressWarnings("checkstyle:RedundantModifier") + public Proxy() { + } - public ExecuteRpc(final String inputCompositeNode, final QName rpc) { - Preconditions.checkNotNull(inputCompositeNode, "Composite Node input string should be present"); - Preconditions.checkNotNull(rpc, "rpc Qname should not be null"); + Proxy(final ExecuteRpc executeRpc) { + this.executeRpc = executeRpc; + } - this.inputCompositeNode = inputCompositeNode; - this.rpc = rpc; - } + @Override + public void writeExternal(final ObjectOutput out) throws IOException { + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) { + stream.writeQName(executeRpc.getRpc()); + stream.writeOptionalNormalizedNode(executeRpc.getInputNormalizedNode()); + } + } - public String getInputCompositeNode() { - return inputCompositeNode; - } + @Override + public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { + final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in); + final QName qname = stream.readQName(); + executeRpc = new ExecuteRpc(stream.readOptionalNormalizedNode().orElse(null), qname); + } - public QName getRpc() { - return rpc; - } + private Object readResolve() { + return executeRpc; + } + } }