Remove JournalWriter.getLastEntry()
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / messages / RpcResponse.java
1 /*
2  * Copyright (c) 2014 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.controller.remote.rpc.messages;
9
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.InvalidObjectException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 public class RpcResponse extends AbstractResponse<ContainerNode> {
22     private static final long serialVersionUID = -4211279498688989245L;
23
24     public RpcResponse(final @Nullable ContainerNode output) {
25         super(output);
26     }
27
28     @Override
29     Object writeReplace() {
30         return new Proxy(this);
31     }
32
33     static @Nullable ContainerNode unmaskContainer(final Optional<NormalizedNode> optNode)
34             throws InvalidObjectException {
35         if (optNode.isEmpty()) {
36             return null;
37         }
38         final var node = optNode.orElseThrow();
39         if (node instanceof ContainerNode container) {
40             return container;
41         }
42         throw new InvalidObjectException("Unexpected data " + node.contract().getSimpleName());
43     }
44
45     private static class Proxy implements Externalizable {
46         private static final long serialVersionUID = 1L;
47
48         private RpcResponse rpcResponse;
49
50         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
51         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
52         @SuppressWarnings("checkstyle:RedundantModifier")
53         public Proxy() {
54         }
55
56         Proxy(final RpcResponse rpcResponse) {
57             this.rpcResponse = rpcResponse;
58         }
59
60         @Override
61         public void writeExternal(final ObjectOutput out) throws IOException {
62             SerializationUtils.writeNormalizedNode(out, rpcResponse.getOutput());
63         }
64
65         @Override
66         public void readExternal(final ObjectInput in) throws IOException {
67             rpcResponse = new RpcResponse(unmaskContainer(SerializationUtils.readNormalizedNode(in)));
68         }
69
70         private Object readResolve() {
71             return rpcResponse;
72         }
73     }
74 }