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