7ded55b3ce91686d93f26d1dafe9f15c2585a1c3
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / client / messages / OnDemandRaftState.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.raft.client.messages;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15
16 /**
17  * The response to a GetOnDemandRaftState message.
18  *
19  * @author Thomas Pantelis
20  */
21 public class OnDemandRaftState {
22     private long lastLogIndex = -1L;
23     private long lastLogTerm = -1L;
24     private long currentTerm = -1L;
25     private long commitIndex = -1L;
26     private long lastApplied = -1L;
27     private long lastIndex = -1L;
28     private long lastTerm = -1L;
29     private long snapshotIndex = -1L;
30     private long snapshotTerm = -1L;
31     private long replicatedToAllIndex = -1L;
32     private long inMemoryJournalDataSize;
33     private long inMemoryJournalLogSize;
34     private String leader;
35     private String raftState;
36     private String votedFor;
37     private boolean isSnapshotCaptureInitiated;
38     private String customRaftPolicyClassName;
39     private boolean isVoting;
40
41     private List<FollowerInfo> followerInfoList = Collections.emptyList();
42     private Map<String, String> peerAddresses = Collections.emptyMap();
43     private Map<String, Boolean> peerVotingStates = Collections.emptyMap();
44
45     protected OnDemandRaftState() {
46     }
47
48     public static Builder builder() {
49         return new Builder();
50     }
51
52     public long getLastLogIndex() {
53         return lastLogIndex;
54     }
55
56     public long getLastLogTerm() {
57         return lastLogTerm;
58     }
59
60     public long getCurrentTerm() {
61         return currentTerm;
62     }
63
64     public long getCommitIndex() {
65         return commitIndex;
66     }
67
68     public long getLastApplied() {
69         return lastApplied;
70     }
71
72     public long getLastIndex() {
73         return lastIndex;
74     }
75
76     public long getLastTerm() {
77         return lastTerm;
78     }
79
80     public long getSnapshotIndex() {
81         return snapshotIndex;
82     }
83
84     public long getSnapshotTerm() {
85         return snapshotTerm;
86     }
87
88     public long getReplicatedToAllIndex() {
89         return replicatedToAllIndex;
90     }
91
92     public long getInMemoryJournalDataSize() {
93         return inMemoryJournalDataSize;
94     }
95
96     public long getInMemoryJournalLogSize() {
97         return inMemoryJournalLogSize;
98     }
99
100     public String getLeader() {
101         return leader;
102     }
103
104     public String getRaftState() {
105         return raftState;
106     }
107
108     public String getVotedFor() {
109         return votedFor;
110     }
111
112     public boolean isSnapshotCaptureInitiated() {
113         return isSnapshotCaptureInitiated;
114     }
115
116     public boolean isVoting() {
117         return isVoting;
118     }
119
120     public List<FollowerInfo> getFollowerInfoList() {
121         return followerInfoList;
122     }
123
124     public Map<String, String> getPeerAddresses() {
125         return peerAddresses;
126     }
127
128     public Map<String, Boolean> getPeerVotingStates() {
129         return peerVotingStates;
130     }
131
132     public String getCustomRaftPolicyClassName() {
133         return customRaftPolicyClassName;
134     }
135
136     public abstract static class AbstractBuilder<B extends AbstractBuilder<B, T>, T extends OnDemandRaftState> {
137         @SuppressWarnings("unchecked")
138         protected B self() {
139             return (B) this;
140         }
141
142         protected abstract @NonNull OnDemandRaftState state();
143
144         public B lastLogIndex(long value) {
145             state().lastLogIndex = value;
146             return self();
147         }
148
149         public B lastLogTerm(long value) {
150             state().lastLogTerm = value;
151             return self();
152         }
153
154         public B currentTerm(long value) {
155             state().currentTerm = value;
156             return self();
157         }
158
159         public B commitIndex(long value) {
160             state().commitIndex = value;
161             return self();
162         }
163
164         public B lastApplied(long value) {
165             state().lastApplied = value;
166             return self();
167         }
168
169         public B lastIndex(long value) {
170             state().lastIndex = value;
171             return self();
172         }
173
174         public B lastTerm(long value) {
175             state().lastTerm = value;
176             return self();
177         }
178
179         public B snapshotIndex(long value) {
180             state().snapshotIndex = value;
181             return self();
182         }
183
184         public B snapshotTerm(long value) {
185             state().snapshotTerm = value;
186             return self();
187         }
188
189         public B replicatedToAllIndex(long value) {
190             state().replicatedToAllIndex = value;
191             return self();
192         }
193
194         public B inMemoryJournalDataSize(long value) {
195             state().inMemoryJournalDataSize = value;
196             return self();
197         }
198
199         public B inMemoryJournalLogSize(long value) {
200             state().inMemoryJournalLogSize = value;
201             return self();
202         }
203
204         public B leader(String value) {
205             state().leader = value;
206             return self();
207         }
208
209         public B raftState(String value) {
210             state().raftState = value;
211             return self();
212         }
213
214         public B votedFor(String value) {
215             state().votedFor = value;
216             return self();
217         }
218
219         public B isVoting(boolean isVoting) {
220             state().isVoting = isVoting;
221             return self();
222         }
223
224         public B followerInfoList(List<FollowerInfo> followerInfoList) {
225             state().followerInfoList = followerInfoList;
226             return self();
227         }
228
229         public B peerAddresses(Map<String, String> peerAddresses) {
230             state().peerAddresses = peerAddresses;
231             return self();
232         }
233
234         public B peerVotingStates(Map<String, Boolean> peerVotingStates) {
235             state().peerVotingStates = ImmutableMap.copyOf(peerVotingStates);
236             return self();
237         }
238
239         public B isSnapshotCaptureInitiated(boolean value) {
240             state().isSnapshotCaptureInitiated = value;
241             return self();
242         }
243
244         public B customRaftPolicyClassName(String className) {
245             state().customRaftPolicyClassName = className;
246             return self();
247         }
248
249         @SuppressWarnings("unchecked")
250         public T build() {
251             return (T) state();
252         }
253     }
254
255     public static class Builder extends AbstractBuilder<Builder, OnDemandRaftState> {
256         private final OnDemandRaftState state = new OnDemandRaftState();
257
258         @Override
259         protected OnDemandRaftState state() {
260             return state;
261         }
262     }
263 }