2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.INNER_LIST_QNAME;
11 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_PATH;
12 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_QNAME;
13 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
14 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_QNAME;
15 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerEntryPath;
16 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
17 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryKey;
18 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryPath;
19 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.testNodeWithOuter;
23 import akka.actor.ActorRef;
24 import akka.actor.ActorSelection;
25 import akka.pattern.Patterns;
26 import akka.testkit.JavaTestKit;
27 import akka.testkit.TestActorRef;
28 import akka.util.Timeout;
29 import com.google.common.base.Throwables;
30 import java.util.AbstractMap.SimpleEntry;
31 import java.util.Map.Entry;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
37 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
39 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
40 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import scala.concurrent.Await;
45 import scala.concurrent.duration.Duration;
48 * Unit tests for DataTreeChangeListenerSupport.
50 * @author Thomas Pantelis
52 public class DataTreeChangeListenerSupportTest extends AbstractShardTest {
54 private TestActorRef<Shard> shardActor;
65 public void tearDown() {
71 public void testChangeListenerWithNoInitialData() throws Exception {
72 MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 0).getKey();
74 listener.expectNoMoreChanges("Unexpected initial change event");
78 public void testInitialChangeListenerEventWithContainerPath() throws Exception {
79 writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
81 Entry<MockDataTreeChangeListener, ActorSelection> entry = registerChangeListener(TEST_PATH, 1);
82 MockDataTreeChangeListener listener = entry.getKey();
84 listener.waitForChangeEvents();
85 listener.verifyNotifiedData(TEST_PATH);
89 writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
90 listener.waitForChangeEvents();
91 listener.verifyNotifiedData(TEST_PATH);
94 JavaTestKit kit = new JavaTestKit(getSystem());
95 entry.getValue().tell(CloseDataTreeNotificationListenerRegistration.getInstance(), kit.getRef());
96 kit.expectMsgClass(JavaTestKit.duration("5 seconds"), CloseDataTreeNotificationListenerRegistrationReply.class);
98 writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
99 listener.verifyNoNotifiedData(TEST_PATH);
103 public void testInitialChangeListenerEventWithListPath() throws Exception {
104 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
106 MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH, 1).getKey();
108 listener.waitForChangeEvents();
109 listener.verifyNotifiedData(OUTER_LIST_PATH);
113 public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
114 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
116 MockDataTreeChangeListener listener =
117 registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 1).getKey();
119 listener.waitForChangeEvents();
120 listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
124 public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
125 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
126 outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
128 MockDataTreeChangeListener listener = registerChangeListener(
129 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
131 listener.waitForChangeEvents();
132 listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"),
133 innerEntryPath(2, "four"));
135 // Register for a specific outer list entry
137 MockDataTreeChangeListener listener2 = registerChangeListener(
138 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
140 listener2.waitForChangeEvents();
141 listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
142 listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
147 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
148 outerNodeEntry(1, innerNode("three")))));
150 listener.waitForChangeEvents();
151 listener.verifyNotifiedData(innerEntryPath(1, "three"));
153 listener2.waitForChangeEvents();
154 listener2.verifyNotifiedData(innerEntryPath(1, "three"));
157 @SuppressWarnings("checkstyle:IllegalCatch")
158 private Entry<MockDataTreeChangeListener, ActorSelection> registerChangeListener(final YangInstanceIdentifier path,
159 final int expectedEvents) {
160 MockDataTreeChangeListener listener = new MockDataTreeChangeListener(expectedEvents);
161 ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH));
164 RegisterDataTreeChangeListenerReply reply = (RegisterDataTreeChangeListenerReply)
165 Await.result(Patterns.ask(shardActor, new RegisterDataTreeChangeListener(path, dclActor, false),
166 new Timeout(5, TimeUnit.SECONDS)), Duration.create(5, TimeUnit.SECONDS));
167 return new SimpleEntry<>(listener, getSystem().actorSelection(reply.getListenerRegistrationPath()));
169 } catch (Exception e) {
170 Throwables.propagate(e);
175 private void createShard() {
176 shardActor = actorFactory.createTestActor(newShardProps());
177 ShardTestKit.waitUntilLeader(shardActor);
178 shard = shardActor.underlyingActor();