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