2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
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;
28 * Payload persisted when a transaction commits. It contains the transaction identifier and the
29 * {@link DataTreeCandidate}
31 * @author Robert Varga
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;
43 Proxy(final byte[] serialized) {
44 this.serialized = Preconditions.checkNotNull(serialized);
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 out.writeInt(serialized.length);
50 out.write(serialized);
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);
60 private Object readResolve() {
61 return new CommitTransactionPayload(serialized);
65 private static final long serialVersionUID = 1L;
67 private final byte[] serialized;
69 CommitTransactionPayload(final byte[] serialized) {
70 this.serialized = Preconditions.checkNotNull(serialized);
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());
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));
90 return serialized.length;
93 private Object writeReplace() {
94 return new Proxy(serialized);