723de650c2e889f5ab2ca99df4a83f3661f4f779
[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<String, String>(),
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<String, String>(),
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     }
85
86     @Override public ActorRef actorOf(Props props) {
87         return system.actorOf(props);
88     }
89
90     @Override public ActorSelection actorSelection(String path) {
91         return system.actorSelection(path);
92     }
93
94     @Override public ActorSystem getActorSystem() {
95         return this.system;
96     }
97
98     @Override public ActorSelection getPeerActorSelection(String peerId) {
99         String peerAddress = getPeerAddress(peerId);
100         if(peerAddress != null){
101             return actorSelection(peerAddress);
102         }
103         return null;
104     }
105
106     public void setPeerAddresses(Map<String, String> peerAddresses) {
107         for(String id: getPeerIds()) {
108             removePeer(id);
109         }
110
111         for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
112             addToPeers(e.getKey(), e.getValue(), VotingState.VOTING);
113         }
114     }
115
116     @Override
117     public SnapshotManager getSnapshotManager() {
118         SnapshotManager snapshotManager = super.getSnapshotManager();
119         snapshotManager.setCreateSnapshotRunnable(() -> { });
120         return snapshotManager;
121     }
122
123     @Override
124     public RaftPolicy getRaftPolicy() {
125         return raftPolicy != null ? raftPolicy : super.getRaftPolicy();
126     }
127
128     public void setRaftPolicy(RaftPolicy raftPolicy) {
129         this.raftPolicy = raftPolicy;
130     }
131
132     public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
133         @Override
134         public void appendAndPersist(
135             ReplicatedLogEntry replicatedLogEntry) {
136             append(replicatedLogEntry);
137         }
138
139         @Override
140         public int dataSize() {
141             return -1;
142         }
143
144         @Override
145         public void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry) {
146         }
147
148         @Override public void removeFromAndPersist(long index) {
149             removeFrom(index);
150         }
151
152         @Override
153         public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback) {
154             append(replicatedLogEntry);
155
156             if(callback != null) {
157                 try {
158                     callback.apply(replicatedLogEntry);
159                 } catch (Exception e) {
160                     e.printStackTrace();
161                 }
162             }
163         }
164     }
165
166     public static class MockPayload extends Payload implements Serializable {
167         private static final long serialVersionUID = 3121380393130864247L;
168         private String value = "";
169         private int size;
170
171         public MockPayload() {
172         }
173
174         public MockPayload(String s) {
175             this.value = s;
176             size = value.length();
177         }
178
179         public MockPayload(String s, int size) {
180             this(s);
181             this.size = size;
182         }
183
184         @Override
185         public int size() {
186             return size;
187         }
188
189         @Override
190         public String toString() {
191             return value;
192         }
193
194         @Override
195         public int hashCode() {
196             final int prime = 31;
197             int result = 1;
198             result = prime * result + ((value == null) ? 0 : value.hashCode());
199             return result;
200         }
201
202         @Override
203         public boolean equals(Object obj) {
204             if (this == obj) {
205                 return true;
206             }
207             if (obj == null) {
208                 return false;
209             }
210             if (getClass() != obj.getClass()) {
211                 return false;
212             }
213             MockPayload other = (MockPayload) obj;
214             if (value == null) {
215                 if (other.value != null) {
216                     return false;
217                 }
218             } else if (!value.equals(other.value)) {
219                 return false;
220             }
221             return true;
222         }
223     }
224
225     public static class MockReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
226         private static final long serialVersionUID = 1L;
227
228         private final long term;
229         private final long index;
230         private final Payload data;
231
232         public MockReplicatedLogEntry(long term, long index, Payload data){
233
234             this.term = term;
235             this.index = index;
236             this.data = data;
237         }
238
239         @Override public Payload getData() {
240             return data;
241         }
242
243         @Override public long getTerm() {
244             return term;
245         }
246
247         @Override public long getIndex() {
248             return index;
249         }
250
251         @Override
252         public int size() {
253             return getData().size();
254         }
255
256         @Override
257         public int hashCode() {
258             final int prime = 31;
259             int result = 1;
260             result = prime * result + ((data == null) ? 0 : data.hashCode());
261             result = prime * result + (int) (index ^ (index >>> 32));
262             result = prime * result + (int) (term ^ (term >>> 32));
263             return result;
264         }
265
266         @Override
267         public boolean equals(Object obj) {
268             if (this == obj) {
269                 return true;
270             }
271             if (obj == null) {
272                 return false;
273             }
274             if (getClass() != obj.getClass()) {
275                 return false;
276             }
277             MockReplicatedLogEntry other = (MockReplicatedLogEntry) obj;
278             if (data == null) {
279                 if (other.data != null) {
280                     return false;
281                 }
282             } else if (!data.equals(other.data)) {
283                 return false;
284             }
285             if (index != other.index) {
286                 return false;
287             }
288             if (term != other.term) {
289                 return false;
290             }
291             return true;
292         }
293
294         @Override
295         public String toString() {
296             StringBuilder builder = new StringBuilder();
297             builder.append("MockReplicatedLogEntry [term=").append(term).append(", index=").append(index)
298                     .append(", data=").append(data).append("]");
299             return builder.toString();
300         }
301     }
302
303     public static class MockReplicatedLogBuilder {
304         private final ReplicatedLog mockLog = new SimpleReplicatedLog();
305
306         public  MockReplicatedLogBuilder createEntries(int start, int end, int term) {
307             for (int i=start; i<end; i++) {
308                 this.mockLog.append(new ReplicatedLogImplEntry(i, term, new MockRaftActorContext.MockPayload(Integer.toString(i))));
309             }
310             return this;
311         }
312
313         public  MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
314             this.mockLog.append(new ReplicatedLogImplEntry(index, term, payload));
315             return this;
316         }
317
318         public ReplicatedLog build() {
319             return this.mockLog;
320         }
321     }
322
323     @Override
324     public void setCurrentBehavior(final RaftActorBehavior behavior) {
325         super.setCurrentBehavior(behavior);
326     }
327 }