Search in sources :

Example 6 with MockDataTreeChangeListener

use of org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener in project controller by opendaylight.

the class DataTreeChangeListenerSupportTest method testInitialChangeListenerEventWithWildcardedListPath.

@Test
public void testInitialChangeListenerEventWithWildcardedListPath() throws DataValidationFailedException {
    mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
    MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 1).getKey();
    listener.waitForChangeEvents();
    listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
}
Also used : MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) Test(org.junit.Test)

Example 7 with MockDataTreeChangeListener

use of org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener in project controller by opendaylight.

the class DataTreeChangeListenerSupportTest method testInitialChangeListenerEventWithNestedWildcardedListsPath.

@Test
public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws DataValidationFailedException {
    mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
    MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
    listener.waitForChangeEvents();
    listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"), innerEntryPath(2, "four"));
    // Register for a specific outer list entry
    MockDataTreeChangeListener listener2 = registerChangeListener(OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
    listener2.waitForChangeEvents();
    listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
    listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
    listener.reset(1);
    listener2.reset(1);
    mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("three")))));
    listener.waitForChangeEvents();
    listener.verifyNotifiedData(innerEntryPath(1, "three"));
    listener2.waitForChangeEvents();
    listener2.verifyNotifiedData(innerEntryPath(1, "three"));
}
Also used : MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) Test(org.junit.Test)

Example 8 with MockDataTreeChangeListener

use of org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener in project controller by opendaylight.

the class ShardTest method testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration.

@SuppressWarnings("serial")
@Test
public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
    final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
    final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
    final Creator<Shard> creator = new Creator<Shard>() {

        boolean firstElectionTimeout = true;

        @Override
        public Shard create() throws Exception {
            return new Shard(newShardBuilder()) {

                @Override
                public void handleCommand(final Object message) {
                    if (message instanceof ElectionTimeout && firstElectionTimeout) {
                        firstElectionTimeout = false;
                        final ActorRef self = getSelf();
                        new Thread(() -> {
                            Uninterruptibles.awaitUninterruptibly(onChangeListenerRegistered, 5, TimeUnit.SECONDS);
                            self.tell(message, self);
                        }).start();
                        onFirstElectionTimeout.countDown();
                    } else {
                        super.handleCommand(message);
                    }
                }
            };
        }
    };
    setupInMemorySnapshotStore();
    final YangInstanceIdentifier path = TestModel.TEST_PATH;
    final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
    final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path), "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
    final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
    new ShardTestKit(getSystem()) {

        {
            assertEquals("Got first ElectionTimeout", true, onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
            shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
            shard.tell(FindLeader.INSTANCE, getRef());
            final FindLeaderReply findLeadeReply = expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
            assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
            onChangeListenerRegistered.countDown();
            // TODO: investigate why we do not receive data chage events
            listener.waitForChangeEvents();
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Creator(akka.japi.Creator) CountDownLatch(java.util.concurrent.CountDownLatch) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) FindLeaderReply(org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) Test(org.junit.Test)

Example 9 with MockDataTreeChangeListener

use of org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener in project controller by opendaylight.

the class ShardTest method testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed.

@Test
public void testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed";
            dataStoreContextBuilder.shardElectionTimeoutFactor(1000).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
            final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(0);
            final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
            setupInMemorySnapshotStore();
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(testName + "-shard"));
            waitUntilNoLeader(shard);
            shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
            final ActorSelection regActor = getSystem().actorSelection(reply.getListenerRegistrationPath());
            regActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), getRef());
            expectMsgClass(CloseDataTreeNotificationListenerRegistrationReply.class);
            shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
            listener.expectNoMoreChanges("Received unexpected change after close");
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorSelection(akka.actor.ActorSelection) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) Test(org.junit.Test)

Example 10 with MockDataTreeChangeListener

use of org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener in project controller by opendaylight.

the class ShardTest method testRegisterDataTreeChangeListener.

@Test
public void testRegisterDataTreeChangeListener() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testRegisterDataTreeChangeListener");
            waitUntilLeader(shard);
            shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
            final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
            final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener");
            shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("3 seconds"), RegisterDataTreeNotificationListenerReply.class);
            final String replyPath = reply.getListenerRegistrationPath().toString();
            assertTrue("Incorrect reply path: " + replyPath, replyPath.matches("akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
            final YangInstanceIdentifier path = TestModel.TEST_PATH;
            writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            listener.waitForChangeEvents();
        }
    };
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Aggregations

MockDataTreeChangeListener (org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener)12 Test (org.junit.Test)11 ActorRef (akka.actor.ActorRef)6 TestActorRef (akka.testkit.TestActorRef)6 RegisterDataTreeChangeListener (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener)6 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)6 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)4 ActorSelection (akka.actor.ActorSelection)2 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)2 Creator (akka.japi.Creator)1 TestKit (akka.testkit.javadsl.TestKit)1 Timeout (akka.util.Timeout)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)1 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)1 ElectionTimeout (org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout)1 FindLeaderReply (org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply)1 DataValidationFailedException (org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException)1