Fix checkstyle issues in module sal-dom-broker
[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         executor = new DeadlockDetectingListeningExecutorService(commitExecutor,
105                                                                  TransactionCommitDeadlockException
106                                                                          .DEADLOCK_EXCEPTION_SUPPLIER,
107                                                                  futureExecutor);
108         domBroker = new SerializedDOMDataBroker(stores, executor);
109     }
110
111     @After
112     public void tearDown() {
113         if (executor != null) {
114             executor.shutdownNow();
115         }
116
117         if (futureExecutor != null) {
118             futureExecutor.shutdownNow();
119         }
120     }
121
122     @Test
123     public void writeContainerEmptyTreeTest() throws InterruptedException {
124         CountDownLatch latch = new CountDownLatch(1);
125
126         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
127         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
128
129         final TestDataTreeListener listener = new TestDataTreeListener(latch);
130         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
131                 .registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
132
133         final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
134         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
135         writeTx.submit();
136
137         latch.await(5, TimeUnit.SECONDS);
138
139         assertEquals(1, listener.getReceivedChanges().size());
140         final Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
141         assertEquals(1, changes.size());
142
143         DataTreeCandidate candidate = changes.iterator().next();
144         assertNotNull(candidate);
145         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
146         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
147         listenerReg.close();
148     }
149
150     @Test
151     public void replaceContainerContainerInTreeTest() throws InterruptedException, TransactionCommitFailedException {
152         final CountDownLatch latch = new CountDownLatch(2);
153
154         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
155         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
156
157         DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
158         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
159         writeTx.submit().checkedGet();
160
161         final TestDataTreeListener listener = new TestDataTreeListener(latch);
162         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
163                 .registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
164         writeTx = domBroker.newWriteOnlyTransaction();
165         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2);
166         writeTx.submit();
167
168         latch.await(5, TimeUnit.SECONDS);
169
170         assertEquals(2, listener.getReceivedChanges().size());
171         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
172         assertEquals(1, changes.size());
173
174         DataTreeCandidate candidate = changes.iterator().next();
175         assertNotNull(candidate);
176         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
177         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
178
179         changes = listener.getReceivedChanges().get(1);
180         assertEquals(1, changes.size());
181
182         candidate = changes.iterator().next();
183         assertNotNull(candidate);
184         candidateRoot = candidate.getRootNode();
185         checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.WRITE, candidateRoot);
186         listenerReg.close();
187     }
188
189     @Test
190     public void deleteContainerContainerInTreeTest() throws InterruptedException, TransactionCommitFailedException {
191         final CountDownLatch latch = new CountDownLatch(2);
192
193         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
194         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
195
196         DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
197         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
198         writeTx.submit().checkedGet();
199
200         final TestDataTreeListener listener = new TestDataTreeListener(latch);
201         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
202                 .registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
203
204         writeTx = domBroker.newWriteOnlyTransaction();
205         writeTx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
206         writeTx.submit();
207
208         latch.await(5, TimeUnit.SECONDS);
209
210         assertEquals(2, listener.getReceivedChanges().size());
211         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
212         assertEquals(1, changes.size());
213
214         DataTreeCandidate candidate = changes.iterator().next();
215         assertNotNull(candidate);
216         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
217         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
218
219         changes = listener.getReceivedChanges().get(1);
220         assertEquals(1, changes.size());
221
222         candidate = changes.iterator().next();
223         assertNotNull(candidate);
224         candidateRoot = candidate.getRootNode();
225         checkChange(TEST_CONTAINER, null, ModificationType.DELETE, candidateRoot);
226         listenerReg.close();
227     }
228
229     @Test
230     public void replaceChildListContainerInTreeTest() throws InterruptedException, TransactionCommitFailedException {
231         final CountDownLatch latch = new CountDownLatch(2);
232
233         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
234         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
235
236         DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
237         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
238         writeTx.submit().checkedGet();
239
240         final TestDataTreeListener listener = new TestDataTreeListener(latch);
241         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
242                 .registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener);
243
244         writeTx = domBroker.newWriteOnlyTransaction();
245         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH, OUTER_LIST_2);
246         writeTx.submit();
247
248         latch.await(5, TimeUnit.SECONDS);
249
250         assertEquals(2, listener.getReceivedChanges().size());
251         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
252         assertEquals(1, changes.size());
253
254         DataTreeCandidate candidate = changes.iterator().next();
255         assertNotNull(candidate);
256         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
257         checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot);
258
259         changes = listener.getReceivedChanges().get(1);
260         assertEquals(1, changes.size());
261
262         candidate = changes.iterator().next();
263         assertNotNull(candidate);
264         candidateRoot = candidate.getRootNode();
265         checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.SUBTREE_MODIFIED, candidateRoot);
266         final DataTreeCandidateNode modifiedChild = candidateRoot
267                 .getModifiedChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME));
268         assertNotNull(modifiedChild);
269         checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, modifiedChild);
270         listenerReg.close();
271     }
272
273     @Test
274     public void rootModificationChildListenerTest() throws InterruptedException, TransactionCommitFailedException {
275         final CountDownLatch latch = new CountDownLatch(2);
276
277         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
278         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
279
280         DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
281         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
282         writeTx.submit().checkedGet();
283
284         final TestDataTreeListener listener = new TestDataTreeListener(latch);
285         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
286                 .registerDataTreeChangeListener(OUTER_LIST_DATA_TREE_ID, listener);
287
288         writeTx = domBroker.newWriteOnlyTransaction();
289         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2);
290         writeTx.submit().checkedGet();
291
292         latch.await(1, TimeUnit.SECONDS);
293
294         assertEquals(2, listener.getReceivedChanges().size());
295         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
296         assertEquals(1, changes.size());
297
298         DataTreeCandidate candidate = changes.iterator().next();
299         assertNotNull(candidate);
300         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
301         checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot);
302
303         changes = listener.getReceivedChanges().get(1);
304         assertEquals(1, changes.size());
305
306         candidate = changes.iterator().next();
307         assertNotNull(candidate);
308         candidateRoot = candidate.getRootNode();
309         checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, candidateRoot);
310         listenerReg.close();
311     }
312
313     @Test
314     public void listEntryChangeNonRootRegistrationTest() throws InterruptedException, TransactionCommitFailedException {
315         final CountDownLatch latch = new CountDownLatch(2);
316
317         DOMDataTreeChangeService dataTreeChangeService = getDOMDataTreeChangeService();
318         assertNotNull("DOMDataTreeChangeService not found, cannot continue with test!", dataTreeChangeService);
319
320         DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
321         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER);
322         writeTx.submit().checkedGet();
323
324         final TestDataTreeListener listener = new TestDataTreeListener(latch);
325         final ListenerRegistration<TestDataTreeListener> listenerReg = dataTreeChangeService
326                 .registerDataTreeChangeListener(OUTER_LIST_DATA_TREE_ID, listener);
327
328         final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId1
329                 = new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME,
330                                                                           TestModel.ID_QNAME, 1);
331         final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId2
332                 = new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME,
333                                                                           TestModel.ID_QNAME, 2);
334         final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId3
335                 = new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME,
336                                                                           TestModel.ID_QNAME, 3);
337
338         final MapEntryNode outerListEntry1 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
339         final MapEntryNode outerListEntry2 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
340         final MapEntryNode outerListEntry3 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3);
341
342         final MapNode listAfter = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(outerListEntry2)
343                 .withChild(outerListEntry3).build();
344
345         writeTx = domBroker.newWriteOnlyTransaction();
346         writeTx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId1));
347         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId2),
348                     outerListEntry2);
349         writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId3),
350                     outerListEntry3);
351         writeTx.submit();
352
353         latch.await(5, TimeUnit.SECONDS);
354
355         assertEquals(2, listener.getReceivedChanges().size());
356         Collection<DataTreeCandidate> changes = listener.getReceivedChanges().get(0);
357         assertEquals(1, changes.size());
358
359         DataTreeCandidate candidate = changes.iterator().next();
360         assertNotNull(candidate);
361         DataTreeCandidateNode candidateRoot = candidate.getRootNode();
362         checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot);
363
364         changes = listener.getReceivedChanges().get(1);
365         assertEquals(1, changes.size());
366
367         candidate = changes.iterator().next();
368         assertNotNull(candidate);
369         candidateRoot = candidate.getRootNode();
370         checkChange(OUTER_LIST, listAfter, ModificationType.SUBTREE_MODIFIED, candidateRoot);
371         final DataTreeCandidateNode entry1Canditate = candidateRoot.getModifiedChild(outerListEntryId1);
372         checkChange(outerListEntry1, null, ModificationType.DELETE, entry1Canditate);
373         final DataTreeCandidateNode entry2Canditate = candidateRoot.getModifiedChild(outerListEntryId2);
374         checkChange(null, outerListEntry2, ModificationType.WRITE, entry2Canditate);
375         final DataTreeCandidateNode entry3Canditate = candidateRoot.getModifiedChild(outerListEntryId3);
376         checkChange(null, outerListEntry3, ModificationType.WRITE, entry3Canditate);
377         listenerReg.close();
378     }
379
380     private static void checkChange(final NormalizedNode<?, ?> expectedBefore, final NormalizedNode<?, ?> expectedAfter,
381                                     final ModificationType expectedMod, final DataTreeCandidateNode candidateNode) {
382         if (expectedBefore != null) {
383             assertTrue(candidateNode.getDataBefore().isPresent());
384             assertEquals(expectedBefore, candidateNode.getDataBefore().get());
385         } else {
386             assertFalse(candidateNode.getDataBefore().isPresent());
387         }
388
389         if (expectedAfter != null) {
390             assertTrue(candidateNode.getDataAfter().isPresent());
391             assertEquals(expectedAfter, candidateNode.getDataAfter().get());
392         } else {
393             assertFalse(candidateNode.getDataAfter().isPresent());
394         }
395
396         assertEquals(expectedMod, candidateNode.getModificationType());
397     }
398
399     private DOMDataTreeChangeService getDOMDataTreeChangeService() {
400         final DOMDataBrokerExtension extension = domBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
401         if (extension == null) {
402             return null;
403         }
404         DOMDataTreeChangeService dataTreeChangeService = null;
405         if (extension instanceof DOMDataTreeChangeService) {
406             dataTreeChangeService = (DOMDataTreeChangeService) extension;
407         }
408         return dataTreeChangeService;
409     }
410
411
412     static class CommitExecutorService extends ForwardingExecutorService {
413
414         ExecutorService delegate;
415
416         CommitExecutorService(final ExecutorService delegate) {
417             this.delegate = delegate;
418         }
419
420         @Override
421         protected ExecutorService delegate() {
422             return delegate;
423         }
424     }
425
426     static class TestDataTreeListener implements DOMDataTreeChangeListener {
427
428         private final List<Collection<DataTreeCandidate>> receivedChanges = new ArrayList<>();
429         private final CountDownLatch latch;
430
431         TestDataTreeListener(final CountDownLatch latch) {
432             this.latch = latch;
433         }
434
435         @Override
436         public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
437             receivedChanges.add(changes);
438             latch.countDown();
439         }
440
441         public List<Collection<DataTreeCandidate>> getReceivedChanges() {
442             return receivedChanges;
443         }
444     }
445 }