Search in sources :

Example 6 with RegisterDataTreeChangeListener

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

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

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

Example 9 with RegisterDataTreeChangeListener

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

the class DataTreeChangeListenerProxyTest method testSuccessfulRegistrationForClusteredListener.

@Test(timeout = 10000)
public void testSuccessfulRegistrationForClusteredListener() {
    new TestKit(getSystem()) {

        {
            ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class), mock(Configuration.class));
            ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(ClusteredDOMDataTreeChangeListener.class);
            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
            final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path);
            new Thread(() -> proxy.init("shard-1")).start();
            FiniteDuration timeout = duration("5 seconds");
            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
            reply(new LocalShardFound(getRef()));
            RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
            Assert.assertEquals("getPath", path, registerMsg.getPath());
            Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
            proxy.close();
        }
    };
}
Also used : LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) FiniteDuration(scala.concurrent.duration.FiniteDuration) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) Test(org.junit.Test)

Example 10 with RegisterDataTreeChangeListener

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

the class DataTreeChangeListenerProxyTest method testSuccessfulRegistration.

@Test(timeout = 10000)
public void testSuccessfulRegistration() {
    new TestKit(getSystem()) {

        {
            ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class), mock(Configuration.class));
            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(actorContext, mockListener, path);
            new Thread(() -> proxy.init("shard-1")).start();
            FiniteDuration timeout = duration("5 seconds");
            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
            reply(new LocalShardFound(getRef()));
            RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
            Assert.assertEquals("getPath", path, registerMsg.getPath());
            Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
            reply(new RegisterDataTreeNotificationListenerReply(getRef()));
            for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
                Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
            }
            Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()), proxy.getListenerRegistrationActor());
            watch(proxy.getDataChangeListenerActor());
            proxy.close();
            // The listener registration actor should get a Close message
            expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
            // The DataChangeListener actor should be terminated
            expectMsgClass(timeout, Terminated.class);
            proxy.close();
            expectNoMsg();
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) FiniteDuration(scala.concurrent.duration.FiniteDuration) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) DOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) Test(org.junit.Test)

Aggregations

RegisterDataTreeChangeListener (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener)10 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)8 Test (org.junit.Test)7 ActorRef (akka.actor.ActorRef)6 TestActorRef (akka.testkit.TestActorRef)6 MockDataTreeChangeListener (org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener)6 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)5 ClusteredDOMDataTreeChangeListener (org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener)3 TestKit (akka.testkit.javadsl.TestKit)2 Configuration (org.opendaylight.controller.cluster.datastore.config.Configuration)2 FindLocalShard (org.opendaylight.controller.cluster.datastore.messages.FindLocalShard)2 LocalShardFound (org.opendaylight.controller.cluster.datastore.messages.LocalShardFound)2 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)2 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)2 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)2 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 ActorSelection (akka.actor.ActorSelection)1 Creator (akka.japi.Creator)1 Timeout (akka.util.Timeout)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1