213d61b8dda72ad24b6eb3577d43dad0d0d859ab
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayload.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
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;
25
26 /**
27  * Payload persisted when a transaction commits. It contains the transaction identifier and the
28  * {@link DataTreeCandidate}
29  *
30  * @author Robert Varga
31  */
32 @Beta
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;
37
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")
41         public Proxy() {
42             // For Externalizable
43         }
44
45         Proxy(final byte[] serialized) {
46             this.serialized = Preconditions.checkNotNull(serialized);
47         }
48
49         @Override
50         public void writeExternal(final ObjectOutput out) throws IOException {
51             out.writeInt(serialized.length);
52             out.write(serialized);
53         }
54
55         @Override
56         public void readExternal(final ObjectInput in) throws IOException {
57             final int length = in.readInt();
58             serialized = new byte[length];
59             in.readFully(serialized);
60         }
61
62         private Object readResolve() {
63             return new CommitTransactionPayload(serialized);
64         }
65     }
66
67     private static final long serialVersionUID = 1L;
68
69     private final byte[] serialized;
70
71     CommitTransactionPayload(final byte[] serialized) {
72         this.serialized = Preconditions.checkNotNull(serialized);
73     }
74
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());
81     }
82
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));
87     }
88
89     @Override
90     public int size() {
91         return serialized.length;
92     }
93
94     private Object writeReplace() {
95         return new Proxy(serialized);
96     }
97 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.