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 org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
15 public class InstallSnapshot extends AbstractRaftRPC {
17 public static final Class<InstallSnapshotMessages.InstallSnapshot> SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
18 private static final long serialVersionUID = 1L;
20 private final String leaderId;
21 private final long lastIncludedIndex;
22 private final long lastIncludedTerm;
23 private final ByteString data;
24 private final int chunkIndex;
25 private final int totalChunks;
26 private final Optional<Integer> lastChunkHashCode;
28 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
29 long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode) {
31 this.leaderId = leaderId;
32 this.lastIncludedIndex = lastIncludedIndex;
33 this.lastIncludedTerm = lastIncludedTerm;
35 this.chunkIndex = chunkIndex;
36 this.totalChunks = totalChunks;
37 this.lastChunkHashCode = lastChunkHashCode;
40 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
41 long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks) {
42 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, Optional.<Integer>absent());
46 public String getLeaderId() {
50 public long getLastIncludedIndex() {
51 return lastIncludedIndex;
54 public long getLastIncludedTerm() {
55 return lastIncludedTerm;
58 public ByteString getData() {
62 public int getChunkIndex() {
66 public int getTotalChunks() {
70 public Optional<Integer> getLastChunkHashCode() {
71 return lastChunkHashCode;
74 public <T extends Object> Object toSerializable(){
75 InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder()
76 .setLeaderId(this.getLeaderId())
77 .setChunkIndex(this.getChunkIndex())
78 .setData(this.getData())
79 .setLastIncludedIndex(this.getLastIncludedIndex())
80 .setLastIncludedTerm(this.getLastIncludedTerm())
81 .setTotalChunks(this.getTotalChunks());
83 if(lastChunkHashCode.isPresent()){
84 builder.setLastChunkHashCode(lastChunkHashCode.get());
86 return builder.build();
89 public static InstallSnapshot fromSerializable (Object o) {
90 InstallSnapshotMessages.InstallSnapshot from =
91 (InstallSnapshotMessages.InstallSnapshot) o;
93 Optional<Integer> lastChunkHashCode = Optional.absent();
94 if(from.hasLastChunkHashCode()){
95 lastChunkHashCode = Optional.of(from.getLastChunkHashCode());
98 InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
99 from.getLeaderId(), from.getLastIncludedIndex(),
100 from.getLastIncludedTerm(), from.getData(),
101 from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode);
103 return installSnapshot;