Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / DatastoreTestTask.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.md.sal.dom.store.impl;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.SettableFuture;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.function.Function;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
34
35 public class DatastoreTestTask {
36
37     private final DOMStore store;
38
39     private WriteTransactionCustomizer setup;
40     private WriteTransactionCustomizer write;
41     private ReadTransactionVerifier read;
42     private WriteTransactionCustomizer cleanup;
43     private YangInstanceIdentifier changePath;
44     private DOMStoreTreeChangePublisher storeTreeChangePublisher;
45     private ChangeEventListener internalListener;
46     private final TestDCLExecutorService dclExecutorService;
47
48     public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
49         this.store = datastore;
50         this.dclExecutorService = dclExecutorService;
51     }
52
53     @SafeVarargs
54     public final DatastoreTestTask changeListener(final YangInstanceIdentifier path,
55             Function<DataTreeCandidate, Boolean>... matchers) {
56         assertTrue(store instanceof DOMStoreTreeChangePublisher);
57         this.storeTreeChangePublisher = (DOMStoreTreeChangePublisher)store;
58         this.changePath = path;
59         this.internalListener = new ChangeEventListener(matchers);
60         return this;
61     }
62
63     public static Function<DataTreeCandidate, Boolean> added(YangInstanceIdentifier path) {
64         return candidate -> candidate.getRootNode().getModificationType() == ModificationType.WRITE
65                 && path.equals(candidate.getRootPath()) && !candidate.getRootNode().getDataBefore().isPresent()
66                 && candidate.getRootNode().getDataAfter().isPresent();
67     }
68
69     public static Function<DataTreeCandidate, Boolean> replaced(YangInstanceIdentifier path) {
70         return candidate -> candidate.getRootNode().getModificationType() == ModificationType.WRITE
71                 && path.equals(candidate.getRootPath()) && candidate.getRootNode().getDataBefore().isPresent()
72                 && candidate.getRootNode().getDataAfter().isPresent();
73     }
74
75     public static Function<DataTreeCandidate, Boolean> deleted(YangInstanceIdentifier path) {
76         return candidate -> candidate.getRootNode().getModificationType() == ModificationType.DELETE
77                 && path.equals(candidate.getRootPath()) && candidate.getRootNode().getDataBefore().isPresent()
78                 && !candidate.getRootNode().getDataAfter().isPresent();
79     }
80
81     public static Function<DataTreeCandidate, Boolean> subtreeModified(YangInstanceIdentifier path) {
82         return candidate -> candidate.getRootNode().getModificationType() == ModificationType.SUBTREE_MODIFIED
83                 && path.equals(candidate.getRootPath()) && candidate.getRootNode().getDataBefore().isPresent()
84                 && candidate.getRootNode().getDataAfter().isPresent();
85     }
86
87     public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) {
88         this.setup = customizer;
89         return this;
90     }
91
92     public DatastoreTestTask test(final WriteTransactionCustomizer customizer) {
93         this.write = customizer;
94         return this;
95     }
96
97     public DatastoreTestTask read(final ReadTransactionVerifier customizer) {
98         this.read = customizer;
99         return this;
100     }
101
102     public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) {
103         this.cleanup = customizer;
104         return this;
105     }
106
107     public void run() throws Exception {
108         if (setup != null) {
109             execute(setup);
110         }
111         ListenerRegistration<ChangeEventListener> registration = null;
112         if (changePath != null) {
113             registration = storeTreeChangePublisher.registerTreeChangeListener(changePath, internalListener);
114         }
115
116         Preconditions.checkState(write != null, "Write Transaction must be set.");
117
118         dclExecutorService.afterTestSetup();
119
120         execute(write);
121         if (registration != null) {
122             // DCL is asynchronous, we need to make sure all tasks are executed before we close the registration,
123             // otherwise they would get lost
124             dclExecutorService.shutdown();
125             dclExecutorService.awaitTermination(5, TimeUnit.SECONDS);
126             registration.close();
127         }
128
129         if (read != null) {
130             read.verify(store.newReadOnlyTransaction());
131         }
132         if (cleanup != null) {
133             execute(cleanup);
134         }
135     }
136
137     public void verifyChangeEvents() {
138         internalListener.verifyChangeEvents();
139     }
140
141     public void verifyNoChangeEvent() {
142         internalListener.verifyNoChangeEvent();
143     }
144
145     private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
146             ExecutionException {
147         DOMStoreReadWriteTransaction tx = store.newReadWriteTransaction();
148         writeCustomizer.customize(tx);
149         DOMStoreThreePhaseCommitCohort cohort = tx.ready();
150         assertTrue(cohort.canCommit().get());
151         cohort.preCommit().get();
152         cohort.commit().get();
153     }
154
155     public interface WriteTransactionCustomizer {
156         void customize(DOMStoreReadWriteTransaction tx);
157     }
158
159     public interface ReadTransactionVerifier {
160         void verify(DOMStoreReadTransaction tx);
161     }
162
163     private final class ChangeEventListener implements DOMDataTreeChangeListener {
164
165         final SettableFuture<Collection<DataTreeCandidate>> future = SettableFuture.create();
166         final Collection<DataTreeCandidate> accumulatedChanges = new ArrayList<>();
167         final Function<DataTreeCandidate, Boolean>[] matchers;
168         final int expChangeCount;
169
170         ChangeEventListener(Function<DataTreeCandidate, Boolean>[] matchers) {
171             this.expChangeCount = matchers.length;
172             this.matchers = matchers;
173         }
174
175         Collection<DataTreeCandidate> changes() {
176             try {
177                 Collection<DataTreeCandidate> changes = internalListener.future.get(10, TimeUnit.SECONDS);
178                 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
179                 return changes;
180             } catch (TimeoutException e) {
181                 throw new AssertionError(String.format(
182                         "Data tree change notifications not received. Expected: %s. Actual: %s - %s",
183                         expChangeCount, accumulatedChanges.size(), accumulatedChanges), e);
184             } catch (InterruptedException | ExecutionException e) {
185                 throw new AssertionError("Data tree change notifications failed", e);
186             }
187         }
188
189         void verifyChangeEvents() {
190             Collection<DataTreeCandidate> changes = new ArrayList<>(changes());
191             Iterator<DataTreeCandidate> iter = changes.iterator();
192             while (iter.hasNext()) {
193                 DataTreeCandidate dataTreeModification = iter.next();
194                 for (Function<DataTreeCandidate, Boolean> matcher: matchers) {
195                     if (matcher.apply(dataTreeModification)) {
196                         iter.remove();
197                         break;
198                     }
199                 }
200             }
201
202             if (!changes.isEmpty()) {
203                 DataTreeCandidate mod = changes.iterator().next();
204                 fail(String.format("Received unexpected notification: type: %s, path: %s, before: %s, after: %s",
205                         mod.getRootNode().getModificationType(), mod.getRootPath(),
206                         mod.getRootNode().getDataBefore(), mod.getRootNode().getDataAfter()));
207             }
208         }
209
210         void verifyNoChangeEvent() {
211             try {
212                 Object unexpected = internalListener.future.get(500, TimeUnit.MILLISECONDS);
213                 fail("Got unexpected Data tree change notifications: " + unexpected);
214             } catch (TimeoutException e) {
215                 // Expected
216             } catch (InterruptedException | ExecutionException e) {
217                 throw new AssertionError("Data tree change notifications failed", e);
218             }
219         }
220
221         @Override
222         public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
223             synchronized (accumulatedChanges) {
224                 accumulatedChanges.addAll(changes);
225                 if (expChangeCount == accumulatedChanges.size()) {
226                     future.set(new ArrayList<>(accumulatedChanges));
227                 }
228             }
229         }
230     }
231
232     public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
233             final NormalizedNode<?, ?> data) {
234         return tx -> tx.write(path, data);
235     }
236
237     public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
238             final NormalizedNode<?, ?> data) {
239         return tx -> tx.merge(path, data);
240     }
241
242     public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
243         return tx -> tx.delete(path);
244     }
245 }