Search in sources :

Example 1 with MockDataTreeChangeListener

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

the class ShardTest method testClusteredDataTreeChangeListenerRegistration.

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

        {
            final String testName = "testClusteredDataTreeChangeListenerRegistration";
            final ShardIdentifier followerShardID = ShardIdentifier.create("inventory", MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
            final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory", MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
            final TestActorRef<Shard> followerShard = actorFactory.createTestActor(Shard.builder().id(followerShardID).datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).peerAddresses(Collections.singletonMap(leaderShardID.toString(), "akka://test/user/" + leaderShardID.toString())).schemaContextProvider(() -> SCHEMA_CONTEXT).props().withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
            final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).peerAddresses(Collections.singletonMap(followerShardID.toString(), "akka://test/user/" + followerShardID.toString())).schemaContextProvider(() -> SCHEMA_CONTEXT).props().withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
            leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
            final String leaderPath = waitUntilLeader(followerShard);
            assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
            final YangInstanceIdentifier path = TestModel.TEST_PATH;
            final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
            final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
            followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
            writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            listener.waitForChangeEvents();
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) Test(org.junit.Test)

Example 2 with MockDataTreeChangeListener

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

the class ShardTest method testClusteredDataTreeChangeListenerWithDelayedRegistration.

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

        {
            final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistration";
            dataStoreContextBuilder.shardElectionTimeoutFactor(1000).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
            final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
            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());
            shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
            listener.waitForChangeEvents();
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) 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 3 with MockDataTreeChangeListener

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

the class DistributedDataStoreIntegrationTest method testDataTreeChangeListenerRegistration.

@Test
public void testDataTreeChangeListenerRegistration() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
                testWriteTransaction(dataStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
                ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore.registerTreeChangeListener(TestModel.TEST_PATH, listener);
                assertNotNull("registerTreeChangeListener returned null", listenerReg);
                IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("getTreeChangeListenerActors", 1, state.getTreeChangeListenerActors().size()));
                // Wait for the initial notification
                listener.waitForChangeEvents(TestModel.TEST_PATH);
                listener.reset(2);
                // Write 2 updates.
                testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
                YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
                testWriteTransaction(dataStore, listPath, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
                // Wait for the 2 updates.
                listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
                listenerReg.close();
                IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("getTreeChangeListenerActors", 0, state.getTreeChangeListenerActors().size()));
                testWriteTransaction(dataStore, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(), ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
                listener.expectNoMoreChanges("Received unexpected change after close");
            }
        }
    };
}
Also used : MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 4 with MockDataTreeChangeListener

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

the class DataTreeChangeListenerSupportTest method testInitialChangeListenerEventWithContainerPath.

@Test
public void testInitialChangeListenerEventWithContainerPath() throws DataValidationFailedException {
    writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
    Entry<MockDataTreeChangeListener, ActorSelection> entry = registerChangeListener(TEST_PATH, 1);
    MockDataTreeChangeListener listener = entry.getKey();
    listener.waitForChangeEvents();
    listener.verifyNotifiedData(TEST_PATH);
    listener.reset(1);
    writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
    listener.waitForChangeEvents();
    listener.verifyNotifiedData(TEST_PATH);
    listener.reset(1);
    TestKit kit = new TestKit(getSystem());
    entry.getValue().tell(CloseDataTreeNotificationListenerRegistration.getInstance(), kit.getRef());
    kit.expectMsgClass(kit.duration("5 seconds"), CloseDataTreeNotificationListenerRegistrationReply.class);
    writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
    listener.verifyNoNotifiedData(TEST_PATH);
}
Also used : MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorSelection(akka.actor.ActorSelection) TestKit(akka.testkit.javadsl.TestKit) Test(org.junit.Test)

Example 5 with MockDataTreeChangeListener

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

the class DataTreeChangeListenerSupportTest method registerChangeListener.

@SuppressWarnings("checkstyle:IllegalCatch")
private Entry<MockDataTreeChangeListener, ActorSelection> registerChangeListener(final YangInstanceIdentifier path, final int expectedEvents) {
    MockDataTreeChangeListener listener = new MockDataTreeChangeListener(expectedEvents);
    ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH));
    RegisterDataTreeNotificationListenerReply reply;
    try {
        reply = (RegisterDataTreeNotificationListenerReply) Await.result(Patterns.ask(shardActor, new RegisterDataTreeChangeListener(path, dclActor, false), new Timeout(5, TimeUnit.SECONDS)), Duration.create(5, TimeUnit.SECONDS));
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return new SimpleEntry<>(listener, getSystem().actorSelection(reply.getListenerRegistrationPath()));
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Timeout(akka.util.Timeout) SimpleEntry(java.util.AbstractMap.SimpleEntry) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) DataValidationFailedException(org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException)

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