ed17aa55cfb852fa5724272f87f8d298f1f694cb
[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 import static org.junit.Assert.fail;
12
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;
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 volatile boolean postSetup = false;
41     private final ChangeEventListener internalListener;
42     private final TestDCLExecutorService dclExecutorService;
43
44     public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
45         this.store = datastore;
46         this.dclExecutorService = dclExecutorService;
47         internalListener = new ChangeEventListener();
48     }
49
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;
55         return this;
56     }
57
58     public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
59         this.changePath = path;
60         this.changeScope = scope;
61         return this;
62     }
63
64     public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) {
65         this.setup = customizer;
66         return this;
67     }
68
69     public DatastoreTestTask test(final WriteTransactionCustomizer customizer) {
70         this.write = customizer;
71         return this;
72     }
73
74     public DatastoreTestTask read(final ReadTransactionVerifier customizer) {
75         this.read = customizer;
76         return this;
77     }
78
79     public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) {
80         this.cleanup = customizer;
81         return this;
82     }
83
84     public void run() throws Exception {
85         if (setup != null) {
86             execute(setup);
87         }
88         ListenerRegistration<ChangeEventListener> registration = null;
89         if (changePath != null) {
90             registration = store.registerChangeListener(changePath, internalListener, changeScope);
91         }
92
93         Preconditions.checkState(write != null, "Write Transaction must be set.");
94
95         postSetup = true;
96         dclExecutorService.afterTestSetup();
97
98         execute(write);
99         if (registration != null) {
100             registration.close();
101         }
102
103         if (changeListener != null) {
104             changeListener.onDataChanged(getChangeEvent());
105         }
106         if (read != null) {
107             read.verify(store.newReadOnlyTransaction());
108         }
109         if (cleanup != null) {
110             execute(cleanup);
111         }
112     }
113
114     public AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> getChangeEvent() throws Exception {
115         return internalListener.receivedChange.get(10, TimeUnit.SECONDS);
116     }
117
118     public void verifyNoChangeEvent() throws Exception {
119         try {
120             Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS);
121             fail("Got unexpected AsyncDataChangeEvent from the Future: " + unexpected);
122         } catch (TimeoutException e) {
123             // Expected
124         }
125     }
126
127     private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
128             ExecutionException {
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();
135     }
136
137     public interface WriteTransactionCustomizer {
138         void customize(DOMStoreReadWriteTransaction tx);
139     }
140
141     public interface ReadTransactionVerifier {
142         void verify(DOMStoreReadTransaction tx);
143     }
144
145     private final class ChangeEventListener implements
146             AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
147
148         protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>>
149                 receivedChange = SettableFuture.create();
150
151         @Override
152         public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
153             if (postSetup) {
154                 receivedChange.set(change);
155             }
156         }
157     }
158
159     public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
160             final NormalizedNode<?, ?> data) {
161         return tx -> tx.write(path, data);
162     }
163
164     public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
165             final NormalizedNode<?, ?> data) {
166         return tx -> tx.merge(path, data);
167     }
168
169     public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
170         return tx -> tx.delete(path);
171     }
172 }