Add Payload.serializedSize()
[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.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Payload persisted when a local history is instructed some transaction identifiers, i.e. the frontend has used them
25  * for other purposes. It contains a {@link LocalHistoryIdentifier} and a list of transaction identifiers within that
26  * local history.
27  */
28 public final class SkipTransactionsPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
29     private static final class Proxy extends AbstractProxy<LocalHistoryIdentifier> {
30         private static final long serialVersionUID = 1L;
31
32         private ImmutableUnsignedLongSet transactionIds;
33
34         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final byte[] serialized) {
42             super(serialized);
43         }
44
45         @Override
46         protected LocalHistoryIdentifier readIdentifier(final DataInput in) throws IOException {
47             final var id = LocalHistoryIdentifier.readFrom(in);
48             transactionIds = ImmutableUnsignedLongSet.readFrom(in);
49             return id;
50         }
51
52         @Override
53         protected SkipTransactionsPayload createObject(final LocalHistoryIdentifier identifier,
54                 final byte[] serialized) {
55             return new SkipTransactionsPayload(identifier, serialized, verifyNotNull(transactionIds));
56         }
57     }
58
59     private static final Logger LOG = LoggerFactory.getLogger(SkipTransactionsPayload.class);
60     private static final long serialVersionUID = 1L;
61     private static final int PROXY_SIZE = externalizableProxySize(Proxy::new);
62
63     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via externalizable proxy")
64     private final @NonNull ImmutableUnsignedLongSet transactionIds;
65
66     private SkipTransactionsPayload(final @NonNull LocalHistoryIdentifier historyId,
67             final byte @NonNull [] serialized, final ImmutableUnsignedLongSet transactionIds) {
68         super(historyId, serialized);
69         this.transactionIds = requireNonNull(transactionIds);
70     }
71
72     public static @NonNull SkipTransactionsPayload create(final LocalHistoryIdentifier historyId,
73             final ImmutableUnsignedLongSet transactionIds, final int initialSerializedBufferCapacity) {
74         final var out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
75         try {
76             historyId.writeTo(out);
77             transactionIds.writeTo(out);
78         } catch (IOException e) {
79             // This should never happen
80             LOG.error("Failed to serialize {} ids {}", historyId, transactionIds, e);
81             throw new IllegalStateException("Failed to serialize " + historyId + " ids " + transactionIds, e);
82         }
83
84         return new SkipTransactionsPayload(historyId, out.toByteArray(), transactionIds);
85     }
86
87     public @NonNull ImmutableUnsignedLongSet getTransactionIds() {
88         return transactionIds;
89     }
90
91     @Override
92     protected Proxy externalizableProxy(final byte[] serialized) {
93         return new Proxy(serialized);
94     }
95
96     @Override
97     protected int externalizableProxySize() {
98         return PROXY_SIZE;
99     }
100 }