Search in sources :

Example 1 with DataChanged

use of org.opendaylight.controller.cluster.datastore.messages.DataChanged 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 DataChanged

use of org.opendaylight.controller.cluster.datastore.messages.DataChanged 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 DataChanged

use of org.opendaylight.controller.cluster.datastore.messages.DataChanged in project controller by opendaylight.

the class DataChangeListener method dataChanged.

@SuppressWarnings("checkstyle:IllegalCatch")
private void dataChanged(Object message) {
    // Do nothing if notifications are not enabled
    if (!notificationsEnabled) {
        LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
        return;
    }
    DataChanged reply = (DataChanged) message;
    AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
    LOG.debug("Sending change notification {} to listener {}", change, listener);
    notificationCount++;
    try {
        this.listener.onDataChanged(change);
    } catch (RuntimeException e) {
        LOG.error(String.format("Error notifying listener %s", this.listener), e);
    }
    if (isValidSender(getSender())) {
        getSender().tell(DataChangedReply.INSTANCE, getSelf());
    }
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 4 with DataChanged

use of org.opendaylight.controller.cluster.datastore.messages.DataChanged in project controller by opendaylight.

the class DataChangeListenerTest method testDataChangedWithListenerRuntimeEx.

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

        {
            final AsyncDataChangeEvent mockChangeEvent1 = Mockito.mock(AsyncDataChangeEvent.class);
            final AsyncDataChangeEvent mockChangeEvent2 = Mockito.mock(AsyncDataChangeEvent.class);
            final AsyncDataChangeEvent mockChangeEvent3 = Mockito.mock(AsyncDataChangeEvent.class);
            AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
            Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataChanged(mockChangeEvent2);
            Props props = DataChangeListener.props(mockListener, TEST_PATH);
            ActorRef subject = getSystem().actorOf(props, "testDataChangedWithListenerRuntimeEx");
            // Let the DataChangeListener know that notifications should be
            // enabled
            subject.tell(new EnableNotification(true, "test"), getRef());
            subject.tell(new DataChanged(mockChangeEvent1), getRef());
            expectMsgClass(DataChangedReply.class);
            subject.tell(new DataChanged(mockChangeEvent2), getRef());
            expectMsgClass(DataChangedReply.class);
            subject.tell(new DataChanged(mockChangeEvent3), getRef());
            expectMsgClass(DataChangedReply.class);
            Mockito.verify(mockListener).onDataChanged(mockChangeEvent1);
            Mockito.verify(mockListener).onDataChanged(mockChangeEvent2);
            Mockito.verify(mockListener).onDataChanged(mockChangeEvent3);
        }
    };
}
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 5 with DataChanged

use of org.opendaylight.controller.cluster.datastore.messages.DataChanged in project controller by opendaylight.

the class DataChangeListenerTest method testDataChangedWithNoSender.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testDataChangedWithNoSender() {
    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, "testDataChangedWithNoSender");
            getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
            subject.tell(new DataChanged(mockChangeEvent), ActorRef.noSender());
            // Make sure no DataChangedReply is sent to DeadLetters.
            while (true) {
                DeadLetter deadLetter;
                try {
                    deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
                } catch (AssertionError e) {
                    // Timed out - got no DeadLetter - this is good
                    break;
                }
                // We may get DeadLetters for other messages we don't care
                // about.
                Assert.assertFalse("Unexpected DataChangedReply", deadLetter.message() instanceof DataChangedReply);
            }
        }
    };
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) DeadLetter(akka.actor.DeadLetter) 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) DataChangedReply(org.opendaylight.controller.cluster.datastore.messages.DataChangedReply) Test(org.junit.Test)

Aggregations

DataChanged (org.opendaylight.controller.cluster.datastore.messages.DataChanged)5 ActorRef (akka.actor.ActorRef)4 Props (akka.actor.Props)4 TestKit (akka.testkit.javadsl.TestKit)4 Test (org.junit.Test)4 AsyncDataChangeEvent (org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent)4 AsyncDataChangeListener (org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener)4 EnableNotification (org.opendaylight.controller.cluster.datastore.messages.EnableNotification)2 DeadLetter (akka.actor.DeadLetter)1 DataChangedReply (org.opendaylight.controller.cluster.datastore.messages.DataChangedReply)1 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1