Add RaftActor integration tests
[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 com.google.common.base.Preconditions;
16 import com.google.protobuf.GeneratedMessage;
17 import java.io.Serializable;
18 import java.util.HashMap;
19 import java.util.Map;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
22 import org.opendaylight.controller.protobuff.messages.cluster.raft.test.MockPayloadMessages;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class MockRaftActorContext implements RaftActorContext {
27
28     private String id;
29     private ActorSystem system;
30     private ActorRef actor;
31     private long index = 0;
32     private long lastApplied = 0;
33     private final ElectionTerm electionTerm;
34     private ReplicatedLog replicatedLog;
35     private Map<String, String> peerAddresses = new HashMap<>();
36     private ConfigParams configParams;
37     private boolean snapshotCaptureInitiated;
38
39     public MockRaftActorContext(){
40         electionTerm = new ElectionTerm() {
41             private long currentTerm = 1;
42             private String votedFor = "";
43
44             @Override
45             public long getCurrentTerm() {
46                 return currentTerm;
47             }
48
49             @Override
50             public String getVotedFor() {
51                 return votedFor;
52             }
53
54             @Override
55             public void update(long currentTerm, String votedFor){
56                 this.currentTerm = currentTerm;
57                 this.votedFor = votedFor;
58
59                 // TODO : Write to some persistent state
60             }
61
62             @Override public void updateAndPersist(long currentTerm,
63                 String votedFor) {
64                 update(currentTerm, votedFor);
65             }
66         };
67
68         configParams = new DefaultConfigParamsImpl();
69     }
70
71     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
72         this();
73         this.id = id;
74         this.system = system;
75         this.actor = actor;
76
77         initReplicatedLog();
78     }
79
80
81     public void initReplicatedLog(){
82         this.replicatedLog = new SimpleReplicatedLog();
83         long term = getTermInformation().getCurrentTerm();
84         this.replicatedLog.append(new MockReplicatedLogEntry(term, 0, new MockPayload("1")));
85         this.replicatedLog.append(new MockReplicatedLogEntry(term, 1, new MockPayload("2")));
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 String getId() {
97         return id;
98     }
99
100     @Override public ActorRef getActor() {
101         return actor;
102     }
103
104     @Override public ElectionTerm getTermInformation() {
105         return electionTerm;
106     }
107
108     public void setIndex(long index){
109         this.index = index;
110     }
111
112     @Override public long getCommitIndex() {
113         return index;
114     }
115
116     @Override public void setCommitIndex(long commitIndex) {
117         this.index = commitIndex;
118     }
119
120     @Override public void setLastApplied(long lastApplied){
121         this.lastApplied = lastApplied;
122     }
123
124     @Override public long getLastApplied() {
125         return lastApplied;
126     }
127
128     @Override
129     // FIXME : A lot of tests try to manipulate the replicated log by setting it using this method
130     // This is OK to do if the underlyingActor is not RafActor or a derived class. If not then you should not
131     // used this way to manipulate the log because the RaftActor actually has a field replicatedLog
132     // which it creates internally and sets on the RaftActorContext
133     // The only right way to manipulate the replicated log therefore is to get it from either the RaftActor
134     // or the RaftActorContext and modify the entries in there instead of trying to replace it by using this setter
135     // Simple assertion that will fail if you do so
136     // ReplicatedLog log = new ReplicatedLogImpl();
137     // raftActor.underlyingActor().getRaftActorContext().setReplicatedLog(log);
138     // assertEquals(log, raftActor.underlyingActor().getReplicatedLog())
139     public void setReplicatedLog(ReplicatedLog replicatedLog) {
140         this.replicatedLog = replicatedLog;
141     }
142
143     @Override public ReplicatedLog getReplicatedLog() {
144         return replicatedLog;
145     }
146
147     @Override public ActorSystem getActorSystem() {
148         return this.system;
149     }
150
151     @Override public Logger getLogger() {
152         return LoggerFactory.getLogger(getClass());
153     }
154
155     @Override public Map<String, String> getPeerAddresses() {
156         return peerAddresses;
157     }
158
159     @Override public String getPeerAddress(String peerId) {
160         return peerAddresses.get(peerId);
161     }
162
163     @Override public void addToPeers(String name, String address) {
164         peerAddresses.put(name, address);
165     }
166
167     @Override public void removePeer(String name) {
168         peerAddresses.remove(name);
169     }
170
171     @Override public ActorSelection getPeerActorSelection(String peerId) {
172         String peerAddress = getPeerAddress(peerId);
173         if(peerAddress != null){
174             return actorSelection(peerAddress);
175         }
176         return null;
177     }
178
179     @Override public void setPeerAddress(String peerId, String peerAddress) {
180         Preconditions.checkState(peerAddresses.containsKey(peerId));
181         peerAddresses.put(peerId, peerAddress);
182     }
183
184     public void setPeerAddresses(Map<String, String> peerAddresses) {
185         this.peerAddresses = peerAddresses;
186     }
187
188     @Override
189     public ConfigParams getConfigParams() {
190         return configParams;
191     }
192
193     @Override
194     public void setSnapshotCaptureInitiated(boolean snapshotCaptureInitiated) {
195         this.snapshotCaptureInitiated = snapshotCaptureInitiated;
196     }
197
198     @Override
199     public boolean isSnapshotCaptureInitiated() {
200         return snapshotCaptureInitiated;
201     }
202
203     public void setConfigParams(ConfigParams configParams) {
204         this.configParams = configParams;
205     }
206
207     public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
208         @Override public void appendAndPersist(
209             ReplicatedLogEntry replicatedLogEntry) {
210             append(replicatedLogEntry);
211         }
212
213         @Override
214         public int dataSize() {
215             return -1;
216         }
217
218         @Override public void removeFromAndPersist(long index) {
219             removeFrom(index);
220         }
221     }
222
223     public static class MockPayload extends Payload implements Serializable {
224         private static final long serialVersionUID = 3121380393130864247L;
225         private String value = "";
226         private int size;
227
228         public MockPayload() {
229         }
230
231         public MockPayload(String s) {
232             this.value = s;
233             size = value.length();
234         }
235
236         public MockPayload(String s, int size) {
237             this(s);
238             this.size = size;
239         }
240
241         @Override public  Map<GeneratedMessage.GeneratedExtension, String> encode() {
242             Map<GeneratedMessage.GeneratedExtension, String> map = new HashMap<GeneratedMessage.GeneratedExtension, String>();
243             map.put(MockPayloadMessages.value, value);
244             return map;
245         }
246
247         @Override public Payload decode(
248             AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payloadProtoBuff) {
249             String value = payloadProtoBuff.getExtension(MockPayloadMessages.value);
250             this.value = value;
251             return this;
252         }
253
254         @Override
255         public int size() {
256             return size;
257         }
258
259         @Override public String getClientPayloadClassName() {
260             return MockPayload.class.getName();
261         }
262
263         @Override
264         public String toString() {
265             return value;
266         }
267
268         @Override
269         public int hashCode() {
270             final int prime = 31;
271             int result = 1;
272             result = prime * result + ((value == null) ? 0 : value.hashCode());
273             return result;
274         }
275
276         @Override
277         public boolean equals(Object obj) {
278             if (this == obj) {
279                 return true;
280             }
281             if (obj == null) {
282                 return false;
283             }
284             if (getClass() != obj.getClass()) {
285                 return false;
286             }
287             MockPayload other = (MockPayload) obj;
288             if (value == null) {
289                 if (other.value != null) {
290                     return false;
291                 }
292             } else if (!value.equals(other.value)) {
293                 return false;
294             }
295             return true;
296         }
297     }
298
299     public static class MockReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
300         private static final long serialVersionUID = 1L;
301
302         private final long term;
303         private final long index;
304         private final Payload data;
305
306         public MockReplicatedLogEntry(long term, long index, Payload data){
307
308             this.term = term;
309             this.index = index;
310             this.data = data;
311         }
312
313         @Override public Payload getData() {
314             return data;
315         }
316
317         @Override public long getTerm() {
318             return term;
319         }
320
321         @Override public long getIndex() {
322             return index;
323         }
324
325         @Override
326         public int size() {
327             return getData().size();
328         }
329
330         @Override
331         public int hashCode() {
332             final int prime = 31;
333             int result = 1;
334             result = prime * result + ((data == null) ? 0 : data.hashCode());
335             result = prime * result + (int) (index ^ (index >>> 32));
336             result = prime * result + (int) (term ^ (term >>> 32));
337             return result;
338         }
339
340         @Override
341         public boolean equals(Object obj) {
342             if (this == obj) {
343                 return true;
344             }
345             if (obj == null) {
346                 return false;
347             }
348             if (getClass() != obj.getClass()) {
349                 return false;
350             }
351             MockReplicatedLogEntry other = (MockReplicatedLogEntry) obj;
352             if (data == null) {
353                 if (other.data != null) {
354                     return false;
355                 }
356             } else if (!data.equals(other.data)) {
357                 return false;
358             }
359             if (index != other.index) {
360                 return false;
361             }
362             if (term != other.term) {
363                 return false;
364             }
365             return true;
366         }
367
368         @Override
369         public String toString() {
370             StringBuilder builder = new StringBuilder();
371             builder.append("MockReplicatedLogEntry [term=").append(term).append(", index=").append(index)
372                     .append(", data=").append(data).append("]");
373             return builder.toString();
374         }
375     }
376
377     public static class MockReplicatedLogBuilder {
378         private final ReplicatedLog mockLog = new SimpleReplicatedLog();
379
380         public  MockReplicatedLogBuilder createEntries(int start, int end, int term) {
381             for (int i=start; i<end; i++) {
382                 this.mockLog.append(new ReplicatedLogImplEntry(i, term, new MockRaftActorContext.MockPayload("foo" + i)));
383             }
384             return this;
385         }
386
387         public  MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
388             this.mockLog.append(new ReplicatedLogImplEntry(index, term, payload));
389             return this;
390         }
391
392         public ReplicatedLog build() {
393             return this.mockLog;
394         }
395     }
396 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.