Annotate AbstractRaftRPC with java.io.Serial
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / InstallSnapshot.java
1 /*
2  * Copyright (c) 2014 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.raft.messages;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import java.util.OptionalInt;
18 import org.opendaylight.controller.cluster.raft.RaftVersions;
19 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
20
21 /**
22  * Message sent from a leader to install a snapshot chunk on a follower.
23  */
24 public final class InstallSnapshot extends AbstractRaftRPC {
25     @java.io.Serial
26     private static final long serialVersionUID = 1L;
27
28     private final String leaderId;
29     private final long lastIncludedIndex;
30     private final long lastIncludedTerm;
31     private final byte[] data;
32     private final int chunkIndex;
33     private final int totalChunks;
34     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
35     private final OptionalInt lastChunkHashCode;
36     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
37     private final Optional<ServerConfigurationPayload> serverConfig;
38     private final short recipientRaftVersion;
39
40     @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
41             + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
42             + "Also it would be inefficient to create a copy as the byte[] could be large.")
43     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
44             final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
45             final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
46             final short recipientRaftVersion) {
47         super(term);
48         this.leaderId = leaderId;
49         this.lastIncludedIndex = lastIncludedIndex;
50         this.lastIncludedTerm = lastIncludedTerm;
51         this.data = data;
52         this.chunkIndex = chunkIndex;
53         this.totalChunks = totalChunks;
54         this.lastChunkHashCode = lastChunkHashCode;
55         this.serverConfig = serverConfig;
56         this.recipientRaftVersion = recipientRaftVersion;
57     }
58
59     @VisibleForTesting
60     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
61                            final long lastIncludedTerm, final byte[] data, final int chunkIndex,
62                            final int totalChunks) {
63         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
64             Optional.empty(), RaftVersions.CURRENT_VERSION);
65     }
66
67     public String getLeaderId() {
68         return leaderId;
69     }
70
71     public long getLastIncludedIndex() {
72         return lastIncludedIndex;
73     }
74
75     public long getLastIncludedTerm() {
76         return lastIncludedTerm;
77     }
78
79     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
80             + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
81             + "Also it would be inefficient to create a return copy as the byte[] could be large.")
82     public byte[] getData() {
83         return data;
84     }
85
86     public int getChunkIndex() {
87         return chunkIndex;
88     }
89
90     public int getTotalChunks() {
91         return totalChunks;
92     }
93
94     public OptionalInt getLastChunkHashCode() {
95         return lastChunkHashCode;
96     }
97
98     public Optional<ServerConfigurationPayload> getServerConfig() {
99         return serverConfig;
100     }
101
102     @Override
103     public String toString() {
104         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
105                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
106                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
107                 + ", serverConfig=" + serverConfig.orElse(null) + "]";
108     }
109
110     @Override
111     Object writeReplace() {
112         return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
113     }
114
115     private static class Proxy implements Externalizable {
116         @java.io.Serial
117         private static final long serialVersionUID = 1L;
118
119         private InstallSnapshot installSnapshot;
120
121         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
122         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
123         @SuppressWarnings("checkstyle:RedundantModifier")
124         public Proxy() {
125         }
126
127         Proxy(final InstallSnapshot installSnapshot) {
128             this.installSnapshot = installSnapshot;
129         }
130
131         @Override
132         public void writeExternal(final ObjectOutput out) throws IOException {
133             out.writeLong(installSnapshot.getTerm());
134             out.writeObject(installSnapshot.leaderId);
135             out.writeLong(installSnapshot.lastIncludedIndex);
136             out.writeLong(installSnapshot.lastIncludedTerm);
137             out.writeInt(installSnapshot.chunkIndex);
138             out.writeInt(installSnapshot.totalChunks);
139
140             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
141             if (installSnapshot.lastChunkHashCode.isPresent()) {
142                 out.writeInt(installSnapshot.lastChunkHashCode.getAsInt());
143             }
144
145             out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
146             if (installSnapshot.serverConfig.isPresent()) {
147                 out.writeObject(installSnapshot.serverConfig.get());
148             }
149
150             out.writeObject(installSnapshot.data);
151         }
152
153         @Override
154         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
155             long term = in.readLong();
156             String leaderId = (String) in.readObject();
157             long lastIncludedIndex = in.readLong();
158             long lastIncludedTerm = in.readLong();
159             int chunkIndex = in.readInt();
160             int totalChunks = in.readInt();
161
162             OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
163             Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
164                     ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
165
166             byte[] data = (byte[])in.readObject();
167
168             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
169                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
170         }
171
172         @java.io.Serial
173         private Object readResolve() {
174             return installSnapshot;
175         }
176     }
177 }