Make DOMSchemaService operate of EffectiveModelContext
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / DOMDataTreeListenerTest.java
1 /*
2  * Copyright (c) 2015 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.mdsal.dom.broker;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
16
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.ForwardingExecutorService;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.TimeUnit;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.mdsal.common.api.TransactionCommitDeadlockException;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
38 import org.opendaylight.mdsal.dom.broker.util.TestModel;
39 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
40 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
43 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
53 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55
56 public class DOMDataTreeListenerTest extends AbstractDatastoreTest {
57
58     private AbstractDOMDataBroker domBroker;
59     private ListeningExecutorService executor;
60     private ExecutorService futureExecutor;
61     private CommitExecutorService commitExecutor;
62
63     private static final DataContainerChild<?, ?> OUTER_LIST =
64             ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
65             .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1))
66             .build();
67
68     private static final DataContainerChild<?, ?> OUTER_LIST_2 =
69             ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
70             .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2))
71             .build();
72
73     private static final NormalizedNode<?, ?> TEST_CONTAINER = Builders.containerBuilder()
74             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
75             .withChild(OUTER_LIST)
76             .build();
77
78     private static final NormalizedNode<?, ?> TEST_CONTAINER_2 = Builders.containerBuilder()
79             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
80             .withChild(OUTER_LIST_2)
81             .build();
82
83     private static final DOMDataTreeIdentifier ROOT_DATA_TREE_ID = new DOMDataTreeIdentifier(
84             LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
85
86     private static final DOMDataTreeIdentifier OUTER_LIST_DATA_TREE_ID = new DOMDataTreeIdentifier(
87             LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH);
88
89     @Before
90     public void setupStore() {
91         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
92                 MoreExecutors.newDirectExecutorService());
93         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
94                 MoreExecutors.newDirectExecutorService());
95
96         operStore.onModelContextUpdated(SCHEMA_CONTEXT);
97         configStore.onModelContextUpdated(SCHEMA_CONTEXT);
98
99         final ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType,
100                 DOMStore>builder()
101                 .put(CONFIGURATION, configStore)
102                 .put(OPERATIONAL, operStore)
103                 .build();
104
105         commitExecutor = new CommitExecutorService(Executors.newSingleThreadExecutor());
106         futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB",
107                 DOMDataTreeListenerTest.class);
108         executor = new DeadlockDetectingListeningExecutorService(commitExecutor,
109                 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER, futureExecutor);
110         domBroker = new SerializedDOMDataBroker(stores, executor);
111     }
112
113     @After
114     public void tearDown() {
115         if (executor != null) {
116             executor.shutdownNow();
117         }
118
119         if (futureExecutor != null) {
120             futureExecutor.shutdownNow();
121         }
122     }
123
124     @Test
125     public void writeContainerEmptyTreeTest() throws InterruptedException {
126         final CountDownLatch latch = new CountDownLatch(1);
127
128         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
129         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
130                 dataTreeChangeService);
131
132         final TestDataTreeListener listener = new TestDataTreeListener(latch);
133         final ListenerRegistration<TestDataTreeListener> listenerReg =
134                 dataTreeChangeService.registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
135
136         final DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
137         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
138         writeTx.commit();
139
140         latch.await(5, TimeUnit.SECONDS);
141
142         assertEquals(1, listener.getReceivedChanges().size());
143         final Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
144         assertEquals(1, changes.size());
145
146         final DataTreeCandidate candidate = changes.iterator().next();
147         assertNotNull(candidate);
148         final DataTreeCandidateNode candidateRoot = candidate.getRootNode();
149         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
150         listenerReg.close();
151     }
152
153     @Test
154     public void replaceContainerContainerInTreeTest() throws ExecutionException, InterruptedException {
155         final CountDownLatch latch = new CountDownLatch(2);
156
157         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
158         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
159                 dataTreeChangeService);
160
161         DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
162         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
163         writeTx.commit().get();
164
165         final TestDataTreeListener listener = new TestDataTreeListener(latch);
166         final ListenerRegistration<TestDataTreeListener> listenerReg =
167                 dataTreeChangeService.registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
168         writeTx = domBroker.newWriteOnlyTransaction();
169         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2);
170         writeTx.commit();
171
172         latch.await(5, TimeUnit.SECONDS);
173
174         assertEquals(2, listener.getReceivedChanges().size());
175         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
176         assertEquals(1, changes.size());
177
178         DataTreeCandidate candidate = changes.iterator().next();
179         assertNotNull(candidate);
180         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
181         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
182
183         changes = listener.getReceivedChanges().get(1);
184         assertEquals(1, changes.size());
185
186         candidate = changes.iterator().next();
187         assertNotNull(candidate);
188         candidateRoot = candidate.getRootNode();
189         checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.WRITE, candidateRoot);
190         listenerReg.close();
191     }
192
193     @Test
194     public void deleteContainerContainerInTreeTest() throws ExecutionException, InterruptedException {
195         final CountDownLatch latch = new CountDownLatch(2);
196
197         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
198         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
199                 dataTreeChangeService);
200
201         DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
202         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
203         writeTx.commit().get();
204
205         final TestDataTreeListener listener = new TestDataTreeListener(latch);
206         final ListenerRegistration<TestDataTreeListener> listenerReg =
207                 dataTreeChangeService.registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
208
209         writeTx = domBroker.newWriteOnlyTransaction();
210         writeTx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
211         writeTx.commit();
212
213         latch.await(5, TimeUnit.SECONDS);
214
215         assertEquals(2, listener.getReceivedChanges().size());
216         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
217         assertEquals(1, changes.size());
218
219         DataTreeCandidate candidate = changes.iterator().next();
220         assertNotNull(candidate);
221         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
222         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
223
224         changes = listener.getReceivedChanges().get(1);
225         assertEquals(1, changes.size());
226
227         candidate = changes.iterator().next();
228         assertNotNull(candidate);
229         candidateRoot = candidate.getRootNode();
230         checkChange(TEST_CONTAINER, null, ModificationType.DELETE, candidateRoot);
231         listenerReg.close();
232     }
233
234     @Test
235     public void replaceChildListContainerInTreeTest() throws ExecutionException, InterruptedException {
236         final CountDownLatch latch = new CountDownLatch(2);
237
238         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
239         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
240                 dataTreeChangeService);
241
242         DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
243         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
244         writeTx.commit().get();
245
246         final TestDataTreeListener listener = new TestDataTreeListener(latch);
247         final ListenerRegistration<TestDataTreeListener> listenerReg =
248                 dataTreeChangeService.registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
249
250         writeTx = domBroker.newWriteOnlyTransaction();
251         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH, OUTER_LIST_2);
252         writeTx.commit();
253
254         latch.await(5, TimeUnit.SECONDS);
255
256         assertEquals(2, listener.getReceivedChanges().size());
257         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
258         assertEquals(1, changes.size());
259
260         DataTreeCandidate candidate = changes.iterator().next();
261         assertNotNull(candidate);
262         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
263         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
264
265         changes = listener.getReceivedChanges().get(1);
266         assertEquals(1, changes.size());
267
268         candidate = changes.iterator().next();
269         assertNotNull(candidate);
270         candidateRoot = candidate.getRootNode();
271         checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.SUBTREE_MODIFIED, candidateRoot);
272         final DataTreeCandidateNode modifiedChild = candidateRoot.getModifiedChild(
273                 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).get();
274         checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, modifiedChild);
275         listenerReg.close();
276     }
277
278     @Test
279     public void rootModificationChildListenerTest() throws ExecutionException, InterruptedException {
280         final CountDownLatch latch = new CountDownLatch(2);
281
282         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
283         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
284                 dataTreeChangeService);
285
286         DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
287         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
288         writeTx.commit().get();
289
290         final TestDataTreeListener listener = new TestDataTreeListener(latch);
291         final ListenerRegistration<TestDataTreeListener> listenerReg =
292                 dataTreeChangeService.registerDataTreeChangeListener(OUTER_LIST_DATA_TREE_ID, listener);
293
294         writeTx = domBroker.newWriteOnlyTransaction();
295         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2);
296         writeTx.commit().get();
297
298         latch.await(1, TimeUnit.SECONDS);
299
300         assertEquals(2, listener.getReceivedChanges().size());
301         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
302         assertEquals(1, changes.size());
303
304         DataTreeCandidate candidate = changes.iterator().next();
305         assertNotNull(candidate);
306         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
307         checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot);
308
309         changes = listener.getReceivedChanges().get(1);
310         assertEquals(1, changes.size());
311
312         candidate = changes.iterator().next();
313         assertNotNull(candidate);
314         candidateRoot = candidate.getRootNode();
315         checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, candidateRoot);
316         listenerReg.close();
317     }
318
319     @Test
320     public void listEntryChangeNonRootRegistrationTest() throws ExecutionException, InterruptedException {
321         final CountDownLatch latch = new CountDownLatch(2);
322
323         final DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
324         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!",
325                 dataTreeChangeService);
326
327         DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
328         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
329         writeTx.commit().get();
330
331         final TestDataTreeListener listener = new TestDataTreeListener(latch);
332         final ListenerRegistration<TestDataTreeListener> listenerReg =
333                 dataTreeChangeService.registerDataTreeChangeListener(OUTER_LIST_DATA_TREE_ID, listener);
334
335         final NodeIdentifierWithPredicates outerListEntryId1 =
336                 NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
337         final NodeIdentifierWithPredicates outerListEntryId2 =
338                 NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
339         final NodeIdentifierWithPredicates outerListEntryId3 =
340                 NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3);
341
342         final MapEntryNode outerListEntry1 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
343         final MapEntryNode outerListEntry2 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
344         final MapEntryNode outerListEntry3 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3);
345
346         final MapNode listAfter = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
347                 .withChild(outerListEntry2)
348                 .withChild(outerListEntry3)
349                 .build();
350
351         writeTx = domBroker.newWriteOnlyTransaction();
352         writeTx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId1));
353         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId2),
354                 outerListEntry2);
355         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId3),
356                 outerListEntry3);
357         writeTx.commit();
358
359         latch.await(5, TimeUnit.SECONDS);
360
361         assertEquals(2, listener.getReceivedChanges().size());
362         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
363         assertEquals(1, changes.size());
364
365         DataTreeCandidate candidate = changes.iterator().next();
366         assertNotNull(candidate);
367         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
368         checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot);
369
370         changes = listener.getReceivedChanges().get(1);
371         assertEquals(1, changes.size());
372
373         candidate = changes.iterator().next();
374         assertNotNull(candidate);
375         candidateRoot = candidate.getRootNode();
376         checkChange(OUTER_LIST, listAfter, ModificationType.SUBTREE_MODIFIED, candidateRoot);
377         final DataTreeCandidateNode entry1Canditate = candidateRoot.getModifiedChild(outerListEntryId1).get();
378         checkChange(outerListEntry1, null, ModificationType.DELETE, entry1Canditate);
379         final DataTreeCandidateNode entry2Canditate = candidateRoot.getModifiedChild(outerListEntryId2).get();
380         checkChange(null, outerListEntry2, ModificationType.WRITE, entry2Canditate);
381         final DataTreeCandidateNode entry3Canditate = candidateRoot.getModifiedChild(outerListEntryId3).get();
382         checkChange(null, outerListEntry3, ModificationType.WRITE, entry3Canditate);
383         listenerReg.close();
384     }
385
386     private static void checkChange(final NormalizedNode<?, ?> expectedBefore,
387                                     final NormalizedNode<?, ?> expectedAfter,
388                                     final ModificationType expectedMod,
389                                     final DataTreeCandidateNode candidateNode) {
390         if (expectedBefore != null) {
391             assertTrue(candidateNode.getDataBefore().isPresent());
392             assertEquals(expectedBefore, candidateNode.getDataBefore().get());
393         } else {
394             assertFalse(candidateNode.getDataBefore().isPresent());
395         }
396
397         if (expectedAfter != null) {
398             assertTrue(candidateNode.getDataAfter().isPresent());
399             assertEquals(expectedAfter, candidateNode.getDataAfter().get());
400         } else {
401             assertFalse(candidateNode.getDataAfter().isPresent());
402         }
403
404         assertEquals(expectedMod, candidateNode.getModificationType());
405     }
406
407     private DOMDataTreeChangeService getDOMDataTreeChangeService() {
408         return domBroker.getExtensions().getInstance(DOMDataTreeChangeService.class);
409     }
410
411     static class CommitExecutorService extends ForwardingExecutorService {
412
413         ExecutorService delegate;
414
415         CommitExecutorService(final ExecutorService delegate) {
416             this.delegate = delegate;
417         }
418
419         @Override
420         protected ExecutorService delegate() {
421             return delegate;
422         }
423     }
424
425     static class TestDataTreeListener implements DOMDataTreeChangeListener {
426
427         private final List<Collection<DataTreeCandidate>> receivedChanges = new ArrayList<>();
428         private final CountDownLatch latch;
429
430         TestDataTreeListener(final CountDownLatch latch) {
431             this.latch = latch;
432         }
433
434         @Override
435         public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
436             receivedChanges.add(changes);
437             latch.countDown();
438         }
439
440         public List<Collection<DataTreeCandidate>> getReceivedChanges() {
441             return receivedChanges;
442         }
443     }
444 }