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