2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft.messages;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.Optional;
18 import java.util.OptionalInt;
19 import org.opendaylight.controller.cluster.raft.RaftVersions;
20 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
24 * Serialization proxy for {@link InstallSnapshot}.
26 final class IS implements Externalizable {
28 private static final long serialVersionUID = 1L;
31 private static final int LAST_CHUNK_HASHCODE = 0x10;
32 private static final int SERVER_CONFIG = 0x20;
34 private InstallSnapshot installSnapshot;
36 @SuppressWarnings("checkstyle:RedundantModifier")
41 IS(final InstallSnapshot installSnapshot) {
42 this.installSnapshot = requireNonNull(installSnapshot);
46 public void writeExternal(final ObjectOutput out) throws IOException {
48 final var lastChunkHashCode = installSnapshot.getLastChunkHashCode();
49 if (lastChunkHashCode.isPresent()) {
50 flags |= LAST_CHUNK_HASHCODE;
52 final var serverConfig = installSnapshot.getServerConfig();
53 if (serverConfig.isPresent()) {
54 flags |= SERVER_CONFIG;
57 WritableObjects.writeLong(out, installSnapshot.getTerm(), flags);
58 out.writeObject(installSnapshot.getLeaderId());
59 WritableObjects.writeLongs(out, installSnapshot.getLastIncludedIndex(), installSnapshot.getLastIncludedTerm());
60 out.writeInt(installSnapshot.getChunkIndex());
61 out.writeInt(installSnapshot.getTotalChunks());
63 if (lastChunkHashCode.isPresent()) {
64 out.writeInt(lastChunkHashCode.getAsInt());
66 if (serverConfig.isPresent()) {
67 out.writeObject(serverConfig.get());
70 out.writeObject(installSnapshot.getData());
74 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
75 byte hdr = WritableObjects.readLongHeader(in);
76 final int flags = WritableObjects.longHeaderFlags(hdr);
78 long term = WritableObjects.readLongBody(in, hdr);
79 String leaderId = (String) in.readObject();
81 hdr = WritableObjects.readLongHeader(in);
82 long lastIncludedIndex = WritableObjects.readFirstLong(in, hdr);
83 long lastIncludedTerm = WritableObjects.readSecondLong(in, hdr);
84 int chunkIndex = in.readInt();
85 int totalChunks = in.readInt();
87 OptionalInt lastChunkHashCode = getFlag(flags, LAST_CHUNK_HASHCODE) ? OptionalInt.of(in.readInt())
88 : OptionalInt.empty();
89 Optional<ServerConfigurationPayload> serverConfig = getFlag(flags, SERVER_CONFIG)
90 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
92 byte[] data = (byte[])in.readObject();
94 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
95 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
99 private Object readResolve() {
100 return verifyNotNull(installSnapshot);
103 private static boolean getFlag(final int flags, final int bit) {
104 return (flags & bit) != 0;