BUG-5280: add CommitTransactionPayload
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayload.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.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.ByteArrayDataOutput;
13 import com.google.common.io.ByteStreams;
14 import java.io.DataInput;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.io.Serializable;
20 import java.util.AbstractMap.SimpleImmutableEntry;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26
27 /**
28  * Payload persisted when a transaction commits. It contains the transaction identifier and the
29  * {@link DataTreeCandidate}
30  *
31  * @author Robert Varga
32  */
33 @Beta
34 public final class CommitTransactionPayload extends Payload implements DataTreeCandidateSupplier, Serializable {
35     private static final class Proxy implements Externalizable {
36         private static final long serialVersionUID = 1L;
37         private byte[] serialized;
38
39         public Proxy() {
40             // For Externalizable
41         }
42
43         Proxy(final byte[] serialized) {
44             this.serialized = Preconditions.checkNotNull(serialized);
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             out.writeInt(serialized.length);
50             out.write(serialized);
51         }
52
53         @Override
54         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
55             final int length = in.readInt();
56             serialized = new byte[length];
57             in.readFully(serialized);
58         }
59
60         private Object readResolve() {
61             return new CommitTransactionPayload(serialized);
62         }
63     }
64
65     private static final long serialVersionUID = 1L;
66
67     private final byte[] serialized;
68
69     CommitTransactionPayload(final byte[] serialized) {
70         this.serialized = Preconditions.checkNotNull(serialized);
71     }
72
73     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
74             final DataTreeCandidate candidate) throws IOException {
75         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
76         transactionId.writeTo(out);
77         DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
78         return new CommitTransactionPayload(out.toByteArray());
79     }
80
81     @Override
82     public Entry<Optional<TransactionIdentifier>, DataTreeCandidate> getCandidate() throws IOException {
83         final DataInput in = ByteStreams.newDataInput(serialized);
84         return new SimpleImmutableEntry<>(Optional.of(TransactionIdentifier.readFrom(in)),
85                 DataTreeCandidateInputOutput.readDataTreeCandidate(in));
86     }
87
88     @Override
89     public int size() {
90         return serialized.length;
91     }
92
93     private Object writeReplace() {
94         return new Proxy(serialized);
95     }
96 }