Merge "BUG 1839 - HTTP delete of non existing data"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / InMemoryJournal.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.datastore.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.Map;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.TimeUnit;
18 import com.google.common.collect.Maps;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import scala.concurrent.Future;
21 import akka.dispatch.Futures;
22 import akka.japi.Procedure;
23 import akka.persistence.PersistentConfirmation;
24 import akka.persistence.PersistentId;
25 import akka.persistence.PersistentImpl;
26 import akka.persistence.PersistentRepr;
27 import akka.persistence.journal.japi.AsyncWriteJournal;
28
29 public class InMemoryJournal extends AsyncWriteJournal {
30
31     private static final Map<String, Map<Long, Object>> journals = new ConcurrentHashMap<>();
32
33     private static final Map<String, CountDownLatch> deleteMessagesCompleteLatches = new ConcurrentHashMap<>();
34
35     public static void addEntry(String persistenceId, long sequenceNr, Object data) {
36         Map<Long, Object> journal = journals.get(persistenceId);
37         if(journal == null) {
38             journal = Maps.newLinkedHashMap();
39             journals.put(persistenceId, journal);
40         }
41
42         synchronized (journal) {
43             journal.put(sequenceNr, data);
44         }
45     }
46
47     public static void clear() {
48         journals.clear();
49     }
50
51     public static Map<Long, Object> get(String persistenceId) {
52         Map<Long, Object> journal = journals.get(persistenceId);
53         return journal != null ? journal : Collections.<Long, Object>emptyMap();
54     }
55
56     public static void waitForDeleteMessagesComplete(String persistenceId) {
57         assertEquals("Recovery complete", true, Uninterruptibles.awaitUninterruptibly(
58                 deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS));
59     }
60
61     public static void addDeleteMessagesCompleteLatch(String persistenceId) {
62         deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
63     }
64
65     @Override
66     public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr,
67             long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
68         return Futures.future(new Callable<Void>() {
69             @Override
70             public Void call() throws Exception {
71                 Map<Long, Object> journal = journals.get(persistenceId);
72                 if(journal == null) {
73                     return null;
74                 }
75
76                 synchronized (journal) {
77                     for (Map.Entry<Long,Object> entry : journal.entrySet()) {
78                         PersistentRepr persistentMessage =
79                                 new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId,
80                                         false, null, null);
81                         replayCallback.apply(persistentMessage);
82                     }
83                 }
84
85                 return null;
86             }
87         }, context().dispatcher());
88     }
89
90     @Override
91     public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
92         return Futures.successful(-1L);
93     }
94
95     @Override
96     public Future<Void> doAsyncWriteMessages(final Iterable<PersistentRepr> messages) {
97         return Futures.future(new Callable<Void>() {
98             @Override
99             public Void call() throws Exception {
100                 for (PersistentRepr repr : messages) {
101                     Map<Long, Object> journal = journals.get(repr.persistenceId());
102                     if(journal == null) {
103                         journal = Maps.newLinkedHashMap();
104                         journals.put(repr.persistenceId(), journal);
105                     }
106
107                     synchronized (journal) {
108                         journal.put(repr.sequenceNr(), repr.payload());
109                     }
110                 }
111                 return null;
112             }
113         }, context().dispatcher());
114     }
115
116     @Override
117     public Future<Void> doAsyncWriteConfirmations(Iterable<PersistentConfirmation> confirmations) {
118         return Futures.successful(null);
119     }
120
121     @Override
122     public Future<Void> doAsyncDeleteMessages(Iterable<PersistentId> messageIds, boolean permanent) {
123         return Futures.successful(null);
124     }
125
126     @Override
127     public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr, boolean permanent) {
128         Map<Long, Object> journal = journals.get(persistenceId);
129         if(journal != null) {
130             synchronized (journal) {
131                 Iterator<Long> iter = journal.keySet().iterator();
132                 while(iter.hasNext()) {
133                     Long n = iter.next();
134                     if(n <= toSequenceNr) {
135                         iter.remove();
136                     }
137                 }
138             }
139         }
140
141         CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
142         if(latch != null) {
143             latch.countDown();
144         }
145
146         return Futures.successful(null);
147     }
148 }