26d8fa1b646af75e2775eea23a72d64ae75e28c8
[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 com.google.common.base.Optional;
13 import java.util.Map.Entry;
14 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
15 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23
24 final class DataChangeListenerSupport extends AbstractDataListenerSupport<
25         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>, RegisterChangeListener,
26             DelayedDataChangeListenerRegistration, DataChangeListenerRegistration<
27                     AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>> {
28
29     DataChangeListenerSupport(final Shard shard) {
30         super(shard);
31     }
32
33     @Override
34     Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
35             Optional<DataTreeCandidate>> createDelegate(final RegisterChangeListener message) {
36         ActorSelection dataChangeListenerPath = selectActor(message.getDataChangeListenerPath());
37
38         // Notify the listener if notifications should be enabled or not
39         // If this shard is the leader then it will enable notifications else
40         // it will not
41         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
42
43         // Now store a reference to the data change listener so it can be notified
44         // at a later point if notifications should be enabled or disabled
45         if (!message.isRegisterOnAllInstances()) {
46             addListenerActor(dataChangeListenerPath);
47         }
48
49         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
50                 new DataChangeListenerProxy(dataChangeListenerPath);
51
52         log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
53
54         Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
55                 Optional<DataTreeCandidate>> regEntry = getShard().getDataStore().registerChangeListener(
56                         message.getPath(), listener, message.getScope());
57
58         getShard().getDataStore().notifyOfInitialData(regEntry.getKey(), regEntry.getValue());
59
60         return regEntry;
61     }
62
63     @Override
64     protected DelayedDataChangeListenerRegistration newDelayedListenerRegistration(RegisterChangeListener message) {
65         return new DelayedDataChangeListenerRegistration(message);
66     }
67
68     @Override
69     protected ActorRef newRegistrationActor(
70             ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> registration) {
71         return createActor(DataChangeListenerRegistrationActor.props(registration));
72     }
73
74     @Override
75     protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
76         return new RegisterChangeListenerReply(registrationActor);
77     }
78
79     @Override
80     protected String logName() {
81         return "registerDataChangeListener";
82     }
83 }