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 org.opendaylight.mdsal.dom.spi.store.DOMStore;
14 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
15 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.mdsal.common.api.AsyncDataChangeEvent;
19 import org.opendaylight.mdsal.common.api.AsyncDataChangeListener;
20 import org.opendaylight.mdsal.common.api.AsyncDataBroker.DataChangeScope;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import com.google.common.base.Preconditions;
28 import com.google.common.util.concurrent.SettableFuture;
30 public class DatastoreTestTask {
32 private final DOMStore store;
33 private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener;
35 private WriteTransactionCustomizer setup;
36 private WriteTransactionCustomizer write;
37 private ReadTransactionVerifier read;
38 private WriteTransactionCustomizer cleanup;
39 private YangInstanceIdentifier changePath;
40 private DataChangeScope changeScope;
41 private volatile boolean postSetup = false;
42 private final ChangeEventListener internalListener;
43 private final TestDCLExecutorService dclExecutorService;
45 public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
46 this.store = datastore;
47 this.dclExecutorService = dclExecutorService;
48 internalListener = new ChangeEventListener();
51 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope,
52 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener) {
53 this.changeListener = changeListener;
54 this.changePath = path;
55 this.changeScope = scope;
59 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
60 this.changePath = path;
61 this.changeScope = scope;
65 public DatastoreTestTask setup(final WriteTransactionCustomizer setup) {
70 public DatastoreTestTask test(final WriteTransactionCustomizer write) {
75 public DatastoreTestTask read(final ReadTransactionVerifier read) {
80 public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) {
81 this.cleanup = cleanup;
85 public void run() throws InterruptedException, ExecutionException, TimeoutException {
89 ListenerRegistration<ChangeEventListener> registration = null;
90 if (changePath != null) {
91 registration = store.registerChangeListener(changePath, internalListener, changeScope);
94 Preconditions.checkState(write != null, "Write Transaction must be set.");
97 dclExecutorService.afterTestSetup();
100 if (registration != null) {
101 registration.close();
104 if (changeListener != null) {
105 changeListener.onDataChanged(getChangeEvent());
108 read.verify(store.newReadOnlyTransaction());
110 if (cleanup != null) {
115 public AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> getChangeEvent() {
117 return internalListener.receivedChange.get(10, TimeUnit.SECONDS);
118 } catch( Exception e ) {
119 fail( "Error getting the AsyncDataChangeEvent from the Future: " + e );
126 public void verifyNoChangeEvent() {
128 Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS);
129 fail( "Got unexpected AsyncDataChangeEvent from the Future: " + unexpected );
130 } catch( TimeoutException e ) {
132 } catch( Exception e ) {
133 fail( "Error getting the AsyncDataChangeEvent from the Future: " + e );
137 private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
139 DOMStoreReadWriteTransaction tx = store.newReadWriteTransaction();
140 writeCustomizer.customize(tx);
141 DOMStoreThreePhaseCommitCohort cohort = tx.ready();
142 assertTrue(cohort.canCommit().get());
143 cohort.preCommit().get();
144 cohort.commit().get();
147 public interface WriteTransactionCustomizer {
148 public void customize(DOMStoreReadWriteTransaction tx);
151 public interface ReadTransactionVerifier {
152 public void verify(DOMStoreReadTransaction tx);
155 private final class ChangeEventListener implements
156 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
158 protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> receivedChange = SettableFuture
162 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
164 receivedChange.set(change);
169 public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
170 final NormalizedNode<?, ?> data) {
171 return new WriteTransactionCustomizer() {
174 public void customize(final DOMStoreReadWriteTransaction tx) {
175 tx.write(path, data);
180 public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
181 final NormalizedNode<?, ?> data) {
182 return new WriteTransactionCustomizer() {
185 public void customize(final DOMStoreReadWriteTransaction tx) {
186 tx.merge(path, data);
191 public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
192 return new WriteTransactionCustomizer() {
194 public void customize(final DOMStoreReadWriteTransaction tx) {