Merge "Fix raw references to SimpleNode"
[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<InstallSnapshotMessages.InstallSnapshot> SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
17     private static final long serialVersionUID = 1L;
18
19     private final String leaderId;
20     private final long lastIncludedIndex;
21     private final long lastIncludedTerm;
22     private final ByteString data;
23     private final int chunkIndex;
24     private final int totalChunks;
25
26     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
27         long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks) {
28         super(term);
29         this.leaderId = leaderId;
30         this.lastIncludedIndex = lastIncludedIndex;
31         this.lastIncludedTerm = lastIncludedTerm;
32         this.data = data;
33         this.chunkIndex = chunkIndex;
34         this.totalChunks = totalChunks;
35     }
36
37     public String getLeaderId() {
38         return leaderId;
39     }
40
41     public long getLastIncludedIndex() {
42         return lastIncludedIndex;
43     }
44
45     public long getLastIncludedTerm() {
46         return lastIncludedTerm;
47     }
48
49     public ByteString getData() {
50         return data;
51     }
52
53     public int getChunkIndex() {
54         return chunkIndex;
55     }
56
57     public int getTotalChunks() {
58         return totalChunks;
59     }
60
61     public <T extends Object> Object toSerializable(){
62         return InstallSnapshotMessages.InstallSnapshot.newBuilder()
63             .setLeaderId(this.getLeaderId())
64             .setChunkIndex(this.getChunkIndex())
65             .setData(this.getData())
66             .setLastIncludedIndex(this.getLastIncludedIndex())
67             .setLastIncludedTerm(this.getLastIncludedTerm())
68             .setTotalChunks(this.getTotalChunks()).build();
69
70     }
71
72     public static InstallSnapshot fromSerializable (Object o) {
73         InstallSnapshotMessages.InstallSnapshot from =
74             (InstallSnapshotMessages.InstallSnapshot) o;
75
76         InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
77             from.getLeaderId(), from.getLastIncludedIndex(),
78             from.getLastIncludedTerm(), from.getData(),
79             from.getChunkIndex(), from.getTotalChunks());
80
81         return installSnapshot;
82     }
83 }