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