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.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Payload persisted when a transaction commits. It contains the transaction identifier and the
32 * {@link DataTreeCandidate}
34 * @author Robert Varga
37 public final class CommitTransactionPayload extends Payload implements Serializable {
38 private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
40 private static final class Proxy implements Externalizable {
41 private static final long serialVersionUID = 1L;
42 private byte[] serialized;
44 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
45 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
46 @SuppressWarnings("checkstyle:RedundantModifier")
51 Proxy(final byte[] serialized) {
52 this.serialized = Preconditions.checkNotNull(serialized);
56 public void writeExternal(final ObjectOutput out) throws IOException {
57 out.writeInt(serialized.length);
58 out.write(serialized);
62 public void readExternal(final ObjectInput in) throws IOException {
63 final int length = in.readInt();
64 serialized = new byte[length];
65 in.readFully(serialized);
68 private Object readResolve() {
69 return new CommitTransactionPayload(serialized);
73 private static final long serialVersionUID = 1L;
75 private final byte[] serialized;
77 CommitTransactionPayload(final byte[] serialized) {
78 this.serialized = Preconditions.checkNotNull(serialized);
81 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
82 final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
83 final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
84 transactionId.writeTo(out);
85 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
86 final byte[] serialized = out.toByteArray();
88 LOG.debug("Initial buffer capacity {}, actual serialized size {}",
89 initialSerializedBufferCapacity, serialized.length);
91 return new CommitTransactionPayload(serialized);
95 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
96 final DataTreeCandidate candidate) throws IOException {
97 return create(transactionId, candidate, 512);
100 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
101 return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
104 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(
105 final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
106 final DataInput in = ByteStreams.newDataInput(serialized);
107 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
108 DataTreeCandidateInputOutput.readDataTreeCandidate(in, writer));
113 return serialized.length;
116 private Object writeReplace() {
117 return new Proxy(serialized);