Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / ExecuteRpc.java
1 /*
2  * Copyright (c) 2014 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
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * @author tony
27  *
28  */
29 public class ExecuteRpc implements Serializable {
30     private static final long serialVersionUID = 1128904894827335676L;
31
32     private final NormalizedNode<?, ?> inputNormalizedNode;
33     private final QName rpc;
34
35     private ExecuteRpc(@Nullable final NormalizedNode<?, ?> inputNormalizedNode, @Nonnull final QName rpc) {
36         this.rpc = Preconditions.checkNotNull(rpc, "rpc Qname should not be null");
37         this.inputNormalizedNode = inputNormalizedNode;
38     }
39
40     public static ExecuteRpc from(@Nonnull final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
41         return new ExecuteRpc(input, rpc.getType().getLastComponent());
42     }
43
44     @Nullable
45     public NormalizedNode<?, ?> getInputNormalizedNode() {
46         return inputNormalizedNode;
47     }
48
49     @Nonnull
50     public QName getRpc() {
51         return rpc;
52     }
53
54     private Object writeReplace() {
55         return new Proxy(this);
56     }
57
58     @Override
59     public String toString() {
60         return MoreObjects.toStringHelper(this)
61                 .add("rpc", rpc)
62                 .add("normalizedNode", inputNormalizedNode)
63                 .toString();
64     }
65
66     private static class Proxy implements Externalizable {
67         private static final long serialVersionUID = 1L;
68
69         private ExecuteRpc executeRpc;
70
71         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
72         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
73         @SuppressWarnings("checkstyle:RedundantModifier")
74         public Proxy() {
75         }
76
77         Proxy(ExecuteRpc executeRpc) {
78             this.executeRpc = executeRpc;
79         }
80
81         @Override
82         public void writeExternal(ObjectOutput out) throws IOException {
83             out.writeObject(executeRpc.getRpc());
84             SerializationUtils.serializeNormalizedNode(executeRpc.getInputNormalizedNode(), out);
85         }
86
87         @Override
88         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
89             QName qname = (QName) in.readObject();
90             executeRpc = new ExecuteRpc(SerializationUtils.deserializeNormalizedNode(in), qname);
91         }
92
93         private Object readResolve() {
94             return executeRpc;
95         }
96     }
97 }