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.stream.ReusableStreamReceiver;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Payload persisted when a transaction commits. It contains the transaction identifier and the
34 * {@link DataTreeCandidate}
36 * @author Robert Varga
39 public final class CommitTransactionPayload extends Payload implements Serializable {
40 private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
42 private static final class Proxy implements Externalizable {
43 private static final long serialVersionUID = 1L;
44 private byte[] serialized;
46 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
47 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
48 @SuppressWarnings("checkstyle:RedundantModifier")
53 Proxy(final byte[] serialized) {
54 this.serialized = requireNonNull(serialized);
58 public void writeExternal(final ObjectOutput out) throws IOException {
59 out.writeInt(serialized.length);
60 out.write(serialized);
64 public void readExternal(final ObjectInput in) throws IOException {
65 final int length = in.readInt();
66 serialized = new byte[length];
67 in.readFully(serialized);
70 private Object readResolve() {
71 return new CommitTransactionPayload(serialized);
75 private static final long serialVersionUID = 1L;
77 private final byte[] serialized;
79 CommitTransactionPayload(final byte[] serialized) {
80 this.serialized = requireNonNull(serialized);
83 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
84 final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
85 final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
86 transactionId.writeTo(out);
87 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
88 final byte[] serialized = out.toByteArray();
90 LOG.debug("Initial buffer capacity {}, actual serialized size {}",
91 initialSerializedBufferCapacity, serialized.length);
93 return new CommitTransactionPayload(serialized);
97 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
98 final DataTreeCandidate candidate) throws IOException {
99 return create(transactionId, candidate, 512);
102 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
103 return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
106 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(final ReusableStreamReceiver receiver)
108 final DataInput in = ByteStreams.newDataInput(serialized);
109 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
110 DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
115 return serialized.length;
118 private Object writeReplace() {
119 return new Proxy(serialized);