Fixup checkstyle
[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 = """
41         Stores a reference to an externally mutable byte[] object but this is OK since this class is merely a DTO and \
42         does not process byte[] internally. Also it would be inefficient to create a copy as the byte[] could be \
43         large.""")
44     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
45             final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
46             final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
47             final short recipientRaftVersion) {
48         super(term);
49         this.leaderId = leaderId;
50         this.lastIncludedIndex = lastIncludedIndex;
51         this.lastIncludedTerm = lastIncludedTerm;
52         this.data = data;
53         this.chunkIndex = chunkIndex;
54         this.totalChunks = totalChunks;
55         this.lastChunkHashCode = lastChunkHashCode;
56         this.serverConfig = serverConfig;
57         this.recipientRaftVersion = recipientRaftVersion;
58     }
59
60     @VisibleForTesting
61     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
62                            final long lastIncludedTerm, final byte[] data, final int chunkIndex,
63                            final int totalChunks) {
64         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
65             Optional.empty(), RaftVersions.CURRENT_VERSION);
66     }
67
68     public String getLeaderId() {
69         return leaderId;
70     }
71
72     public long getLastIncludedIndex() {
73         return lastIncludedIndex;
74     }
75
76     public long getLastIncludedTerm() {
77         return lastIncludedTerm;
78     }
79
80     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = """
81         Exposes a mutable object stored in a field but this is OK since this class is merely a DTO and does not \
82         process the byte[] internally. Also it would be inefficient to create a return copy as the byte[] could be \
83         large.""")
84     public byte[] getData() {
85         return data;
86     }
87
88     public int getChunkIndex() {
89         return chunkIndex;
90     }
91
92     public int getTotalChunks() {
93         return totalChunks;
94     }
95
96     public OptionalInt getLastChunkHashCode() {
97         return lastChunkHashCode;
98     }
99
100     public Optional<ServerConfigurationPayload> getServerConfig() {
101         return serverConfig;
102     }
103
104     @Override
105     public String toString() {
106         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
107                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
108                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
109                 + ", serverConfig=" + serverConfig.orElse(null) + "]";
110     }
111
112     @Override
113     Object writeReplace() {
114         return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
115     }
116
117     private static class Proxy implements Externalizable {
118         @java.io.Serial
119         private static final long serialVersionUID = 1L;
120
121         private InstallSnapshot installSnapshot;
122
123         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
124         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
125         @SuppressWarnings("checkstyle:RedundantModifier")
126         public Proxy() {
127         }
128
129         Proxy(final InstallSnapshot installSnapshot) {
130             this.installSnapshot = installSnapshot;
131         }
132
133         @Override
134         public void writeExternal(final ObjectOutput out) throws IOException {
135             out.writeLong(installSnapshot.getTerm());
136             out.writeObject(installSnapshot.leaderId);
137             out.writeLong(installSnapshot.lastIncludedIndex);
138             out.writeLong(installSnapshot.lastIncludedTerm);
139             out.writeInt(installSnapshot.chunkIndex);
140             out.writeInt(installSnapshot.totalChunks);
141
142             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
143             if (installSnapshot.lastChunkHashCode.isPresent()) {
144                 out.writeInt(installSnapshot.lastChunkHashCode.orElseThrow());
145             }
146
147             out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
148             if (installSnapshot.serverConfig.isPresent()) {
149                 out.writeObject(installSnapshot.serverConfig.orElseThrow());
150             }
151
152             out.writeObject(installSnapshot.data);
153         }
154
155         @Override
156         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
157             long term = in.readLong();
158             String leaderId = (String) in.readObject();
159             long lastIncludedIndex = in.readLong();
160             long lastIncludedTerm = in.readLong();
161             int chunkIndex = in.readInt();
162             int totalChunks = in.readInt();
163
164             OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
165             Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
166                     ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
167
168             byte[] data = (byte[])in.readObject();
169
170             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
171                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
172         }
173
174         @java.io.Serial
175         private Object readResolve() {
176             return installSnapshot;
177         }
178     }
179 }