2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
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;
35 public class DatastoreTestTask {
37 private final DOMStore store;
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;
48 public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
49 this.store = datastore;
50 this.dclExecutorService = dclExecutorService;
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);
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();
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();
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();
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();
87 public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) {
88 this.setup = customizer;
92 public DatastoreTestTask test(final WriteTransactionCustomizer customizer) {
93 this.write = customizer;
97 public DatastoreTestTask read(final ReadTransactionVerifier customizer) {
98 this.read = customizer;
102 public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) {
103 this.cleanup = customizer;
107 public void run() throws Exception {
111 ListenerRegistration<ChangeEventListener> registration = null;
112 if (changePath != null) {
113 registration = storeTreeChangePublisher.registerTreeChangeListener(changePath, internalListener);
116 Preconditions.checkState(write != null, "Write Transaction must be set.");
118 dclExecutorService.afterTestSetup();
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();
130 read.verify(store.newReadOnlyTransaction());
132 if (cleanup != null) {
137 public void verifyChangeEvents() {
138 internalListener.verifyChangeEvents();
141 public void verifyNoChangeEvent() {
142 internalListener.verifyNoChangeEvent();
145 private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
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();
155 public interface WriteTransactionCustomizer {
156 void customize(DOMStoreReadWriteTransaction tx);
159 public interface ReadTransactionVerifier {
160 void verify(DOMStoreReadTransaction tx);
163 private final class ChangeEventListener implements DOMDataTreeChangeListener {
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;
170 ChangeEventListener(Function<DataTreeCandidate, Boolean>[] matchers) {
171 this.expChangeCount = matchers.length;
172 this.matchers = matchers;
175 Collection<DataTreeCandidate> changes() {
177 Collection<DataTreeCandidate> changes = internalListener.future.get(10, TimeUnit.SECONDS);
178 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
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);
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)) {
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()));
210 void verifyNoChangeEvent() {
212 Object unexpected = internalListener.future.get(500, TimeUnit.MILLISECONDS);
213 fail("Got unexpected Data tree change notifications: " + unexpected);
214 } catch (TimeoutException e) {
216 } catch (InterruptedException | ExecutionException e) {
217 throw new AssertionError("Data tree change notifications failed", e);
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));
232 public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
233 final NormalizedNode<?, ?> data) {
234 return tx -> tx.write(path, data);
237 public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
238 final NormalizedNode<?, ?> data) {
239 return tx -> tx.merge(path, data);
242 public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
243 return tx -> tx.delete(path);