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;
38 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
39 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
40 @SuppressWarnings("checkstyle:RedundantModifier")
45 Proxy(final byte[] serialized) {
46 this.serialized = Preconditions.checkNotNull(serialized);
50 public void writeExternal(final ObjectOutput out) throws IOException {
51 out.writeInt(serialized.length);
52 out.write(serialized);
56 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
57 final int length = in.readInt();
58 serialized = new byte[length];
59 in.readFully(serialized);
62 private Object readResolve() {
63 return new CommitTransactionPayload(serialized);
67 private static final long serialVersionUID = 1L;
69 private final byte[] serialized;
71 CommitTransactionPayload(final byte[] serialized) {
72 this.serialized = Preconditions.checkNotNull(serialized);
75 public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
76 final DataTreeCandidate candidate) throws IOException {
77 final ByteArrayDataOutput out = ByteStreams.newDataOutput();
78 transactionId.writeTo(out);
79 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
80 return new CommitTransactionPayload(out.toByteArray());
83 public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
84 final DataInput in = ByteStreams.newDataInput(serialized);
85 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
86 DataTreeCandidateInputOutput.readDataTreeCandidate(in));
91 return serialized.length;
94 private Object writeReplace() {
95 return new Proxy(serialized);