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.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.io.ByteArrayDataOutput;
14 import com.google.common.io.ByteStreams;
15 import java.io.DataInput;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import java.io.Serializable;
21 import java.util.AbstractMap.SimpleImmutableEntry;
22 import java.util.Map.Entry;
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 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Payload persisted when a transaction commits. It contains the transaction identifier and the
31 * {@link DataTreeCandidate}
33 * @author Robert Varga
36 public final class CommitTransactionPayload extends Payload implements Serializable {
37 private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
39 private static final class Proxy implements Externalizable {
40 private static final long serialVersionUID = 1L;
41 private byte[] serialized;
43 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
44 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
45 @SuppressWarnings("checkstyle:RedundantModifier")
50 Proxy(final byte[] serialized) {
51 this.serialized = Preconditions.checkNotNull(serialized);
55 public void writeExternal(final ObjectOutput out) throws IOException {
56 out.writeInt(serialized.length);
57 out.write(serialized);
61 public void readExternal(final ObjectInput in) throws IOException {
62 final int length = in.readInt();
63 serialized = new byte[length];
64 in.readFully(serialized);
67 private Object readResolve() {
68 return new CommitTransactionPayload(serialized);
72 private static final long serialVersionUID = 1L;
74 private final byte[] serialized;
76 CommitTransactionPayload(final byte[] serialized) {
77 this.serialized = Preconditions.checkNotNull(serialized);
80 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
81 final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
82 final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
83 transactionId.writeTo(out);
84 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
85 final byte[] serialized = out.toByteArray();
87 LOG.debug("Initial buffer capacity {}, actual serialized size {}",
88 initialSerializedBufferCapacity, serialized.length);
90 return new CommitTransactionPayload(serialized);
94 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
95 final DataTreeCandidate candidate) throws IOException {
96 return create(transactionId, candidate, 512);
99 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
100 final DataInput in = ByteStreams.newDataInput(serialized);
101 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
102 DataTreeCandidateInputOutput.readDataTreeCandidate(in));
107 return serialized.length;
110 private Object writeReplace() {
111 return new Proxy(serialized);