Bug 8231: Fix testChangeListenerRegistration failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import org.opendaylight.controller.cluster.datastore.actors.DataTreeNotificationListenerRegistrationActor;
13 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
14 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 final class DataChangeListenerSupport extends AbstractDataListenerSupport<
20         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>, RegisterChangeListener,
21             DelayedDataChangeListenerRegistration> {
22
23     DataChangeListenerSupport(final Shard shard) {
24         super(shard);
25     }
26
27     @Override
28     void doRegistration(final RegisterChangeListener message, final ActorRef registrationActor) {
29         final ActorSelection listenerActor = processListenerRegistrationMessage(message);
30
31         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
32                 new DataChangeListenerProxy(listenerActor);
33
34         log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
35
36         final ShardDataTree shardDataTree = getShard().getDataStore();
37         shardDataTree.registerDataChangeListener(message.getPath(), listener, message.getScope(),
38                 shardDataTree.readCurrentData(), registration -> registrationActor.tell(
39                         new DataTreeNotificationListenerRegistrationActor.SetRegistration(registration, () ->
40                             removeListenerActor(listenerActor)), ActorRef.noSender()));
41     }
42
43     @Override
44     protected DelayedDataChangeListenerRegistration newDelayedListenerRegistration(RegisterChangeListener message,
45             ActorRef registrationActor) {
46         return new DelayedDataChangeListenerRegistration(message, registrationActor);
47     }
48
49     @Override
50     protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
51         return new RegisterChangeListenerReply(registrationActor);
52     }
53
54     @Override
55     protected String logName() {
56         return "registerDataChangeListener";
57     }
58 }