Bump upstreams for Silicon
[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.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.NormalizedNodeDataOutput;
26 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29
30 public final class ExecuteAction extends AbstractExecute<Absolute, @NonNull ContainerNode> {
31     private static final long serialVersionUID = 1128904894827335676L;
32
33     private final @NonNull DOMDataTreeIdentifier path;
34
35     private ExecuteAction(final @NonNull Absolute type, final @NonNull DOMDataTreeIdentifier path,
36             final @NonNull ContainerNode input) {
37         super(type, requireNonNull(input));
38         this.path = requireNonNull(path);
39     }
40
41     public static @NonNull ExecuteAction from(final @NonNull Absolute type, @NonNull final DOMDataTreeIdentifier path,
42             final @NonNull ContainerNode input) {
43         return new ExecuteAction(type, path, input);
44     }
45
46     public @NonNull DOMDataTreeIdentifier getPath() {
47         return path;
48     }
49
50     @Override
51     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
52         return super.addToStringAttributes(helper.add("path", path));
53     }
54
55     @Override
56     Object writeReplace() {
57         return new Proxy(this);
58     }
59
60     private static final class Proxy implements Externalizable {
61         private static final long serialVersionUID = 1L;
62
63         private ExecuteAction executeAction;
64
65         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
66         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
67         @SuppressWarnings("checkstyle:RedundantModifier")
68         public Proxy() {
69
70         }
71
72         Proxy(final ExecuteAction executeAction) {
73             this.executeAction = requireNonNull(executeAction);
74         }
75
76         @Override
77         public void writeExternal(final ObjectOutput out) throws IOException {
78             try (NormalizedNodeDataOutput stream = NormalizedNodeStreamVersion.current().newDataOutput(out)) {
79                 stream.writeSchemaNodeIdentifier(executeAction.getType());
80                 executeAction.getPath().getDatastoreType().writeTo(out);
81                 stream.writeYangInstanceIdentifier(executeAction.getPath().getRootIdentifier());
82                 stream.writeOptionalNormalizedNode(executeAction.getInput());
83             }
84         }
85
86         @Override
87         public void readExternal(final ObjectInput in) throws IOException {
88             final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
89             final SchemaNodeIdentifier sni = stream.readSchemaNodeIdentifier();
90             if (!(sni instanceof Absolute)) {
91                 throw new InvalidObjectException("Non-absolute type " + sni);
92             }
93
94             final LogicalDatastoreType type = LogicalDatastoreType.readFrom(in);
95             final YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
96             final ContainerNode input = (ContainerNode) stream.readOptionalNormalizedNode().orElse(null);
97
98             executeAction = new ExecuteAction((Absolute) sni, new DOMDataTreeIdentifier(type, path), input);
99         }
100
101         private Object readResolve() {
102             return verifyNotNull(executeAction);
103         }
104     }
105 }