Search in sources :

Example 1 with AsyncDataChangeEvent

use of org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent in project controller by opendaylight.

the class DataChangeListenerTest method testDataChangedWhenNotificationsAreEnabled.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testDataChangedWhenNotificationsAreEnabled() {
    new TestKit(getSystem()) {

        {
            final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
            final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
            final Props props = DataChangeListener.props(mockListener, TEST_PATH);
            final ActorRef subject = getSystem().actorOf(props, "testDataChangedNotificationsEnabled");
            // Let the DataChangeListener know that notifications should be
            // enabled
            subject.tell(new EnableNotification(true, "test"), getRef());
            subject.tell(new DataChanged(mockChangeEvent), getRef());
            expectMsgClass(DataChangedReply.class);
            Mockito.verify(mockListener).onDataChanged(mockChangeEvent);
        }
    };
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) EnableNotification(org.opendaylight.controller.cluster.datastore.messages.EnableNotification) AsyncDataChangeListener(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) Props(akka.actor.Props) AsyncDataChangeEvent(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent) Test(org.junit.Test)

Example 2 with AsyncDataChangeEvent

use of org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent in project controller by opendaylight.

the class DataChangeListenerTest method testDataChangedWhenNotificationsAreDisabled.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testDataChangedWhenNotificationsAreDisabled() {
    new TestKit(getSystem()) {

        {
            final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
            final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
            final Props props = DataChangeListener.props(mockListener, TEST_PATH);
            final ActorRef subject = getSystem().actorOf(props, "testDataChangedNotificationsDisabled");
            subject.tell(new DataChanged(mockChangeEvent), getRef());
            within(duration("1 seconds"), () -> {
                expectNoMsg();
                Mockito.verify(mockListener, Mockito.never()).onDataChanged(Mockito.any(AsyncDataChangeEvent.class));
                return null;
            });
        }
    };
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) AsyncDataChangeListener(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) Props(akka.actor.Props) AsyncDataChangeEvent(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent) Test(org.junit.Test)

Example 3 with AsyncDataChangeEvent

use of org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent in project controller by opendaylight.

the class DOMBrokerTest method testDataChangeListenerDoingAsyncWriteTxSubmit.

/**
 * Tests a DataChangeListener that does an async submit of a write Tx in its onDataChanged method.
 * This should succeed without deadlock.
 */
@Test
@SuppressWarnings("checkstyle:IllegalThrows")
public void testDataChangeListenerDoingAsyncWriteTxSubmit() throws Throwable {
    final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
    final CountDownLatch commitCompletedLatch = new CountDownLatch(1);
    TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {

        @Override
        public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
            DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
            writeTx.put(OPERATIONAL, TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME));
            Futures.addCallback(writeTx.submit(), new FutureCallback<Void>() {

                @Override
                public void onSuccess(final Void result) {
                    commitCompletedLatch.countDown();
                }

                @Override
                public void onFailure(final Throwable throwable) {
                    caughtCommitEx.set(throwable);
                    commitCompletedLatch.countDown();
                }
            });
            super.onDataChanged(change);
        }
    };
    domBroker.registerDataChangeListener(OPERATIONAL, TestModel.TEST_PATH, dcListener, DataChangeScope.BASE);
    final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
    assertNotNull(writeTx);
    writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
    AtomicReference<Throwable> caughtEx = submitTxAsync(writeTx);
    dcListener.waitForChange();
    if (caughtEx.get() != null) {
        throw caughtEx.get();
    }
    assertTrue("Commit Future was not invoked", commitCompletedLatch.await(5, TimeUnit.SECONDS));
    if (caughtCommitEx.get() != null) {
        throw caughtCommitEx.get();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncDataChangeEvent(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction) Test(org.junit.Test)

Example 4 with AsyncDataChangeEvent

use of org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent in project controller by opendaylight.

the class WildcardedDataChangeListenerTest method testWriteByReplace.

@Test
public void testWriteByReplace() throws InterruptedException, TimeoutException, ExecutionException {
    DataBroker dataBroker = testContext.getDataBroker();
    final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
    dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, DEEP_WILDCARDED_PATH, dataChangeEvent -> eventFuture.set(dataChangeEvent), DataChangeScope.SUBTREE);
    final WriteTransaction cwuTx = dataBroker.newWriteOnlyTransaction();
    cwuTx.put(LogicalDatastoreType.OPERATIONAL, NODE_0_CWU_PATH, CWU, true);
    cwuTx.submit().get(5, TimeUnit.SECONDS);
    assertFalse(eventFuture.isDone());
    final WriteTransaction lvuTx = dataBroker.newWriteOnlyTransaction();
    TreeComplexUsesAugment tcua = new TreeComplexUsesAugmentBuilder().setListViaUses(Collections.singletonList(LVU)).build();
    lvuTx.put(LogicalDatastoreType.OPERATIONAL, NODE_0_TCU_PATH, tcua, true);
    lvuTx.put(LogicalDatastoreType.OPERATIONAL, NODE_1_LVU_PATH, LVU, true);
    lvuTx.submit().get(5, TimeUnit.SECONDS);
    validateEvent(eventFuture.get(1000, TimeUnit.MILLISECONDS));
}
Also used : WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) TreeComplexUsesAugment(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment) TreeComplexUsesAugmentBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) AsyncDataChangeEvent(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent) AbstractDataServiceTest(org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest) Test(org.junit.Test)

