Propagate action logical datastore type
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteAction.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc.messages;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27
28 public final class ExecuteAction extends AbstractExecute<@NonNull ContainerNode> {
29     private static final long serialVersionUID = 1128904894827335676L;
30
31     private final @NonNull DOMDataTreeIdentifier path;
32
33     private ExecuteAction(final @NonNull SchemaPath type, final @NonNull DOMDataTreeIdentifier path,
34             final @NonNull ContainerNode input) {
35         super(type, requireNonNull(input));
36         this.path = requireNonNull(path);
37     }
38
39     public static @NonNull ExecuteAction from(final @NonNull SchemaPath type, @NonNull final DOMDataTreeIdentifier path,
40             final @NonNull ContainerNode input) {
41         return new ExecuteAction(type, path, input);
42     }
43
44     public @NonNull DOMDataTreeIdentifier getPath() {
45         return path;
46     }
47
48     @Override
49     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
50         return super.addToStringAttributes(helper.add("path", path));
51     }
52
53     @Override
54     Object writeReplace() {
55         return new Proxy(this);
56     }
57
58     private static final class Proxy implements Externalizable {
59         private static final long serialVersionUID = 1L;
60
61         private ExecuteAction executeAction;
62
63         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
64         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
65         @SuppressWarnings("checkstyle:RedundantModifier")
66         public Proxy() {
67
68         }
69
70         Proxy(final ExecuteAction executeAction) {
71             this.executeAction = requireNonNull(executeAction);
72         }
73
74         @Override
75         public void writeExternal(final ObjectOutput out) throws IOException {
76             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
77                 stream.writeSchemaPath(executeAction.getType());
78                 executeAction.getPath().getDatastoreType().writeTo(out);
79                 stream.writeYangInstanceIdentifier(executeAction.getPath().getRootIdentifier());
80                 stream.writeOptionalNormalizedNode(executeAction.getInput());
81             }
82         }
83
84         @Override
85         public void readExternal(final ObjectInput in) throws IOException {
86             final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
87             final SchemaPath name = stream.readSchemaPath();
88             final LogicalDatastoreType type = LogicalDatastoreType.readFrom(in);
89             final YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
90             final ContainerNode input = (ContainerNode) stream.readOptionalNormalizedNode().orElse(null);
91
92             executeAction = new ExecuteAction(name, new DOMDataTreeIdentifier(type, path), input);
93         }
94
95         private Object readResolve() {
96             return verifyNotNull(executeAction);
97         }
98     }
99 }