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