Use ReusableNormalizedNodeReceiver
[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 static java.util.Objects.requireNonNull;
11
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;
31
32 /**
33  * Payload persisted when a transaction commits. It contains the transaction identifier and the
34  * {@link DataTreeCandidate}
35  *
36  * @author Robert Varga
37  */
38 @Beta
39 public final class CommitTransactionPayload extends Payload implements Serializable {
40     private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
41
42     private static final class Proxy implements Externalizable {
43         private static final long serialVersionUID = 1L;
44         private byte[] serialized;
45
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")
49         public Proxy() {
50             // For Externalizable
51         }
52
53         Proxy(final byte[] serialized) {
54             this.serialized = requireNonNull(serialized);
55         }
56
57         @Override
58         public void writeExternal(final ObjectOutput out) throws IOException {
59             out.writeInt(serialized.length);
60             out.write(serialized);
61         }
62
63         @Override
64         public void readExternal(final ObjectInput in) throws IOException {
65             final int length = in.readInt();
66             serialized = new byte[length];
67             in.readFully(serialized);
68         }
69
70         private Object readResolve() {
71             return new CommitTransactionPayload(serialized);
72         }
73     }
74
75     private static final long serialVersionUID = 1L;
76
77     private final byte[] serialized;
78
79     CommitTransactionPayload(final byte[] serialized) {
80         this.serialized = requireNonNull(serialized);
81     }
82
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();
89
90         LOG.debug("Initial buffer capacity {}, actual serialized size {}",
91                 initialSerializedBufferCapacity, serialized.length);
92
93         return new CommitTransactionPayload(serialized);
94     }
95
96     @VisibleForTesting
97     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
98             final DataTreeCandidate candidate) throws IOException {
99         return create(transactionId, candidate, 512);
100     }
101
102     public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
103         return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
104     }
105
106     public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(final ReusableStreamReceiver receiver)
107             throws IOException {
108         final DataInput in = ByteStreams.newDataInput(serialized);
109         return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
110                 DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
111     }
112
113     @Override
114     public int size() {
115         return serialized.length;
116     }
117
118     private Object writeReplace() {
119         return new Proxy(serialized);
120     }
121 }