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;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import com.google.common.base.Preconditions;
27 import com.google.common.util.concurrent.SettableFuture;
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 boolean postSetup = false;
41 private final ChangeEventListener internalListener;
43 public DatastoreTestTask(final DOMStore datastore) {
44 this.store = datastore;
45 internalListener = new ChangeEventListener();
48 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope,
49 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener) {
50 this.changeListener = changeListener;
51 this.changePath = path;
52 this.changeScope = scope;
56 public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
57 this.changePath = path;
58 this.changeScope = scope;
62 public DatastoreTestTask setup(final WriteTransactionCustomizer setup) {
67 public DatastoreTestTask test(final WriteTransactionCustomizer write) {
72 public DatastoreTestTask read(final ReadTransactionVerifier read) {
77 public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) {
78 this.cleanup = cleanup;
82 public void run() throws InterruptedException, ExecutionException {
86 ListenerRegistration<ChangeEventListener> registration = null;
87 if (changePath != null) {
88 registration = store.registerChangeListener(changePath, internalListener, changeScope);
91 Preconditions.checkState(write != null, "Write Transaction must be set.");
94 if (registration != null) {
97 if (changeListener != null) {
98 changeListener.onDataChanged(internalListener.receivedChange.get());
101 read.verify(store.newReadOnlyTransaction());
103 if (cleanup != null) {
108 public Future<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> getChangeEvent() {
109 return internalListener.receivedChange;
112 private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
114 DOMStoreReadWriteTransaction tx = store.newReadWriteTransaction();
115 writeCustomizer.customize(tx);
116 DOMStoreThreePhaseCommitCohort cohort = tx.ready();
117 assertTrue(cohort.canCommit().get());
118 cohort.preCommit().get();
119 cohort.commit().get();
122 public interface WriteTransactionCustomizer {
123 public void customize(DOMStoreReadWriteTransaction tx);
126 public interface ReadTransactionVerifier {
127 public void verify(DOMStoreReadTransaction tx);
130 private final class ChangeEventListener implements
131 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
133 protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> receivedChange = SettableFuture
137 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
139 receivedChange.set(change);
144 public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
145 final NormalizedNode<?, ?> data) {
146 return new WriteTransactionCustomizer() {
149 public void customize(final DOMStoreReadWriteTransaction tx) {
150 tx.write(path, data);
155 public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
156 final NormalizedNode<?, ?> data) {
157 return new WriteTransactionCustomizer() {
160 public void customize(final DOMStoreReadWriteTransaction tx) {
161 tx.merge(path, data);
166 public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
167 return new WriteTransactionCustomizer() {
169 public void customize(final DOMStoreReadWriteTransaction tx) {