Example 5 with AsyncDataChangeEvent

use of org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent in project controller by opendaylight.

the class WildcardedDataChangeListenerTest method testNoChangeOnReplaceWithSameValue.

@Test
public void testNoChangeOnReplaceWithSameValue() throws InterruptedException, TimeoutException, ExecutionException {
    DataBroker dataBroker = testContext.getDataBroker();
    // We wrote initial state NODE_0_FLOW
    final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
    transaction.put(LogicalDatastoreType.OPERATIONAL, NODE_0_LVU_PATH, LVU, true);
    transaction.submit().get(5, TimeUnit.SECONDS);
    // We registered DataChangeListener
    final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
    dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, DEEP_WILDCARDED_PATH, dataChangeEvent -> eventFuture.set(dataChangeEvent), DataChangeScope.SUBTREE);
    assertFalse(eventFuture.isDone());
    final WriteTransaction secondTx = dataBroker.newWriteOnlyTransaction();
    secondTx.put(LogicalDatastoreType.OPERATIONAL, NODE_0_LVU_PATH, LVU, true);
    secondTx.put(LogicalDatastoreType.OPERATIONAL, NODE_1_LVU_PATH, LVU, true);
    secondTx.submit().get(5, TimeUnit.SECONDS);
    AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event = eventFuture.get(1000, TimeUnit.MILLISECONDS);
    assertNotNull(event);
    // Data change should contains NODE_1 Flow - which was added
    assertTrue(event.getCreatedData().containsKey(NODE_1_LVU_PATH));
    // Data change must not containe NODE_0 Flow which was replaced with same value.
    assertFalse(event.getUpdatedData().containsKey(NODE_0_LVU_PATH));
}
Also used : WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) AsyncDataChangeEvent(org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent) AbstractDataServiceTest(org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 AsyncDataChangeEvent (org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent)12 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)5 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)5 AbstractDataServiceTest (org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest)5 ActorRef (akka.actor.ActorRef)4 Props (akka.actor.Props)4 TestKit (akka.testkit.javadsl.TestKit)4 DataChanged (org.opendaylight.controller.cluster.datastore.messages.DataChanged)4 AsyncDataChangeListener (org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener)4 DataObject (org.opendaylight.yangtools.yang.binding.DataObject)4 InstanceIdentifier (org.opendaylight.yangtools.yang.binding.InstanceIdentifier)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EnableNotification (org.opendaylight.controller.cluster.datastore.messages.EnableNotification)2 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)2 TreeComplexUsesAugment (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment)2 DeadLetter (akka.actor.DeadLetter)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1