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 java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
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;
28 import com.google.common.base.Preconditions;
29 import com.google.common.util.concurrent.SettableFuture;
31 public class DatastoreTestTask {
33 private final DOMStore store;
34 private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener;
36 private WriteTransactionCustomizer setup;
37 private WriteTransactionCustomizer write;
38 private ReadTransactionVerifier read;
39 private WriteTransactionCustomizer cleanup;
40 private YangInstanceIdentifier changePath;
41 private DataChangeScope changeScope;
42 private volatile boolean postSetup = false;
43 private final ChangeEventListener internalListener;
44 private final TestDCLExecutorService dclExecutorService;
46 public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
47 this.store = datastore;
48 this.dclExecutorService = dclExecutorService;
49 internalListener = new ChangeEventListener();
52 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope,
53 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener) {
54 this.changeListener = changeListener;
55 this.changePath = path;
56 this.changeScope = scope;
60 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
61 this.changePath = path;
62 this.changeScope = scope;
66 public DatastoreTestTask setup(final WriteTransactionCustomizer setup) {
71 public DatastoreTestTask test(final WriteTransactionCustomizer write) {
76 public DatastoreTestTask read(final ReadTransactionVerifier read) {
81 public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) {
82 this.cleanup = cleanup;
86 public void run() throws InterruptedException, ExecutionException, TimeoutException {
90 ListenerRegistration<ChangeEventListener> registration = null;
91 if (changePath != null) {
92 registration = store.registerChangeListener(changePath, internalListener, changeScope);
95 Preconditions.checkState(write != null, "Write Transaction must be set.");
98 dclExecutorService.afterTestSetup();
101 if (registration != null) {
102 registration.close();
105 if (changeListener != null) {
106 changeListener.onDataChanged(getChangeEvent());
109 read.verify(store.newReadOnlyTransaction());
111 if (cleanup != null) {
116 public AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> getChangeEvent() {
118 return internalListener.receivedChange.get(10, TimeUnit.SECONDS);
119 } catch( Exception e ) {
120 fail( "Error getting the AsyncDataChangeEvent from the Future: " + e );
127 public void verifyNoChangeEvent() {
129 Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS);
130 fail( "Got unexpected AsyncDataChangeEvent from the Future: " + unexpected );
131 } catch( TimeoutException e ) {
133 } catch( Exception e ) {
134 fail( "Error getting the AsyncDataChangeEvent from the Future: " + e );
138 private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
140 DOMStoreReadWriteTransaction tx = store.newReadWriteTransaction();
141 writeCustomizer.customize(tx);
142 DOMStoreThreePhaseCommitCohort cohort = tx.ready();
143 assertTrue(cohort.canCommit().get());
144 cohort.preCommit().get();
145 cohort.commit().get();
148 public interface WriteTransactionCustomizer {
149 public void customize(DOMStoreReadWriteTransaction tx);
152 public interface ReadTransactionVerifier {
153 public void verify(DOMStoreReadTransaction tx);
156 private final class ChangeEventListener implements
157 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
159 protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> receivedChange = SettableFuture
163 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
165 receivedChange.set(change);
170 public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
171 final NormalizedNode<?, ?> data) {
172 return new WriteTransactionCustomizer() {
175 public void customize(final DOMStoreReadWriteTransaction tx) {
176 tx.write(path, data);
181 public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
182 final NormalizedNode<?, ?> data) {
183 return new WriteTransactionCustomizer() {
186 public void customize(final DOMStoreReadWriteTransaction tx) {
187 tx.merge(path, data);
192 public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
193 return new WriteTransactionCustomizer() {
195 public void customize(final DOMStoreReadWriteTransaction tx) {