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 org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 * Payload persisted when a transaction commits. It contains the transaction identifier and the
28 * {@link DataTreeCandidate}
30 * @author Robert Varga
33 public final class CommitTransactionPayload extends Payload implements Serializable {
34 private static final class Proxy implements Externalizable {
35 private static final long serialVersionUID = 1L;
36 private byte[] serialized;
42 Proxy(final byte[] serialized) {
43 this.serialized = Preconditions.checkNotNull(serialized);
47 public void writeExternal(final ObjectOutput out) throws IOException {
48 out.writeInt(serialized.length);
49 out.write(serialized);
53 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54 final int length = in.readInt();
55 serialized = new byte[length];
56 in.readFully(serialized);
59 private Object readResolve() {
60 return new CommitTransactionPayload(serialized);
64 private static final long serialVersionUID = 1L;
66 private final byte[] serialized;
68 CommitTransactionPayload(final byte[] serialized) {
69 this.serialized = Preconditions.checkNotNull(serialized);
72 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
73 final DataTreeCandidate candidate) throws IOException {
74 final ByteArrayDataOutput out = ByteStreams.newDataOutput();
75 transactionId.writeTo(out);
76 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
77 return new CommitTransactionPayload(out.toByteArray());
80 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
81 final DataInput in = ByteStreams.newDataInput(serialized);
82 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
83 DataTreeCandidateInputOutput.readDataTreeCandidate(in));
88 return serialized.length;
91 private Object writeReplace() {
92 return new Proxy(serialized);