Bug-1903:On recovery all replicated log entries should not be applied to state
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / MockAkkaJournal.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 package org.opendaylight.controller.cluster.raft.utils;
9
10 import akka.dispatch.Futures;
11 import akka.japi.Procedure;
12 import akka.persistence.PersistentConfirmation;
13 import akka.persistence.PersistentId;
14 import akka.persistence.PersistentImpl;
15 import akka.persistence.PersistentRepr;
16 import akka.persistence.journal.japi.AsyncWriteJournal;
17 import com.google.common.collect.Maps;
18 import scala.concurrent.Future;
19
20 import java.util.Map;
21 import java.util.concurrent.Callable;
22
23 public class MockAkkaJournal extends AsyncWriteJournal {
24
25     private static Map<Long, Object> journal = Maps.newHashMap();
26
27     public static void addToJournal(long sequenceNr, Object message) {
28         journal.put(sequenceNr, message);
29     }
30
31     public static void clearJournal() {
32         journal.clear();
33     }
34
35     @Override
36     public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr,
37         long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
38
39         return Futures.future(new Callable<Void>() {
40             @Override
41             public Void call() throws Exception {
42                 for (Map.Entry<Long,Object> entry : journal.entrySet()) {
43                     PersistentRepr persistentMessage =
44                         new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId, false, null, null);
45                     replayCallback.apply(persistentMessage);
46                 }
47                 return null;
48             }
49         }, context().dispatcher());
50     }
51
52     @Override
53     public Future<Long> doAsyncReadHighestSequenceNr(String s, long l) {
54         return Futures.successful(new Long(0));
55     }
56
57     @Override
58     public Future<Void> doAsyncWriteMessages(Iterable<PersistentRepr> persistentReprs) {
59         return Futures.successful(null);
60     }
61
62     @Override
63     public Future<Void> doAsyncWriteConfirmations(Iterable<PersistentConfirmation> persistentConfirmations) {
64         return Futures.successful(null);
65     }
66
67     @Override
68     public Future<Void> doAsyncDeleteMessages(Iterable<PersistentId> persistentIds, boolean b) {
69         return Futures.successful(null);
70     }
71
72     @Override
73     public Future<Void> doAsyncDeleteMessagesTo(String s, long l, boolean b) {
74         return Futures.successful(null);
75     }
76 }