b553bf92a2739cbb1e27474fd775fd7692281718
[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
16 /**
17  * Payload persisted when a transaction is aborted. It contains the transaction identifier.
18  *
19  * @author Robert Varga
20  */
21 public final class AbortTransactionPayload extends AbstractIdentifiablePayload<TransactionIdentifier> {
22     private static final class Proxy extends AbstractProxy<TransactionIdentifier> {
23         private static final long serialVersionUID = 1L;
24
25         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
26         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
27         @SuppressWarnings("checkstyle:RedundantModifier")
28         public Proxy() {
29             // For Externalizable
30         }
31
32         Proxy(final byte[] serialized) {
33             super(serialized);
34         }
35
36         @Override
37         protected TransactionIdentifier readIdentifier(final DataInput in) throws IOException {
38             return TransactionIdentifier.readFrom(in);
39         }
40
41         @Override
42         protected AbortTransactionPayload createObject(final TransactionIdentifier identifier,
43                 final byte[] serialized) {
44             return new AbortTransactionPayload(identifier, serialized);
45         }
46     }
47
48     private static final long serialVersionUID = 1L;
49
50     AbortTransactionPayload(final TransactionIdentifier transactionId, final byte[] serialized) {
51         super(transactionId, serialized);
52     }
53
54     public static AbortTransactionPayload create(final TransactionIdentifier transactionId) throws IOException {
55         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
56         transactionId.writeTo(out);
57         return new AbortTransactionPayload(transactionId, out.toByteArray());
58     }
59
60     @Override
61     protected Proxy externalizableProxy(final byte[] serialized) {
62         return new Proxy(serialized);
63     }
64 }