Remove old payload proxies
[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 java.util.Objects.requireNonNull;
11
12 import com.google.common.io.ByteStreams;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.IOException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Payload persisted when a local history is instructed some transaction identifiers, i.e. the frontend has used them
23  * for other purposes. It contains a {@link LocalHistoryIdentifier} and a list of transaction identifiers within that
24  * local history.
25  */
26 public final class SkipTransactionsPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
27     private static final Logger LOG = LoggerFactory.getLogger(SkipTransactionsPayload.class);
28     @java.io.Serial
29     private static final long serialVersionUID = 1L;
30     private static final int PROXY_SIZE = externalizableProxySize(ST::new);
31
32     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via externalizable proxy")
33     private final @NonNull ImmutableUnsignedLongSet transactionIds;
34
35     SkipTransactionsPayload(final @NonNull LocalHistoryIdentifier historyId,
36             final byte @NonNull [] serialized, final ImmutableUnsignedLongSet transactionIds) {
37         super(historyId, serialized);
38         this.transactionIds = requireNonNull(transactionIds);
39     }
40
41     public static @NonNull SkipTransactionsPayload create(final LocalHistoryIdentifier historyId,
42             final ImmutableUnsignedLongSet transactionIds, final int initialSerializedBufferCapacity) {
43         final var out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
44         try {
45             historyId.writeTo(out);
46             transactionIds.writeTo(out);
47         } catch (IOException e) {
48             // This should never happen
49             LOG.error("Failed to serialize {} ids {}", historyId, transactionIds, e);
50             throw new IllegalStateException("Failed to serialize " + historyId + " ids " + transactionIds, e);
51         }
52
53         return new SkipTransactionsPayload(historyId, out.toByteArray(), transactionIds);
54     }
55
56     public @NonNull ImmutableUnsignedLongSet getTransactionIds() {
57         return transactionIds;
58     }
59
60     @Override
61     protected ST externalizableProxy(final byte[] serialized) {
62         return new ST(serialized);
63     }
64
65     @Override
66     protected int externalizableProxySize() {
67         return PROXY_SIZE;
68     }
69 }