Teach sal-remoterpc-connector to route actions
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
1 /*
2  * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  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 java.util.Objects.requireNonNull;
11
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
19 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
21 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24
25 public final class ExecuteRpc extends AbstractExecute<@Nullable NormalizedNode<?, ?>> {
26     private static final long serialVersionUID = 1128904894827335676L;
27
28     private ExecuteRpc(final @NonNull SchemaPath type, final @Nullable NormalizedNode<?, ?> input) {
29         super(type, input);
30     }
31
32     public static @NonNull ExecuteRpc from(final @NonNull DOMRpcIdentifier rpc,
33             final @Nullable NormalizedNode<?, ?> input) {
34         return new ExecuteRpc(rpc.getType(), input);
35     }
36
37     @Override
38     Object writeReplace() {
39         return new Proxy(this);
40     }
41
42     private static final class Proxy implements Externalizable {
43         private static final long serialVersionUID = 1L;
44
45         private ExecuteRpc executeRpc;
46
47         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
48         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
49         @SuppressWarnings("checkstyle:RedundantModifier")
50         public Proxy() {
51
52         }
53
54         Proxy(final ExecuteRpc executeRpc) {
55             this.executeRpc = requireNonNull(executeRpc);
56         }
57
58         @Override
59         public void writeExternal(final ObjectOutput out) throws IOException {
60             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
61                 stream.writeQName(executeRpc.getType().getLastComponent());
62                 stream.writeOptionalNormalizedNode(executeRpc.getInput());
63             }
64         }
65
66         @Override
67         public void readExternal(final ObjectInput in) throws IOException {
68             final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
69             final SchemaPath type = SchemaPath.ROOT.createChild(stream.readQName());
70             final NormalizedNode<?, ?> input = stream.readOptionalNormalizedNode().orElse(null);
71             executeRpc = new ExecuteRpc(type, input);
72         }
73
74         private Object readResolve() {
75             return executeRpc;
76         }
77     }
78 }