Mechanical code cleanup (sal-akka-raft)
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / MockRaftActorContext.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;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import akka.japi.Procedure;
16 import java.io.Serializable;
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
20 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
21 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class MockRaftActorContext extends RaftActorContextImpl {
27     private static final Logger LOG = LoggerFactory.getLogger(MockRaftActorContext.class);
28
29     private ActorSystem system;
30     private RaftPolicy raftPolicy;
31
32     private static ElectionTerm newElectionTerm() {
33         return new ElectionTerm() {
34             private long currentTerm = 1;
35             private String votedFor = "";
36
37             @Override
38             public long getCurrentTerm() {
39                 return currentTerm;
40             }
41
42             @Override
43             public String getVotedFor() {
44                 return votedFor;
45             }
46
47             @Override
48             public void update(long currentTerm, String votedFor){
49                 this.currentTerm = currentTerm;
50                 this.votedFor = votedFor;
51
52                 // TODO : Write to some persistent state
53             }
54
55             @Override public void updateAndPersist(long currentTerm,
56                 String votedFor) {
57                 update(currentTerm, votedFor);
58             }
59         };
60     }
61
62     public MockRaftActorContext(){
63         super(null, null, "test", newElectionTerm(), -1, -1, new HashMap<>(),
64                 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), LOG);
65         setReplicatedLog(new MockReplicatedLogBuilder().build());
66     }
67
68     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
69         super(actor, null, id, newElectionTerm(), -1, -1, new HashMap<>(),
70                 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), LOG);
71
72         this.system = system;
73
74         initReplicatedLog();
75     }
76
77
78     public void initReplicatedLog(){
79         SimpleReplicatedLog replicatedLog = new SimpleReplicatedLog();
80         long term = getTermInformation().getCurrentTerm();
81         replicatedLog.append(new MockReplicatedLogEntry(term, 0, new MockPayload("1")));
82         replicatedLog.append(new MockReplicatedLogEntry(term, 1, new MockPayload("2")));
83         setReplicatedLog(replicatedLog);
84         setCommitIndex(replicatedLog.lastIndex());
85         setLastApplied(replicatedLog.lastIndex());
86     }
87
88     @Override public ActorRef actorOf(Props props) {
89         return system.actorOf(props);
90     }
91
92     @Override public ActorSelection actorSelection(String path) {
93         return system.actorSelection(path);
94     }
95
96     @Override public ActorSystem getActorSystem() {
97         return this.system;
98     }
99
100     @Override public ActorSelection getPeerActorSelection(String peerId) {
101         String peerAddress = getPeerAddress(peerId);
102         if(peerAddress != null){
103             return actorSelection(peerAddress);
104         }
105         return null;
106     }
107
108     public void setPeerAddresses(Map<String, String> peerAddresses) {
109         for(String id: getPeerIds()) {
110             removePeer(id);
111         }
112
113         for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
114             addToPeers(e.getKey(), e.getValue(), VotingState.VOTING);
115         }
116     }
117
118     @Override
119     public SnapshotManager getSnapshotManager() {
120         SnapshotManager snapshotManager = super.getSnapshotManager();
121         snapshotManager.setCreateSnapshotRunnable(() -> { });
122         return snapshotManager;
123     }
124
125     @Override
126     public RaftPolicy getRaftPolicy() {
127         return raftPolicy != null ? raftPolicy : super.getRaftPolicy();
128     }
129
130     public void setRaftPolicy(RaftPolicy raftPolicy) {
131         this.raftPolicy = raftPolicy;
132     }
133
134     public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
135         @Override
136         public void appendAndPersist(
137             ReplicatedLogEntry replicatedLogEntry) {
138             append(replicatedLogEntry);
139         }
140
141         @Override
142         public int dataSize() {
143             return -1;
144         }
145
146         @Override
147         public void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry) {
148         }
149
150         @Override
151         public boolean removeFromAndPersist(long index) {
152             return removeFrom(index) >= 0;
153         }
154
155         @Override
156         public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback) {
157             append(replicatedLogEntry);
158
159             if(callback != null) {
160                 try {
161                     callback.apply(replicatedLogEntry);
162                 } catch (Exception e) {
163                     e.printStackTrace();
164                 }
165             }
166         }
167     }
168
169     public static class MockPayload extends Payload implements Serializable {
170         private static final long serialVersionUID = 3121380393130864247L;
171         private String value = "";
172         private int size;
173
174         public MockPayload() {
175         }
176
177         public MockPayload(String s) {
178             this.value = s;
179             size = value.length();
180         }
181
182         public MockPayload(String s, int size) {
183             this(s);
184             this.size = size;
185         }
186
187         @Override
188         public int size() {
189             return size;
190         }
191
192         @Override
193         public String toString() {
194             return value;
195         }
196
197         @Override
198         public int hashCode() {
199             final int prime = 31;
200             int result = 1;
201             result = prime * result + ((value == null) ? 0 : value.hashCode());
202             return result;
203         }
204
205         @Override
206         public boolean equals(Object obj) {
207             if (this == obj) {
208                 return true;
209             }
210             if (obj == null) {
211                 return false;
212             }
213             if (getClass() != obj.getClass()) {
214                 return false;
215             }
216             MockPayload other = (MockPayload) obj;
217             if (value == null) {
218                 if (other.value != null) {
219                     return false;
220                 }
221             } else if (!value.equals(other.value)) {
222                 return false;
223             }
224             return true;
225         }
226     }
227
228     public static class MockReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
229         private static final long serialVersionUID = 1L;
230
231         private final long term;
232         private final long index;
233         private final Payload data;
234
235         public MockReplicatedLogEntry(long term, long index, Payload data){
236
237             this.term = term;
238             this.index = index;
239             this.data = data;
240         }
241
242         @Override public Payload getData() {
243             return data;
244         }
245
246         @Override public long getTerm() {
247             return term;
248         }
249
250         @Override public long getIndex() {
251             return index;
252         }
253
254         @Override
255         public int size() {
256             return getData().size();
257         }
258
259         @Override
260         public int hashCode() {
261             final int prime = 31;
262             int result = 1;
263             result = prime * result + ((data == null) ? 0 : data.hashCode());
264             result = prime * result + (int) (index ^ (index >>> 32));
265             result = prime * result + (int) (term ^ (term >>> 32));
266             return result;
267         }
268
269         @Override
270         public boolean equals(Object obj) {
271             if (this == obj) {
272                 return true;
273             }
274             if (obj == null) {
275                 return false;
276             }
277             if (getClass() != obj.getClass()) {
278                 return false;
279             }
280             MockReplicatedLogEntry other = (MockReplicatedLogEntry) obj;
281             if (data == null) {
282                 if (other.data != null) {
283                     return false;
284                 }
285             } else if (!data.equals(other.data)) {
286                 return false;
287             }
288             if (index != other.index) {
289                 return false;
290             }
291             if (term != other.term) {
292                 return false;
293             }
294             return true;
295         }
296
297         @Override
298         public String toString() {
299             StringBuilder builder = new StringBuilder();
300             builder.append("MockReplicatedLogEntry [term=").append(term).append(", index=").append(index)
301                     .append(", data=").append(data).append("]");
302             return builder.toString();
303         }
304     }
305
306     public static class MockReplicatedLogBuilder {
307         private final ReplicatedLog mockLog = new SimpleReplicatedLog();
308
309         public  MockReplicatedLogBuilder createEntries(int start, int end, int term) {
310             for (int i=start; i<end; i++) {
311                 this.mockLog.append(new ReplicatedLogImplEntry(i, term, new MockRaftActorContext.MockPayload(Integer.toString(i))));
312             }
313             return this;
314         }
315
316         public  MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
317             this.mockLog.append(new ReplicatedLogImplEntry(index, term, payload));
318             return this;
319         }
320
321         public ReplicatedLog build() {
322             return this.mockLog;
323         }
324     }
325
326     @Override
327     public void setCurrentBehavior(final RaftActorBehavior behavior) {
328         super.setCurrentBehavior(behavior);
329     }
330 }