Merge "Bug 7891: Add ssh feature to netconf-netty-util"
[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 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 java.util.Collection;
17 import java.util.LinkedList;
18 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
19 import org.opendaylight.yangtools.yang.common.RpcError;
20
21 public class InvokeRpcMessageReply implements Serializable {
22     private static final long serialVersionUID = 1L;
23
24     private final Collection<RpcError> rpcErrors;
25     private final NormalizedNodeMessage normalizedNodeMessage;
26
27     public InvokeRpcMessageReply(final NormalizedNodeMessage normalizedNodeMessage,
28                                  final Collection<RpcError> rpcErrors) {
29         this.normalizedNodeMessage = normalizedNodeMessage;
30         this.rpcErrors = rpcErrors;
31     }
32
33     public NormalizedNodeMessage getNormalizedNodeMessage() {
34         return normalizedNodeMessage;
35     }
36
37     public Collection<RpcError> getRpcErrors() {
38         return rpcErrors;
39     }
40
41     private Object writeReplace() {
42         return new Proxy(this);
43     }
44
45     private static class Proxy implements Externalizable {
46         private static final long serialVersionUID = 2L;
47
48         private InvokeRpcMessageReply invokeRpcMessageReply;
49
50         Proxy() {
51             //due to Externalizable
52         }
53
54         Proxy(final InvokeRpcMessageReply invokeRpcMessageReply) {
55             this.invokeRpcMessageReply = invokeRpcMessageReply;
56         }
57
58         @Override
59         public void writeExternal(ObjectOutput out) throws IOException {
60             out.writeInt(invokeRpcMessageReply.getRpcErrors().size());
61             for (final RpcError rpcError : invokeRpcMessageReply.getRpcErrors()) {
62                 out.writeObject(rpcError);
63             }
64             out.writeObject(invokeRpcMessageReply.getNormalizedNodeMessage());
65         }
66
67         @Override
68         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
69             final int size = in.readInt();
70             final Collection<RpcError> rpcErrors = new LinkedList<>();
71             for (int i = 0; i < size; i++) {
72                 rpcErrors.add((RpcError) in.readObject());
73             }
74             final NormalizedNodeMessage normalizedNodeMessage = (NormalizedNodeMessage) in.readObject();
75             invokeRpcMessageReply = new InvokeRpcMessageReply(normalizedNodeMessage, rpcErrors);
76         }
77
78         private Object readResolve() {
79             return invokeRpcMessageReply;
80         }
81     }
82
83 }