2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.raft.messages;
11 import com.google.common.base.Optional;
12 import com.google.protobuf.ByteString;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.controller.cluster.raft.RaftVersions;
18 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
19 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
21 public class InstallSnapshot extends AbstractRaftRPC implements Externalizable {
22 private static final long serialVersionUID = 1L;
23 public static final Class<InstallSnapshotMessages.InstallSnapshot> SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
25 private String leaderId;
26 private long lastIncludedIndex;
27 private long lastIncludedTerm;
29 private int chunkIndex;
30 private int totalChunks;
31 private Optional<Integer> lastChunkHashCode;
32 private Optional<ServerConfigurationPayload> serverConfig;
35 * Empty constructor to satisfy Externalizable.
37 public InstallSnapshot() {
40 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
41 int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode, Optional<ServerConfigurationPayload> serverConfig) {
43 this.leaderId = leaderId;
44 this.lastIncludedIndex = lastIncludedIndex;
45 this.lastIncludedTerm = lastIncludedTerm;
47 this.chunkIndex = chunkIndex;
48 this.totalChunks = totalChunks;
49 this.lastChunkHashCode = lastChunkHashCode;
50 this.serverConfig = serverConfig;
53 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
54 long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
55 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
56 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
59 public String getLeaderId() {
63 public long getLastIncludedIndex() {
64 return lastIncludedIndex;
67 public long getLastIncludedTerm() {
68 return lastIncludedTerm;
71 public byte[] getData() {
75 public int getChunkIndex() {
79 public int getTotalChunks() {
83 public Optional<Integer> getLastChunkHashCode() {
84 return lastChunkHashCode;
87 public Optional<ServerConfigurationPayload> getServerConfig() {
92 public void writeExternal(ObjectOutput out) throws IOException {
93 out.writeShort(RaftVersions.CURRENT_VERSION);
94 out.writeLong(getTerm());
95 out.writeUTF(leaderId);
96 out.writeLong(lastIncludedIndex);
97 out.writeLong(lastIncludedTerm);
98 out.writeInt(chunkIndex);
99 out.writeInt(totalChunks);
101 out.writeByte(lastChunkHashCode.isPresent() ? 1 : 0);
102 if(lastChunkHashCode.isPresent()) {
103 out.writeInt(lastChunkHashCode.get().intValue());
106 out.writeByte(serverConfig.isPresent() ? 1 : 0);
107 if(serverConfig.isPresent()) {
108 out.writeObject(serverConfig.get());
111 out.writeObject(data);
115 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
116 in.readShort(); // raft version - not currently used
117 setTerm(in.readLong());
118 leaderId = in.readUTF();
119 lastIncludedIndex = in.readLong();
120 lastIncludedTerm = in.readLong();
121 chunkIndex = in.readInt();
122 totalChunks = in.readInt();
124 lastChunkHashCode = Optional.absent();
125 boolean chunkHashCodePresent = in.readByte() == 1;
126 if(chunkHashCodePresent) {
127 lastChunkHashCode = Optional.of(in.readInt());
130 serverConfig = Optional.absent();
131 boolean serverConfigPresent = in.readByte() == 1;
132 if(serverConfigPresent) {
133 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
136 data = (byte[])in.readObject();
139 public <T extends Object> Object toSerializable(short version) {
140 if(version >= RaftVersions.BORON_VERSION) {
143 InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder()
144 .setTerm(this.getTerm())
145 .setLeaderId(this.getLeaderId())
146 .setChunkIndex(this.getChunkIndex())
147 .setData(ByteString.copyFrom(getData()))
148 .setLastIncludedIndex(this.getLastIncludedIndex())
149 .setLastIncludedTerm(this.getLastIncludedTerm())
150 .setTotalChunks(this.getTotalChunks());
152 if(lastChunkHashCode.isPresent()){
153 builder.setLastChunkHashCode(lastChunkHashCode.get());
155 return builder.build();
160 public String toString() {
161 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
162 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
163 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
164 + ", serverConfig=" + serverConfig.orNull() + "]";
167 public static InstallSnapshot fromSerializable (Object o) {
168 if(o instanceof InstallSnapshot) {
169 return (InstallSnapshot)o;
171 InstallSnapshotMessages.InstallSnapshot from =
172 (InstallSnapshotMessages.InstallSnapshot) o;
174 Optional<Integer> lastChunkHashCode = Optional.absent();
175 if(from.hasLastChunkHashCode()){
176 lastChunkHashCode = Optional.of(from.getLastChunkHashCode());
179 InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
180 from.getLeaderId(), from.getLastIncludedIndex(),
181 from.getLastIncludedTerm(), from.getData().toByteArray(),
182 from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode,
183 Optional.<ServerConfigurationPayload>absent());
185 return installSnapshot;
189 public static boolean isSerializedType(Object message) {
190 return message instanceof InstallSnapshot || message instanceof InstallSnapshotMessages.InstallSnapshot;