Integrate MRI projects for Neon
[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 java.util.Objects;
20 import javax.annotation.Nonnull;
21 import javax.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(@Nullable final NormalizedNodeMessage normalizedNodeMessage,
33                                  @Nonnull final Collection<? extends RpcError> rpcErrors) {
34         this.normalizedNodeMessage = normalizedNodeMessage;
35         this.rpcErrors = Objects.requireNonNull(rpcErrors);
36     }
37
38     @Nullable
39     public NormalizedNodeMessage getNormalizedNodeMessage() {
40         return normalizedNodeMessage;
41     }
42
43     @Nonnull
44     public Collection<? extends RpcError> getRpcErrors() {
45         return rpcErrors;
46     }
47
48     private Object writeReplace() {
49         return new Proxy(this);
50     }
51
52     private static class Proxy implements Externalizable {
53         private static final long serialVersionUID = 2L;
54
55         private InvokeRpcMessageReply invokeRpcMessageReply;
56
57         @SuppressWarnings("checkstyle:RedundantModifier")
58         public Proxy() {
59             //due to Externalizable
60         }
61
62         Proxy(final InvokeRpcMessageReply invokeRpcMessageReply) {
63             this.invokeRpcMessageReply = invokeRpcMessageReply;
64         }
65
66         @Override
67         public void writeExternal(final ObjectOutput out) throws IOException {
68             out.writeInt(invokeRpcMessageReply.getRpcErrors().size());
69             for (final RpcError rpcError : invokeRpcMessageReply.getRpcErrors()) {
70                 out.writeObject(rpcError);
71             }
72             out.writeObject(invokeRpcMessageReply.getNormalizedNodeMessage());
73         }
74
75         @Override
76         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
77             final int size = in.readInt();
78             final Collection<RpcError> rpcErrors = new LinkedList<>();
79             for (int i = 0; i < size; i++) {
80                 rpcErrors.add((RpcError) in.readObject());
81             }
82             final NormalizedNodeMessage normalizedNodeMessage = (NormalizedNodeMessage) in.readObject();
83             invokeRpcMessageReply = new InvokeRpcMessageReply(normalizedNodeMessage, rpcErrors);
84         }
85
86         private Object readResolve() {
87             return invokeRpcMessageReply;
88         }
89     }
90
91 }