X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=dom%2Fmdsal-dom-broker%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fdom%2Fbroker%2FDOMDataTreeListenerTest.java;h=39947d728bf1b907cadc95283bf20d64015c25b1;hb=11408d627adca7eb71ac956c3ad01f75b6b91596;hp=51c40728d41399e029ff6bd9b756baa09b1af7db;hpb=c4d02d92b7125cb5b0aa2e67e3a8b5a7c6f92b21;p=mdsal.git diff --git a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/DOMDataTreeListenerTest.java b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/DOMDataTreeListenerTest.java index 51c40728d4..39947d728b 100644 --- a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/DOMDataTreeListenerTest.java +++ b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/DOMDataTreeListenerTest.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.mdsal.dom.broker; import static org.junit.Assert.assertEquals; @@ -20,66 +19,58 @@ import com.google.common.util.concurrent.ForwardingExecutorService; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import javax.annotation.Nonnull; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.mdsal.common.api.TransactionCommitDeadlockException; -import org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension; import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener; import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService; import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction; -import org.opendaylight.mdsal.dom.broker.util.TestModel; import org.opendaylight.mdsal.dom.spi.store.DOMStore; import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService; import org.opendaylight.yangtools.util.concurrent.SpecialExecutors; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode; +import org.opendaylight.yangtools.yang.data.tree.api.ModificationType; -public class DOMDataTreeListenerTest { +public class DOMDataTreeListenerTest extends AbstractDatastoreTest { - private SchemaContext schemaContext; private AbstractDOMDataBroker domBroker; private ListeningExecutorService executor; private ExecutorService futureExecutor; private CommitExecutorService commitExecutor; - private static final DataContainerChild OUTER_LIST = - ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME) + private static final MapNode OUTER_LIST = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME) .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)) .build(); - private static final DataContainerChild OUTER_LIST_2 = - ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME) + private static final MapNode OUTER_LIST_2 = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME) .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)) .build(); - private static final NormalizedNode TEST_CONTAINER = Builders.containerBuilder() + private static final NormalizedNode TEST_CONTAINER = Builders.containerBuilder() .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)) .withChild(OUTER_LIST) .build(); - private static final NormalizedNode TEST_CONTAINER_2 = Builders.containerBuilder() + private static final NormalizedNode TEST_CONTAINER_2 = Builders.containerBuilder() .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)) .withChild(OUTER_LIST_2) .build(); @@ -91,15 +82,14 @@ public class DOMDataTreeListenerTest { LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH); @Before - public void setupStore() throws Exception { + public void setupStore() { final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService()); final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService()); - schemaContext = TestModel.createTestContext(); - operStore.onGlobalContextUpdated(schemaContext); - configStore.onGlobalContextUpdated(schemaContext); + operStore.onModelContextUpdated(SCHEMA_CONTEXT); + configStore.onModelContextUpdated(SCHEMA_CONTEXT); final ImmutableMap stores = ImmutableMap.builder() @@ -108,7 +98,8 @@ public class DOMDataTreeListenerTest { .build(); commitExecutor = new CommitExecutorService(Executors.newSingleThreadExecutor()); - futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB"); + futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB", + DOMDataTreeListenerTest.class); executor = new DeadlockDetectingListeningExecutorService(commitExecutor, TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER, futureExecutor); domBroker = new SerializedDOMDataBroker(stores, executor); @@ -139,15 +130,15 @@ public class DOMDataTreeListenerTest { final DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit(); + writeTx.commit(); latch.await(5, TimeUnit.SECONDS); assertEquals(1, listener.getReceivedChanges().size()); - final Collection changes = listener.getReceivedChanges().get(0); + final List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - final DataTreeCandidate candidate = changes.iterator().next(); + final DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); final DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot); @@ -164,22 +155,22 @@ public class DOMDataTreeListenerTest { DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit().get(); + writeTx.commit().get(); final TestDataTreeListener listener = new TestDataTreeListener(latch); final ListenerRegistration listenerReg = dataTreeChangeService.registerDataTreeChangeListener(ROOT_DATA_TREE_ID, listener); writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2); - writeTx.submit(); + writeTx.commit(); latch.await(5, TimeUnit.SECONDS); assertEquals(2, listener.getReceivedChanges().size()); - Collection changes = listener.getReceivedChanges().get(0); + List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - DataTreeCandidate candidate = changes.iterator().next(); + DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot); @@ -187,7 +178,7 @@ public class DOMDataTreeListenerTest { changes = listener.getReceivedChanges().get(1); assertEquals(1, changes.size()); - candidate = changes.iterator().next(); + candidate = changes.get(0); assertNotNull(candidate); candidateRoot = candidate.getRootNode(); checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.WRITE, candidateRoot); @@ -204,7 +195,7 @@ public class DOMDataTreeListenerTest { DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit().get(); + writeTx.commit().get(); final TestDataTreeListener listener = new TestDataTreeListener(latch); final ListenerRegistration listenerReg = @@ -212,15 +203,15 @@ public class DOMDataTreeListenerTest { writeTx = domBroker.newWriteOnlyTransaction(); writeTx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH); - writeTx.submit(); + writeTx.commit(); latch.await(5, TimeUnit.SECONDS); assertEquals(2, listener.getReceivedChanges().size()); - Collection changes = listener.getReceivedChanges().get(0); + List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - DataTreeCandidate candidate = changes.iterator().next(); + DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot); @@ -228,7 +219,7 @@ public class DOMDataTreeListenerTest { changes = listener.getReceivedChanges().get(1); assertEquals(1, changes.size()); - candidate = changes.iterator().next(); + candidate = changes.get(0); assertNotNull(candidate); candidateRoot = candidate.getRootNode(); checkChange(TEST_CONTAINER, null, ModificationType.DELETE, candidateRoot); @@ -245,7 +236,7 @@ public class DOMDataTreeListenerTest { DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit().get(); + writeTx.commit().get(); final TestDataTreeListener listener = new TestDataTreeListener(latch); final ListenerRegistration listenerReg = @@ -253,15 +244,15 @@ public class DOMDataTreeListenerTest { writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH, OUTER_LIST_2); - writeTx.submit(); + writeTx.commit(); latch.await(5, TimeUnit.SECONDS); assertEquals(2, listener.getReceivedChanges().size()); - Collection changes = listener.getReceivedChanges().get(0); + List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - DataTreeCandidate candidate = changes.iterator().next(); + DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, TEST_CONTAINER, ModificationType.WRITE, candidateRoot); @@ -269,13 +260,12 @@ public class DOMDataTreeListenerTest { changes = listener.getReceivedChanges().get(1); assertEquals(1, changes.size()); - candidate = changes.iterator().next(); + candidate = changes.get(0); assertNotNull(candidate); candidateRoot = candidate.getRootNode(); checkChange(TEST_CONTAINER, TEST_CONTAINER_2, ModificationType.SUBTREE_MODIFIED, candidateRoot); final DataTreeCandidateNode modifiedChild = candidateRoot.getModifiedChild( - new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)); - assertNotNull(modifiedChild); + new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).get(); checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, modifiedChild); listenerReg.close(); } @@ -290,7 +280,7 @@ public class DOMDataTreeListenerTest { DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit().get(); + writeTx.commit().get(); final TestDataTreeListener listener = new TestDataTreeListener(latch); final ListenerRegistration listenerReg = @@ -298,15 +288,15 @@ public class DOMDataTreeListenerTest { writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER_2); - writeTx.submit().get(); + writeTx.commit().get(); latch.await(1, TimeUnit.SECONDS); assertEquals(2, listener.getReceivedChanges().size()); - Collection changes = listener.getReceivedChanges().get(0); + List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - DataTreeCandidate candidate = changes.iterator().next(); + DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot); @@ -314,7 +304,7 @@ public class DOMDataTreeListenerTest { changes = listener.getReceivedChanges().get(1); assertEquals(1, changes.size()); - candidate = changes.iterator().next(); + candidate = changes.get(0); assertNotNull(candidate); candidateRoot = candidate.getRootNode(); checkChange(OUTER_LIST, OUTER_LIST_2, ModificationType.WRITE, candidateRoot); @@ -331,21 +321,18 @@ public class DOMDataTreeListenerTest { DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction(); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, TEST_CONTAINER); - writeTx.submit().get(); + writeTx.commit().get(); final TestDataTreeListener listener = new TestDataTreeListener(latch); final ListenerRegistration listenerReg = dataTreeChangeService.registerDataTreeChangeListener(OUTER_LIST_DATA_TREE_ID, listener); - final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId1 = - new YangInstanceIdentifier.NodeIdentifierWithPredicates( - TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1); - final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId2 = - new YangInstanceIdentifier.NodeIdentifierWithPredicates( - TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2); - final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListEntryId3 = - new YangInstanceIdentifier.NodeIdentifierWithPredicates( - TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3); + final NodeIdentifierWithPredicates outerListEntryId1 = + NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1); + final NodeIdentifierWithPredicates outerListEntryId2 = + NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2); + final NodeIdentifierWithPredicates outerListEntryId3 = + NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3); final MapEntryNode outerListEntry1 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1); final MapEntryNode outerListEntry2 = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2); @@ -362,15 +349,15 @@ public class DOMDataTreeListenerTest { outerListEntry2); writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_LIST_PATH.node(outerListEntryId3), outerListEntry3); - writeTx.submit(); + writeTx.commit(); latch.await(5, TimeUnit.SECONDS); assertEquals(2, listener.getReceivedChanges().size()); - Collection changes = listener.getReceivedChanges().get(0); + List changes = listener.getReceivedChanges().get(0); assertEquals(1, changes.size()); - DataTreeCandidate candidate = changes.iterator().next(); + DataTreeCandidate candidate = changes.get(0); assertNotNull(candidate); DataTreeCandidateNode candidateRoot = candidate.getRootNode(); checkChange(null, OUTER_LIST, ModificationType.WRITE, candidateRoot); @@ -378,23 +365,21 @@ public class DOMDataTreeListenerTest { changes = listener.getReceivedChanges().get(1); assertEquals(1, changes.size()); - candidate = changes.iterator().next(); + candidate = changes.get(0); assertNotNull(candidate); candidateRoot = candidate.getRootNode(); checkChange(OUTER_LIST, listAfter, ModificationType.SUBTREE_MODIFIED, candidateRoot); - final DataTreeCandidateNode entry1Canditate = candidateRoot.getModifiedChild(outerListEntryId1); + final DataTreeCandidateNode entry1Canditate = candidateRoot.getModifiedChild(outerListEntryId1).get(); checkChange(outerListEntry1, null, ModificationType.DELETE, entry1Canditate); - final DataTreeCandidateNode entry2Canditate = candidateRoot.getModifiedChild(outerListEntryId2); + final DataTreeCandidateNode entry2Canditate = candidateRoot.getModifiedChild(outerListEntryId2).get(); checkChange(null, outerListEntry2, ModificationType.WRITE, entry2Canditate); - final DataTreeCandidateNode entry3Canditate = candidateRoot.getModifiedChild(outerListEntryId3); + final DataTreeCandidateNode entry3Canditate = candidateRoot.getModifiedChild(outerListEntryId3).get(); checkChange(null, outerListEntry3, ModificationType.WRITE, entry3Canditate); listenerReg.close(); } - private static void checkChange(final NormalizedNode expectedBefore, - final NormalizedNode expectedAfter, - final ModificationType expectedMod, - final DataTreeCandidateNode candidateNode) { + private static void checkChange(final NormalizedNode expectedBefore, final NormalizedNode expectedAfter, + final ModificationType expectedMod, final DataTreeCandidateNode candidateNode) { if (expectedBefore != null) { assertTrue(candidateNode.getDataBefore().isPresent()); assertEquals(expectedBefore, candidateNode.getDataBefore().get()); @@ -413,16 +398,7 @@ public class DOMDataTreeListenerTest { } private DOMDataTreeChangeService getDOMDataTreeChangeService() { - final DOMDataBrokerExtension extension = domBroker.getSupportedExtensions() - .get(DOMDataTreeChangeService.class); - if (extension == null) { - return null; - } - DOMDataTreeChangeService dataTreeChangeService = null; - if (extension instanceof DOMDataTreeChangeService) { - dataTreeChangeService = (DOMDataTreeChangeService) extension; - } - return dataTreeChangeService; + return domBroker.getExtensions().getInstance(DOMDataTreeChangeService.class); } static class CommitExecutorService extends ForwardingExecutorService { @@ -440,8 +416,7 @@ public class DOMDataTreeListenerTest { } static class TestDataTreeListener implements DOMDataTreeChangeListener { - - private final List> receivedChanges = new ArrayList<>(); + private final List> receivedChanges = new ArrayList<>(); private final CountDownLatch latch; TestDataTreeListener(final CountDownLatch latch) { @@ -449,12 +424,17 @@ public class DOMDataTreeListenerTest { } @Override - public void onDataTreeChanged(@Nonnull final Collection changes) { + public void onDataTreeChanged(final List changes) { receivedChanges.add(changes); latch.countDown(); } - public List> getReceivedChanges() { + @Override + public void onInitialData() { + // noop + } + + List> getReceivedChanges() { return receivedChanges; } }