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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.io.ByteArrayDataOutput;
15 import com.google.common.io.ByteStreams;
16 import java.io.DataInput;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.io.Serializable;
22 import java.util.AbstractMap.SimpleImmutableEntry;
23 import java.util.Map.Entry;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Payload persisted when a transaction commits. It contains the transaction identifier and the
33 * {@link DataTreeCandidate}
35 * @author Robert Varga
38 public final class CommitTransactionPayload extends Payload implements Serializable {
39 private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
41 private static final class Proxy implements Externalizable {
42 private static final long serialVersionUID = 1L;
43 private byte[] serialized;
45 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
46 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
47 @SuppressWarnings("checkstyle:RedundantModifier")
52 Proxy(final byte[] serialized) {
53 this.serialized = requireNonNull(serialized);
57 public void writeExternal(final ObjectOutput out) throws IOException {
58 out.writeInt(serialized.length);
59 out.write(serialized);
63 public void readExternal(final ObjectInput in) throws IOException {
64 final int length = in.readInt();
65 serialized = new byte[length];
66 in.readFully(serialized);
69 private Object readResolve() {
70 return new CommitTransactionPayload(serialized);
74 private static final long serialVersionUID = 1L;
76 private final byte[] serialized;
78 CommitTransactionPayload(final byte[] serialized) {
79 this.serialized = requireNonNull(serialized);
82 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
83 final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
84 final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
85 transactionId.writeTo(out);
86 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
87 final byte[] serialized = out.toByteArray();
89 LOG.debug("Initial buffer capacity {}, actual serialized size {}",
90 initialSerializedBufferCapacity, serialized.length);
92 return new CommitTransactionPayload(serialized);
96 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
97 final DataTreeCandidate candidate) throws IOException {
98 return create(transactionId, candidate, 512);
101 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
102 return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
105 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(
106 final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
107 final DataInput in = ByteStreams.newDataInput(serialized);
108 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
109 DataTreeCandidateInputOutput.readDataTreeCandidate(in, writer));
114 return serialized.length;
117 private Object writeReplace() {
118 return new Proxy(serialized);