addf51a63cf3c3cb65baaed1d65da3a0c708c286
[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.event.Logging;
16 import akka.event.LoggingAdapter;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 public class MockRaftActorContext implements RaftActorContext {
25
26     private String id;
27     private ActorSystem system;
28     private ActorRef actor;
29     private long index = 0;
30     private long lastApplied = 0;
31     private final ElectionTerm electionTerm;
32     private ReplicatedLog replicatedLog;
33     private Map<String, String> peerAddresses = new HashMap();
34
35     public MockRaftActorContext(){
36         electionTerm = null;
37
38         initReplicatedLog();
39     }
40
41     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
42         this.id = id;
43         this.system = system;
44         this.actor = actor;
45
46         electionTerm = new ElectionTermImpl(id);
47
48         initReplicatedLog();
49     }
50
51
52     public void initReplicatedLog(){
53         MockReplicatedLog mockReplicatedLog = new MockReplicatedLog();
54         this.replicatedLog = mockReplicatedLog;
55         mockReplicatedLog.setLast(new MockReplicatedLogEntry(1,1,""));
56         mockReplicatedLog.setReplicatedLogEntry(new MockReplicatedLogEntry(1,1, ""));
57     }
58
59     @Override public ActorRef actorOf(Props props) {
60         return system.actorOf(props);
61     }
62
63     @Override public ActorSelection actorSelection(String path) {
64         return system.actorSelection(path);
65     }
66
67     @Override public String getId() {
68         return id;
69     }
70
71     @Override public ActorRef getActor() {
72         return actor;
73     }
74
75     @Override public ElectionTerm getTermInformation() {
76         return electionTerm;
77     }
78
79     public void setIndex(long index){
80         this.index = index;
81     }
82
83     @Override public long getCommitIndex() {
84         return index;
85     }
86
87     @Override public void setCommitIndex(long commitIndex) {
88         this.index = commitIndex;
89     }
90
91     @Override public void setLastApplied(long lastApplied){
92         this.lastApplied = lastApplied;
93     }
94
95     @Override public long getLastApplied() {
96         return lastApplied;
97     }
98
99     public void setReplicatedLog(ReplicatedLog replicatedLog) {
100         this.replicatedLog = replicatedLog;
101     }
102
103     @Override public ReplicatedLog getReplicatedLog() {
104         return replicatedLog;
105     }
106
107     @Override public ActorSystem getActorSystem() {
108         return this.system;
109     }
110
111     @Override public LoggingAdapter getLogger() {
112         return Logging.getLogger(system, this);
113     }
114
115     @Override public Map<String, String> getPeerAddresses() {
116         return peerAddresses;
117     }
118
119     @Override public String getPeerAddress(String peerId) {
120         return peerAddresses.get(peerId);
121     }
122
123     public void setPeerAddresses(Map<String, String> peerAddresses) {
124         this.peerAddresses = peerAddresses;
125     }
126
127
128     public static class MockReplicatedLog implements ReplicatedLog {
129         private ReplicatedLogEntry replicatedLogEntry = new MockReplicatedLogEntry(0,0, "");
130         private ReplicatedLogEntry last = new MockReplicatedLogEntry(0,0, "");
131
132         @Override public ReplicatedLogEntry get(long index) {
133             return replicatedLogEntry;
134         }
135
136         @Override public ReplicatedLogEntry last() {
137             return last;
138         }
139
140         @Override public long lastIndex() {
141             return last.getIndex();
142         }
143
144         @Override public long lastTerm() {
145             return last.getTerm();
146         }
147
148         @Override public void removeFrom(long index) {
149         }
150
151         @Override public void append(ReplicatedLogEntry replicatedLogEntry) {
152         }
153
154         @Override public void appendAndPersist(
155             ReplicatedLogEntry replicatedLogEntry) {
156         }
157
158         @Override public List<ReplicatedLogEntry> getFrom(long index) {
159             return Collections.EMPTY_LIST;
160         }
161
162         @Override public long size() {
163             if(replicatedLogEntry != null){
164                 return 1;
165             }
166             return 0;
167         }
168
169         public void setReplicatedLogEntry(
170             ReplicatedLogEntry replicatedLogEntry) {
171             this.replicatedLogEntry = replicatedLogEntry;
172         }
173
174         public void setLast(ReplicatedLogEntry last) {
175             this.last = last;
176         }
177     }
178
179     public static class SimpleReplicatedLog implements ReplicatedLog {
180         private final List<ReplicatedLogEntry> log = new ArrayList<>(10000);
181
182         @Override public ReplicatedLogEntry get(long index) {
183             if(index >= log.size() || index < 0){
184                 return null;
185             }
186             return log.get((int) index);
187         }
188
189         @Override public ReplicatedLogEntry last() {
190             if(log.size() == 0){
191                 return null;
192             }
193             return log.get(log.size()-1);
194         }
195
196         @Override public long lastIndex() {
197             if(log.size() == 0){
198                 return -1;
199             }
200
201             return last().getIndex();
202         }
203
204         @Override public long lastTerm() {
205             if(log.size() == 0){
206                 return -1;
207             }
208
209             return last().getTerm();
210         }
211
212         @Override public void removeFrom(long index) {
213             for(int i=(int) index ; i < log.size() ; i++) {
214                 log.remove(i);
215             }
216         }
217
218         @Override public void append(ReplicatedLogEntry replicatedLogEntry) {
219             log.add(replicatedLogEntry);
220         }
221
222         @Override public void appendAndPersist(
223             ReplicatedLogEntry replicatedLogEntry) {
224             append(replicatedLogEntry);
225         }
226
227         @Override public List<ReplicatedLogEntry> getFrom(long index) {
228             List<ReplicatedLogEntry> entries = new ArrayList<>();
229             for(int i=(int) index ; i < log.size() ; i++) {
230                 entries.add(get(i));
231             }
232             return entries;
233         }
234
235         @Override public long size() {
236             return log.size();
237         }
238     }
239
240     public static class MockReplicatedLogEntry implements ReplicatedLogEntry {
241
242         private final long term;
243         private final long index;
244         private final Object data;
245
246         public MockReplicatedLogEntry(long term, long index, Object data){
247
248             this.term = term;
249             this.index = index;
250             this.data = data;
251         }
252
253         @Override public Object getData() {
254             return data;
255         }
256
257         @Override public long getTerm() {
258             return term;
259         }
260
261         @Override public long getIndex() {
262             return index;
263         }
264     }
265 }