Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / DatastoreTestTask.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14
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;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.util.concurrent.SettableFuture;
28
29 public class DatastoreTestTask {
30
31     private final DOMStore store;
32     private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener;
33
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;
42
43     public DatastoreTestTask(final DOMStore datastore) {
44         this.store = datastore;
45         internalListener = new ChangeEventListener();
46     }
47
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;
53         return this;
54     }
55
56     public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
57         this.changePath = path;
58         this.changeScope = scope;
59         return this;
60     }
61
62     public DatastoreTestTask setup(final WriteTransactionCustomizer setup) {
63         this.setup = setup;
64         return this;
65     }
66
67     public DatastoreTestTask test(final WriteTransactionCustomizer write) {
68         this.write = write;
69         return this;
70     }
71
72     public DatastoreTestTask read(final ReadTransactionVerifier read) {
73         this.read = read;
74         return this;
75     }
76
77     public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) {
78         this.cleanup = cleanup;
79         return this;
80     }
81
82     public void run() throws InterruptedException, ExecutionException {
83         if (setup != null) {
84             execute(setup);
85         }
86         ListenerRegistration<ChangeEventListener> registration = null;
87         if (changePath != null) {
88             registration = store.registerChangeListener(changePath, internalListener, changeScope);
89         }
90
91         Preconditions.checkState(write != null, "Write Transaction must be set.");
92         postSetup = true;
93         execute(write);
94         if (registration != null) {
95             registration.close();
96         }
97         if (changeListener != null) {
98             changeListener.onDataChanged(internalListener.receivedChange.get());
99         }
100         if (read != null) {
101             read.verify(store.newReadOnlyTransaction());
102         }
103         if (cleanup != null) {
104             execute(cleanup);
105         }
106     }
107
108     public Future<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> getChangeEvent() {
109         return internalListener.receivedChange;
110     }
111
112     private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
113             ExecutionException {
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();
120     }
121
122     public interface WriteTransactionCustomizer {
123         public void customize(DOMStoreReadWriteTransaction tx);
124     }
125
126     public interface ReadTransactionVerifier {
127         public void verify(DOMStoreReadTransaction tx);
128     }
129
130     private final class ChangeEventListener implements
131             AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
132
133         protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> receivedChange = SettableFuture
134                 .create();
135
136         @Override
137         public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
138             if (postSetup) {
139                 receivedChange.set(change);
140             }
141         }
142     }
143
144     public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
145             final NormalizedNode<?, ?> data) {
146         return new WriteTransactionCustomizer() {
147
148             @Override
149             public void customize(final DOMStoreReadWriteTransaction tx) {
150                 tx.write(path, data);
151             }
152         };
153     }
154
155     public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
156             final NormalizedNode<?, ?> data) {
157         return new WriteTransactionCustomizer() {
158
159             @Override
160             public void customize(final DOMStoreReadWriteTransaction tx) {
161                 tx.merge(path, data);
162             }
163         };
164     }
165
166     public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
167         return new WriteTransactionCustomizer() {
168             @Override
169             public void customize(final DOMStoreReadWriteTransaction tx) {
170                 tx.delete(path);
171             }
172         };
173     }
174 }