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 java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 public class DatastoreTestTask {
31 private final DOMStore store;
32 private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener;
34 private WriteTransactionCustomizer setup;
35 private WriteTransactionCustomizer write;
36 private ReadTransactionVerifier read;
37 private WriteTransactionCustomizer cleanup;
38 private YangInstanceIdentifier changePath;
39 private DataChangeScope changeScope;
40 private volatile boolean postSetup = false;
41 private final ChangeEventListener internalListener;
42 private final TestDCLExecutorService dclExecutorService;
44 public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
45 this.store = datastore;
46 this.dclExecutorService = dclExecutorService;
47 internalListener = new ChangeEventListener();
50 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope,
51 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
52 this.changeListener = listener;
53 this.changePath = path;
54 this.changeScope = scope;
58 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
59 this.changePath = path;
60 this.changeScope = scope;
64 public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) {
65 this.setup = customizer;
69 public DatastoreTestTask test(final WriteTransactionCustomizer customizer) {
70 this.write = customizer;
74 public DatastoreTestTask read(final ReadTransactionVerifier customizer) {
75 this.read = customizer;
79 public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) {
80 this.cleanup = customizer;
84 public void run() throws Exception {
88 ListenerRegistration<ChangeEventListener> registration = null;
89 if (changePath != null) {
90 registration = store.registerChangeListener(changePath, internalListener, changeScope);
93 Preconditions.checkState(write != null, "Write Transaction must be set.");
96 dclExecutorService.afterTestSetup();
99 if (registration != null) {
100 registration.close();
103 if (changeListener != null) {
104 changeListener.onDataChanged(getChangeEvent());
107 read.verify(store.newReadOnlyTransaction());
109 if (cleanup != null) {
114 public AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> getChangeEvent() throws Exception {
115 return internalListener.receivedChange.get(10, TimeUnit.SECONDS);
118 public void verifyNoChangeEvent() throws Exception {
120 Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS);
121 fail("Got unexpected AsyncDataChangeEvent from the Future: " + unexpected);
122 } catch (TimeoutException e) {
127 private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
129 DOMStoreReadWriteTransaction tx = store.newReadWriteTransaction();
130 writeCustomizer.customize(tx);
131 DOMStoreThreePhaseCommitCohort cohort = tx.ready();
132 assertTrue(cohort.canCommit().get());
133 cohort.preCommit().get();
134 cohort.commit().get();
137 public interface WriteTransactionCustomizer {
138 void customize(DOMStoreReadWriteTransaction tx);
141 public interface ReadTransactionVerifier {
142 void verify(DOMStoreReadTransaction tx);
145 private final class ChangeEventListener implements
146 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
148 protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>>
149 receivedChange = SettableFuture.create();
152 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
154 receivedChange.set(change);
159 public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
160 final NormalizedNode<?, ?> data) {
161 return tx -> tx.write(path, data);
164 public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
165 final NormalizedNode<?, ?> data) {
166 return tx -> tx.merge(path, data);
169 public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
170 return tx -> tx.delete(path);