Teach NETCONF about YANG 1.1 actions in cluster topology
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / messages / action / InvokeActionMessageReply.java
1 /*
2  * Copyright (C) 2019 Ericsson Software Technology AB. 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.action;
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.ArrayList;
19 import java.util.Collection;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.netconf.topology.singleton.messages.ContainerNodeMessage;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24
25 /**
26  * Message container which holds node reply in {@link ContainerNodeMessage}, {@link RpcError} prepared to send between
27  * remote hosts with serialization when action operation is invoked.
28  */
29 public class InvokeActionMessageReply implements Serializable {
30     private static final long serialVersionUID = 1L;
31
32     @SuppressFBWarnings("SE_BAD_FIELD")
33     private final Collection<? extends RpcError> rpcErrors;
34     private final ContainerNodeMessage containerNodeMessage;
35
36     /**
37      * Constructor for {@code InvokeActionMessage}.
38      *
39      * @param containerNodeMessage ContainerNodeMessage
40      * @param rpcErrors RpcError
41      */
42     public InvokeActionMessageReply(final @Nullable ContainerNodeMessage containerNodeMessage,
43         final @NonNull Collection<? extends RpcError> rpcErrors) {
44         this.containerNodeMessage = requireNonNull(containerNodeMessage);
45         this.rpcErrors = requireNonNull(rpcErrors);
46     }
47
48     public @Nullable ContainerNodeMessage getContainerNodeMessage() {
49         return containerNodeMessage;
50     }
51
52     public @NonNull Collection<? extends RpcError> getRpcErrors() {
53         return rpcErrors;
54     }
55
56     private Object writeReplace() {
57         return new Proxy(this);
58     }
59
60     private static class Proxy implements Externalizable {
61         private static final long serialVersionUID = 2L;
62
63         private InvokeActionMessageReply invokeActionMessageReply;
64
65         @SuppressWarnings("checkstyle:RedundantModifier")
66         public Proxy() {
67             //due to Externalizable
68         }
69
70         Proxy(final InvokeActionMessageReply invokeActionMessageReply) {
71             this.invokeActionMessageReply = invokeActionMessageReply;
72         }
73
74         @Override
75         public void writeExternal(final ObjectOutput out) throws IOException {
76             out.writeInt(invokeActionMessageReply.getRpcErrors().size());
77             for (final RpcError rpcError : invokeActionMessageReply.getRpcErrors()) {
78                 out.writeObject(rpcError);
79             }
80             out.writeObject(invokeActionMessageReply.getContainerNodeMessage());
81         }
82
83         @Override
84         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
85             final int size = in.readInt();
86             final Collection<RpcError> rpcErrors = new ArrayList<>(size);
87             for (int i = 0; i < size; i++) {
88                 rpcErrors.add((RpcError) in.readObject());
89             }
90
91             final ContainerNodeMessage containerNodeMessage = (ContainerNodeMessage) in.readObject();
92             invokeActionMessageReply = new InvokeActionMessageReply(containerNodeMessage, rpcErrors);
93         }
94
95         private Object readResolve() {
96             return invokeActionMessageReply;
97         }
98     }
99 }