6f1b44b8f822cd88cac40c528f261a304c2dbb2d
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.io.Serializable;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 public class RpcResponse implements Serializable {
21     private static final long serialVersionUID = -4211279498688989245L;
22
23     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
24             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
25             + "aren't serialized. FindBugs does not recognize this.")
26     private final NormalizedNode<?, ?> resultNormalizedNode;
27
28     public RpcResponse(final @Nullable NormalizedNode<?, ?> inputNormalizedNode) {
29         resultNormalizedNode = inputNormalizedNode;
30     }
31
32     public @Nullable NormalizedNode<?, ?> getResultNormalizedNode() {
33         return resultNormalizedNode;
34     }
35
36     private Object writeReplace() {
37         return new Proxy(this);
38     }
39
40     private static class Proxy implements Externalizable {
41         private static final long serialVersionUID = 1L;
42
43         private RpcResponse rpcResponse;
44
45         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
46         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
47         @SuppressWarnings("checkstyle:RedundantModifier")
48         public Proxy() {
49         }
50
51         Proxy(final RpcResponse rpcResponse) {
52             this.rpcResponse = rpcResponse;
53         }
54
55         @Override
56         public void writeExternal(final ObjectOutput out) throws IOException {
57             SerializationUtils.writeNormalizedNode(out, rpcResponse.getResultNormalizedNode());
58         }
59
60         @Override
61         public void readExternal(final ObjectInput in) throws IOException {
62             rpcResponse = new RpcResponse(SerializationUtils.readNormalizedNode(in).orElse(null));
63         }
64
65         private Object readResolve() {
66             return rpcResponse;
67         }
68     }
69 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.