5f398853fe7a5561e2ce69bb8b60f360d8c14377
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbortTransactionPayload.java
1 /*
2  * Copyright (c) 2016 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.cluster.datastore.persisted;
9
10 import com.google.common.io.ByteArrayDataOutput;
11 import com.google.common.io.ByteStreams;
12 import java.io.DataInput;
13 import java.io.IOException;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.raft.persisted.LegacySerializable;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Payload persisted when a transaction is aborted. It contains the transaction identifier.
21  *
22  * @author Robert Varga
23  */
24 public sealed class AbortTransactionPayload extends AbstractIdentifiablePayload<TransactionIdentifier> {
25     @Deprecated(since = "7.0.0", forRemoval = true)
26     private static final class Magnesium extends AbortTransactionPayload implements LegacySerializable {
27         @java.io.Serial
28         private static final long serialVersionUID = 1L;
29
30         Magnesium(final TransactionIdentifier transactionId, final byte[] serialized) {
31             super(transactionId, serialized);
32         }
33     }
34
35     @Deprecated(since = "7.0.0", forRemoval = true)
36     private static final class Proxy extends AbstractProxy<TransactionIdentifier> {
37         @java.io.Serial
38         private static final long serialVersionUID = 1L;
39
40         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
41         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
42         @SuppressWarnings("checkstyle:RedundantModifier")
43         public Proxy() {
44             // For Externalizable
45         }
46
47         @Override
48         protected TransactionIdentifier readIdentifier(final DataInput in) throws IOException {
49             return TransactionIdentifier.readFrom(in);
50         }
51
52         @Override
53         protected AbortTransactionPayload createObject(final TransactionIdentifier identifier,
54                 final byte[] serialized) {
55             return new Magnesium(identifier, serialized);
56         }
57     }
58
59     private static final Logger LOG = LoggerFactory.getLogger(AbortTransactionPayload.class);
60     @java.io.Serial
61     private static final long serialVersionUID = 1L;
62     private static final int PROXY_SIZE = externalizableProxySize(AT::new);
63
64     AbortTransactionPayload(final TransactionIdentifier transactionId, final byte[] serialized) {
65         super(transactionId, serialized);
66     }
67
68     public static AbortTransactionPayload create(final TransactionIdentifier transactionId,
69             final int initialSerializedBufferCapacity) {
70         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
71         try {
72             transactionId.writeTo(out);
73         } catch (IOException e) {
74             // This should never happen
75             LOG.error("Failed to serialize {}", transactionId, e);
76             throw new IllegalStateException("Failed to serialized " + transactionId, e);
77         }
78         return new AbortTransactionPayload(transactionId, out.toByteArray());
79     }
80
81     @Override
82     protected AT externalizableProxy(final byte[] serialized) {
83         return new AT(serialized);
84     }
85
86     @Override
87     protected int externalizableProxySize() {
88         return PROXY_SIZE;
89     }
90 }