Fixup checkstyle
[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.NormalizedNodeStreamVersion;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28
29 public final class ExecuteAction extends AbstractExecute<Absolute, @NonNull ContainerNode> {
30     private static final long serialVersionUID = 1128904894827335676L;
31
32     private final @NonNull DOMDataTreeIdentifier path;
33
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);
38     }
39
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);
43     }
44
45     public @NonNull DOMDataTreeIdentifier getPath() {
46         return path;
47     }
48
49     @Override
50     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
51         return super.addToStringAttributes(helper.add("path", path));
52     }
53
54     @Override
55     Object writeReplace() {
56         return new Proxy(this);
57     }
58
59     private static final class Proxy implements Externalizable {
60         private static final long serialVersionUID = 1L;
61
62         private ExecuteAction executeAction;
63
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")
67         public Proxy() {
68
69         }
70
71         Proxy(final ExecuteAction executeAction) {
72             this.executeAction = requireNonNull(executeAction);
73         }
74
75         @Override
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());
82             }
83         }
84
85         @Override
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);
91             }
92
93             final LogicalDatastoreType type = LogicalDatastoreType.readFrom(in);
94             final YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
95             final ContainerNode input = (ContainerNode) stream.readOptionalNormalizedNode().orElse(null);
96
97             executeAction = new ExecuteAction(absolute, DOMDataTreeIdentifier.of(type, path), input);
98         }
99
100         private Object readResolve() {
101             return verifyNotNull(executeAction);
102         }
103     }
104 }