b70582354cc6cd476fd72e93d0303a0211d7babf
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / messages / rpc / InvokeRpcMessageReply.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.netconf.topology.singleton.messages.rpc;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import java.util.Collection;
18 import java.util.LinkedList;
19 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21
22 public class InvokeRpcMessageReply implements Serializable {
23     private static final long serialVersionUID = 1L;
24
25     @SuppressFBWarnings("SE_BAD_FIELD")
26     private final Collection<RpcError> rpcErrors;
27     private final NormalizedNodeMessage normalizedNodeMessage;
28
29     public InvokeRpcMessageReply(final NormalizedNodeMessage normalizedNodeMessage,
30                                  final Collection<RpcError> rpcErrors) {
31         this.normalizedNodeMessage = normalizedNodeMessage;
32         this.rpcErrors = rpcErrors;
33     }
34
35     public NormalizedNodeMessage getNormalizedNodeMessage() {
36         return normalizedNodeMessage;
37     }
38
39     public Collection<RpcError> getRpcErrors() {
40         return rpcErrors;
41     }
42
43     private Object writeReplace() {
44         return new Proxy(this);
45     }
46
47     private static class Proxy implements Externalizable {
48         private static final long serialVersionUID = 2L;
49
50         private InvokeRpcMessageReply invokeRpcMessageReply;
51
52         @SuppressWarnings("checkstyle:RedundantModifier")
53         public Proxy() {
54             //due to Externalizable
55         }
56
57         Proxy(final InvokeRpcMessageReply invokeRpcMessageReply) {
58             this.invokeRpcMessageReply = invokeRpcMessageReply;
59         }
60
61         @Override
62         public void writeExternal(ObjectOutput out) throws IOException {
63             out.writeInt(invokeRpcMessageReply.getRpcErrors().size());
64             for (final RpcError rpcError : invokeRpcMessageReply.getRpcErrors()) {
65                 out.writeObject(rpcError);
66             }
67             out.writeObject(invokeRpcMessageReply.getNormalizedNodeMessage());
68         }
69
70         @Override
71         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
72             final int size = in.readInt();
73             final Collection<RpcError> rpcErrors = new LinkedList<>();
74             for (int i = 0; i < size; i++) {
75                 rpcErrors.add((RpcError) in.readObject());
76             }
77             final NormalizedNodeMessage normalizedNodeMessage = (NormalizedNodeMessage) in.readObject();
78             invokeRpcMessageReply = new InvokeRpcMessageReply(normalizedNodeMessage, rpcErrors);
79         }
80
81         private Object readResolve() {
82             return invokeRpcMessageReply;
83         }
84     }
85
86 }