Enabling Data Change Notifications for all nodes in cluster.
[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 java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 final class DataChangeListenerSupport extends LeaderLocalDelegateFactory<RegisterChangeListener, ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> {
28     private static final Logger LOG = LoggerFactory.getLogger(DataChangeListenerSupport.class);
29     private final List<DelayedListenerRegistration> delayedListenerRegistrations = new ArrayList<>();
30     private final List<ActorSelection> dataChangeListeners =  new ArrayList<>();
31     private final List<DelayedListenerRegistration> delayedRegisterOnAllListeners = new ArrayList<>();
32
33     DataChangeListenerSupport(final Shard shard) {
34         super(shard);
35     }
36
37     @Override
38     void onLeadershipChange(final boolean isLeader, boolean hasLeader) {
39         LOG.debug("onLeadershipChange, isLeader: {}, hasLeader : {}", isLeader, hasLeader);
40
41         for (ActorSelection dataChangeListener : dataChangeListeners) {
42             dataChangeListener.tell(new EnableNotification(isLeader), getSelf());
43         }
44
45         if(hasLeader) {
46             for (DelayedListenerRegistration reg : delayedRegisterOnAllListeners) {
47                 registerDelayedListeners(reg);
48             }
49             delayedRegisterOnAllListeners.clear();
50         }
51
52         if (isLeader) {
53             for (DelayedListenerRegistration reg: delayedListenerRegistrations) {
54                 registerDelayedListeners(reg);
55             }
56
57             delayedListenerRegistrations.clear();
58         }
59     }
60
61     private void registerDelayedListeners(DelayedListenerRegistration reg) {
62         if(!reg.isClosed()) {
63             final Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> res =
64                 createDelegate(reg.getRegisterChangeListener());
65             reg.setDelegate(res.getKey());
66             if (res.getValue() != null) {
67                 reg.getInstance().onDataChanged(res.getValue());
68             }
69         }
70     }
71
72     @Override
73     void onMessage(final RegisterChangeListener message, final boolean isLeader, boolean hasLeader) {
74
75         LOG.debug("{}: registerDataChangeListener for {}, isLeader: {}, hasLeader : {}",
76             persistenceId(), message.getPath(), isLeader, hasLeader);
77
78         final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
79                                                      NormalizedNode<?, ?>>> registration;
80         final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> event;
81         if ((hasLeader && message.isRegisterOnAllInstances()) || isLeader) {
82             final Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> res =
83                     createDelegate(message);
84             registration = res.getKey();
85             event = res.getValue();
86         } else {
87             LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
88
89             DelayedListenerRegistration delayedReg = new DelayedListenerRegistration(message);
90             if(message.isRegisterOnAllInstances()) {
91                 delayedRegisterOnAllListeners.add(delayedReg);
92             } else {
93                 delayedListenerRegistrations.add(delayedReg);
94             }
95             registration = delayedReg;
96             event = null;
97         }
98
99         ActorRef listenerRegistration = createActor(DataChangeListenerRegistration.props(registration));
100
101         LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
102                 persistenceId(), listenerRegistration.path());
103
104         tellSender(new RegisterChangeListenerReply(listenerRegistration));
105         if (event != null) {
106             registration.getInstance().onDataChanged(event);
107         }
108     }
109
110     @Override
111     Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> createDelegate(
112             final RegisterChangeListener message) {
113         ActorSelection dataChangeListenerPath = selectActor(message.getDataChangeListenerPath());
114
115         // Notify the listener if notifications should be enabled or not
116         // If this shard is the leader then it will enable notifications else
117         // it will not
118         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
119
120         // Now store a reference to the data change listener so it can be notified
121         // at a later point if notifications should be enabled or disabled
122         if(!message.isRegisterOnAllInstances()) {
123             dataChangeListeners.add(dataChangeListenerPath);
124         }
125
126         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
127                 new DataChangeListenerProxy(dataChangeListenerPath);
128
129         LOG.debug("{}: Registering for path {}", persistenceId(), message.getPath());
130
131         return getShard().getDataStore().registerChangeListener(message.getPath(), listener,
132                 message.getScope());
133     }
134 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.