Bug 2845: Temporarily ignore RestPutOperationTest
[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     private static final Map<String, CountDownLatch> blockReadMessagesLatches = new ConcurrentHashMap<>();
36
37     public static void addEntry(String persistenceId, long sequenceNr, Object data) {
38         Map<Long, Object> journal = journals.get(persistenceId);
39         if(journal == null) {
40             journal = Maps.newLinkedHashMap();
41             journals.put(persistenceId, journal);
42         }
43
44         synchronized (journal) {
45             journal.put(sequenceNr, data);
46         }
47     }
48
49     public static void clear() {
50         journals.clear();
51     }
52
53     public static Map<Long, Object> get(String persistenceId) {
54         Map<Long, Object> journal = journals.get(persistenceId);
55         return journal != null ? journal : Collections.<Long, Object>emptyMap();
56     }
57
58     public static void waitForDeleteMessagesComplete(String persistenceId) {
59         assertEquals("Recovery complete", true, Uninterruptibles.awaitUninterruptibly(
60                 deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS));
61     }
62
63     public static void addDeleteMessagesCompleteLatch(String persistenceId) {
64         deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
65     }
66
67     public static void addBlockReadMessagesLatch(String persistenceId, CountDownLatch latch) {
68         blockReadMessagesLatches.put(persistenceId, latch);
69     }
70
71     @Override
72     public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr,
73             long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
74         return Futures.future(new Callable<Void>() {
75             @Override
76             public Void call() throws Exception {
77                 CountDownLatch blockLatch = blockReadMessagesLatches.remove(persistenceId);
78                 if(blockLatch != null) {
79                     Uninterruptibles.awaitUninterruptibly(blockLatch);
80                 }
81
82                 Map<Long, Object> journal = journals.get(persistenceId);
83                 if(journal == null) {
84                     return null;
85                 }
86
87                 synchronized (journal) {
88                     for (Map.Entry<Long,Object> entry : journal.entrySet()) {
89                         PersistentRepr persistentMessage =
90                                 new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId,
91                                         false, null, null);
92                         replayCallback.apply(persistentMessage);
93                     }
94                 }
95
96                 return null;
97             }
98         }, context().dispatcher());
99     }
100
101     @Override
102     public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
103         return Futures.successful(-1L);
104     }
105
106     @Override
107     public Future<Void> doAsyncWriteMessages(final Iterable<PersistentRepr> messages) {
108         return Futures.future(new Callable<Void>() {
109             @Override
110             public Void call() throws Exception {
111                 for (PersistentRepr repr : messages) {
112                     Map<Long, Object> journal = journals.get(repr.persistenceId());
113                     if(journal == null) {
114                         journal = Maps.newLinkedHashMap();
115                         journals.put(repr.persistenceId(), journal);
116                     }
117
118                     synchronized (journal) {
119                         journal.put(repr.sequenceNr(), repr.payload());
120                     }
121                 }
122                 return null;
123             }
124         }, context().dispatcher());
125     }
126
127     @Override
128     public Future<Void> doAsyncWriteConfirmations(Iterable<PersistentConfirmation> confirmations) {
129         return Futures.successful(null);
130     }
131
132     @Override
133     public Future<Void> doAsyncDeleteMessages(Iterable<PersistentId> messageIds, boolean permanent) {
134         return Futures.successful(null);
135     }
136
137     @Override
138     public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr, boolean permanent) {
139         Map<Long, Object> journal = journals.get(persistenceId);
140         if(journal != null) {
141             synchronized (journal) {
142                 Iterator<Long> iter = journal.keySet().iterator();
143                 while(iter.hasNext()) {
144                     Long n = iter.next();
145                     if(n <= toSequenceNr) {
146                         iter.remove();
147                     }
148                 }
149             }
150         }
151
152         CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
153         if(latch != null) {
154             latch.countDown();
155         }
156
157         return Futures.successful(null);
158     }
159 }