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