Bug-1830:Move install snapshot messages from akka-raft to sal-commons
[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
9 package org.opendaylight.controller.cluster.raft.messages;
10
11 import com.google.protobuf.ByteString;
12 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
13
14 public class InstallSnapshot extends AbstractRaftRPC {
15
16     public static final Class SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
17
18     private final String leaderId;
19     private final long lastIncludedIndex;
20     private final long lastIncludedTerm;
21     private final ByteString data;
22     private final int chunkIndex;
23     private final int totalChunks;
24
25     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
26         long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks) {
27         super(term);
28         this.leaderId = leaderId;
29         this.lastIncludedIndex = lastIncludedIndex;
30         this.lastIncludedTerm = lastIncludedTerm;
31         this.data = data;
32         this.chunkIndex = chunkIndex;
33         this.totalChunks = totalChunks;
34     }
35
36     public String getLeaderId() {
37         return leaderId;
38     }
39
40     public long getLastIncludedIndex() {
41         return lastIncludedIndex;
42     }
43
44     public long getLastIncludedTerm() {
45         return lastIncludedTerm;
46     }
47
48     public ByteString getData() {
49         return data;
50     }
51
52     public int getChunkIndex() {
53         return chunkIndex;
54     }
55
56     public int getTotalChunks() {
57         return totalChunks;
58     }
59
60     public <T extends Object> Object toSerializable(){
61         return InstallSnapshotMessages.InstallSnapshot.newBuilder()
62             .setLeaderId(this.getLeaderId())
63             .setChunkIndex(this.getChunkIndex())
64             .setData(this.getData())
65             .setLastIncludedIndex(this.getLastIncludedIndex())
66             .setLastIncludedTerm(this.getLastIncludedTerm())
67             .setTotalChunks(this.getTotalChunks()).build();
68
69     }
70
71     public static InstallSnapshot fromSerializable (Object o) {
72         InstallSnapshotMessages.InstallSnapshot from =
73             (InstallSnapshotMessages.InstallSnapshot) o;
74
75         InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
76             from.getLeaderId(), from.getLastIncludedIndex(),
77             from.getLastIncludedTerm(), from.getData(),
78             from.getChunkIndex(), from.getTotalChunks());
79
80         return installSnapshot;
81     }
82 }