2 * Copyright (c) 2019 Nordix Foundation. 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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.InvalidObjectException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
25 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 public final class ExecuteAction extends AbstractExecute<Absolute, @NonNull ContainerNode> {
30 private static final long serialVersionUID = 1128904894827335676L;
32 private final @NonNull DOMDataTreeIdentifier path;
34 private ExecuteAction(final @NonNull Absolute type, final @NonNull DOMDataTreeIdentifier path,
35 final @NonNull ContainerNode input) {
36 super(type, requireNonNull(input));
37 this.path = requireNonNull(path);
40 public static @NonNull ExecuteAction from(final @NonNull Absolute type, @NonNull final DOMDataTreeIdentifier path,
41 final @NonNull ContainerNode input) {
42 return new ExecuteAction(type, path, input);
45 public @NonNull DOMDataTreeIdentifier getPath() {
50 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
51 return super.addToStringAttributes(helper.add("path", path));
55 Object writeReplace() {
56 return new Proxy(this);
59 private static final class Proxy implements Externalizable {
60 private static final long serialVersionUID = 1L;
62 private ExecuteAction executeAction;
64 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
65 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
66 @SuppressWarnings("checkstyle:RedundantModifier")
71 Proxy(final ExecuteAction executeAction) {
72 this.executeAction = requireNonNull(executeAction);
76 public void writeExternal(final ObjectOutput out) throws IOException {
77 try (var stream = NormalizedNodeStreamVersion.current().newDataOutput(out)) {
78 stream.writeSchemaNodeIdentifier(executeAction.getType());
79 executeAction.getPath().datastore().writeTo(out);
80 stream.writeYangInstanceIdentifier(executeAction.getPath().path());
81 stream.writeOptionalNormalizedNode(executeAction.getInput());
86 public void readExternal(final ObjectInput in) throws IOException {
87 final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
88 final SchemaNodeIdentifier sni = stream.readSchemaNodeIdentifier();
89 if (!(sni instanceof Absolute absolute)) {
90 throw new InvalidObjectException("Non-absolute type " + sni);
93 final LogicalDatastoreType type = LogicalDatastoreType.readFrom(in);
94 final YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
95 final ContainerNode input = (ContainerNode) stream.readOptionalNormalizedNode().orElse(null);
97 executeAction = new ExecuteAction(absolute, DOMDataTreeIdentifier.of(type, path), input);
100 private Object readResolve() {
101 return verifyNotNull(executeAction);