Switch to use PayloadVersion.CHLORINE_SR2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / SkipTransactionsPayload.java
1 /*
2  * Copyright (c) 2021 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.datastore.persisted;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.io.ByteStreams;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.DataInput;
16 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
19 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
20 import org.opendaylight.controller.cluster.raft.persisted.LegacySerializable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Payload persisted when a local history is instructed some transaction identifiers, i.e. the frontend has used them
26  * for other purposes. It contains a {@link LocalHistoryIdentifier} and a list of transaction identifiers within that
27  * local history.
28  */
29 public sealed class SkipTransactionsPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
30     private static final class Magnesium extends SkipTransactionsPayload implements LegacySerializable {
31         @java.io.Serial
32         private static final long serialVersionUID = 1L;
33
34         Magnesium(final LocalHistoryIdentifier historyId, final byte[] serialized,
35                 final ImmutableUnsignedLongSet transactionIds) {
36             super(historyId, serialized, transactionIds);
37         }
38     }
39
40     @Deprecated(since = "7.0.0", forRemoval = true)
41     private static final class Proxy extends AbstractProxy<LocalHistoryIdentifier> {
42         @java.io.Serial
43         private static final long serialVersionUID = 1L;
44
45         private ImmutableUnsignedLongSet transactionIds;
46
47         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
48         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
49         @SuppressWarnings("checkstyle:RedundantModifier")
50         public Proxy() {
51             // For Externalizable
52         }
53
54         @Override
55         protected LocalHistoryIdentifier readIdentifier(final DataInput in) throws IOException {
56             final var id = LocalHistoryIdentifier.readFrom(in);
57             transactionIds = ImmutableUnsignedLongSet.readFrom(in);
58             return id;
59         }
60
61         @Override
62         protected SkipTransactionsPayload createObject(final LocalHistoryIdentifier identifier,
63                 final byte[] serialized) {
64             return new Magnesium(identifier, serialized, verifyNotNull(transactionIds));
65         }
66     }
67
68     private static final Logger LOG = LoggerFactory.getLogger(SkipTransactionsPayload.class);
69     @java.io.Serial
70     private static final long serialVersionUID = 1L;
71     private static final int PROXY_SIZE = externalizableProxySize(ST::new);
72
73     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via externalizable proxy")
74     private final @NonNull ImmutableUnsignedLongSet transactionIds;
75
76     SkipTransactionsPayload(final @NonNull LocalHistoryIdentifier historyId,
77             final byte @NonNull [] serialized, final ImmutableUnsignedLongSet transactionIds) {
78         super(historyId, serialized);
79         this.transactionIds = requireNonNull(transactionIds);
80     }
81
82     public static @NonNull SkipTransactionsPayload create(final LocalHistoryIdentifier historyId,
83             final ImmutableUnsignedLongSet transactionIds, final int initialSerializedBufferCapacity) {
84         final var out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
85         try {
86             historyId.writeTo(out);
87             transactionIds.writeTo(out);
88         } catch (IOException e) {
89             // This should never happen
90             LOG.error("Failed to serialize {} ids {}", historyId, transactionIds, e);
91             throw new IllegalStateException("Failed to serialize " + historyId + " ids " + transactionIds, e);
92         }
93
94         return new SkipTransactionsPayload(historyId, out.toByteArray(), transactionIds);
95     }
96
97     public @NonNull ImmutableUnsignedLongSet getTransactionIds() {
98         return transactionIds;
99     }
100
101     @Override
102     protected ST externalizableProxy(final byte[] serialized) {
103         return new ST(serialized);
104     }
105
106     @Override
107     protected int externalizableProxySize() {
108         return PROXY_SIZE;
109     }
110 }