d46bf6ab32ce5e96889482526d37c802d2099002
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / RpcResponse.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 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.io.Serializable;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 public class RpcResponse implements Serializable {
20     private static final long serialVersionUID = -4211279498688989245L;
21
22     private final NormalizedNode<?, ?> resultNormalizedNode;
23
24     public RpcResponse(@Nullable final NormalizedNode<?, ?> inputNormalizedNode) {
25         resultNormalizedNode = inputNormalizedNode;
26     }
27
28     @Nullable
29     public NormalizedNode<?, ?> getResultNormalizedNode() {
30         return resultNormalizedNode;
31     }
32
33     private Object writeReplace() {
34         return new Proxy(this);
35     }
36
37     private static class Proxy implements Externalizable {
38         private static final long serialVersionUID = 1L;
39
40         private RpcResponse rpcResponse;
41
42         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
43         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
44         @SuppressWarnings("checkstyle:RedundantModifier")
45         public Proxy() {
46         }
47
48         Proxy(RpcResponse rpcResponse) {
49             this.rpcResponse = rpcResponse;
50         }
51
52         @Override
53         public void writeExternal(ObjectOutput out) throws IOException {
54             SerializationUtils.serializeNormalizedNode(rpcResponse.getResultNormalizedNode(), out);
55         }
56
57         @Override
58         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
59             rpcResponse = new RpcResponse(SerializationUtils.deserializeNormalizedNode(in));
60         }
61
62         private Object readResolve() {
63             return rpcResponse;
64         }
65     }
66 }