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