Implement handling of AppendEntries from a recipient perspective
[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
16 import java.util.ArrayList;
17 import java.util.List;
18
19 public class MockRaftActorContext implements RaftActorContext {
20
21     private String id;
22     private ActorSystem system;
23     private ActorRef actor;
24     private long index = 0;
25     private long lastApplied = 0;
26     private final ElectionTerm electionTerm;
27     private ReplicatedLog replicatedLog;
28
29     public MockRaftActorContext(){
30         electionTerm = null;
31
32         initReplicatedLog();
33     }
34
35     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
36         this.id = id;
37         this.system = system;
38         this.actor = actor;
39
40         electionTerm = new ElectionTermImpl(id);
41
42         initReplicatedLog();
43     }
44
45
46     public void initReplicatedLog(){
47         MockReplicatedLog mockReplicatedLog = new MockReplicatedLog();
48         this.replicatedLog = mockReplicatedLog;
49         mockReplicatedLog.setLast(new MockReplicatedLogEntry(1,1,""));
50         mockReplicatedLog.setReplicatedLogEntry(new MockReplicatedLogEntry(1,1, ""));
51     }
52
53     @Override public ActorRef actorOf(Props props) {
54         return system.actorOf(props);
55     }
56
57     @Override public ActorSelection actorSelection(String path) {
58         return system.actorSelection(path);
59     }
60
61     @Override public String getId() {
62         return id;
63     }
64
65     @Override public ActorRef getActor() {
66         return actor;
67     }
68
69     @Override public ElectionTerm getTermInformation() {
70         return electionTerm;
71     }
72
73     public void setIndex(long index){
74         this.index = index;
75     }
76
77     @Override public long getCommitIndex() {
78         return index;
79     }
80
81     @Override public void setCommitIndex(long commitIndex) {
82         this.index = commitIndex;
83     }
84
85     @Override public void setLastApplied(long lastApplied){
86         this.lastApplied = lastApplied;
87     }
88
89     @Override public long getLastApplied() {
90         return lastApplied;
91     }
92
93     public void setReplicatedLog(ReplicatedLog replicatedLog) {
94         this.replicatedLog = replicatedLog;
95     }
96
97     @Override public ReplicatedLog getReplicatedLog() {
98         return replicatedLog;
99     }
100
101     @Override public ActorSystem getActorSystem() {
102         return this.system;
103     }
104
105
106     public static class MockReplicatedLog implements ReplicatedLog {
107         private ReplicatedLogEntry replicatedLogEntry = new MockReplicatedLogEntry(0,0, "");
108         private ReplicatedLogEntry last = new MockReplicatedLogEntry(0,0, "");
109
110         @Override public ReplicatedLogEntry get(long index) {
111             return replicatedLogEntry;
112         }
113
114         @Override public ReplicatedLogEntry last() {
115             return last;
116         }
117
118         @Override public void removeFrom(long index) {
119         }
120
121         @Override public void append(ReplicatedLogEntry replicatedLogEntry) {
122         }
123
124         public void setReplicatedLogEntry(
125             ReplicatedLogEntry replicatedLogEntry) {
126             this.replicatedLogEntry = replicatedLogEntry;
127         }
128
129         public void setLast(ReplicatedLogEntry last) {
130             this.last = last;
131         }
132     }
133
134     public static class SimpleReplicatedLog implements ReplicatedLog {
135         private final List<ReplicatedLogEntry> log = new ArrayList<>(10000);
136
137         @Override public ReplicatedLogEntry get(long index) {
138             return log.get((int) index);
139         }
140
141         @Override public ReplicatedLogEntry last() {
142             return log.get(log.size()-1);
143         }
144
145         @Override public void removeFrom(long index) {
146             for(int i=(int) index ; i < log.size() ; i++) {
147                 log.remove(i);
148             }
149         }
150
151         @Override public void append(ReplicatedLogEntry replicatedLogEntry) {
152             log.add(replicatedLogEntry);
153         }
154     }
155
156     public static class MockReplicatedLogEntry implements ReplicatedLogEntry {
157
158         private final long term;
159         private final long index;
160         private final Object data;
161
162         public MockReplicatedLogEntry(long term, long index, Object data){
163
164             this.term = term;
165             this.index = index;
166             this.data = data;
167         }
168
169         @Override public Object getData() {
170             return data;
171         }
172
173         @Override public long getTerm() {
174             return term;
175         }
176
177         @Override public long getIndex() {
178             return index;
179         }
180     }
181 }