Remove ABIVersion.MAGNESIUM
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / RTS.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.cluster.access.commands;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
20
21 /**
22  * Externalizable proxy for use with {@link ReadTransactionSuccess}. It implements the Chlorine SR2 serialization
23  * format.
24  */
25 final class RTS implements TransactionSuccess.SerialForm<ReadTransactionSuccess> {
26     @java.io.Serial
27     private static final long serialVersionUID = 1L;
28
29     private ReadTransactionSuccess message;
30
31     @SuppressWarnings("checkstyle:RedundantModifier")
32     public RTS() {
33         // for Externalizable
34     }
35
36     RTS(final ReadTransactionSuccess message) {
37         this.message = requireNonNull(message);
38     }
39
40     @Override
41     public ReadTransactionSuccess message() {
42         return verifyNotNull(message);
43     }
44
45     @Override
46     public void setMessage(final ReadTransactionSuccess message) {
47         this.message = requireNonNull(message);
48     }
49
50     @Override
51     public ReadTransactionSuccess readExternal(final ObjectInput in, final TransactionIdentifier target,
52             final long sequence) throws IOException {
53         final Optional<NormalizedNode> data;
54         if (in.readBoolean()) {
55             data = Optional.of(NormalizedNodeDataInput.newDataInput(in).readNormalizedNode());
56         } else {
57             data = Optional.empty();
58         }
59         return new ReadTransactionSuccess(target, sequence, data);
60     }
61
62     @Override
63     public void writeExternal(final ObjectOutput out, final ReadTransactionSuccess msg) throws IOException {
64         TransactionSuccess.SerialForm.super.writeExternal(out, msg);
65
66         final var data = msg.getData();
67         if (data.isPresent()) {
68             out.writeBoolean(true);
69             try (var nnout = msg.getVersion().getStreamVersion().newDataOutput(out)) {
70                 nnout.writeNormalizedNode(data.orElseThrow());
71             }
72         } else {
73             out.writeBoolean(false);
74         }
75     }
76
77     @Override
78     public Object readResolve() {
79         return message();
80     }
81 }