db5eeb83e70eedd4101cceeabd5491a2c5b3a47e
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerSupport.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.Collection;
14 import java.util.Collections;
15 import java.util.Map.Entry;
16 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
18 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 final class DataTreeChangeListenerSupport extends LeaderLocalDelegateFactory<RegisterDataTreeChangeListener, ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> {
26     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerSupport.class);
27     private final ArrayList<DelayedDataTreeListenerRegistration> delayedRegistrations = new ArrayList<>();
28     private final Collection<ActorSelection> actors = new ArrayList<>();
29
30     DataTreeChangeListenerSupport(final Shard shard) {
31         super(shard);
32     }
33
34     @Override
35     void onLeadershipChange(final boolean isLeader) {
36         if (isLeader) {
37             for (DelayedDataTreeListenerRegistration reg : delayedRegistrations) {
38                 reg.createDelegate(this);
39             }
40             delayedRegistrations.clear();
41             delayedRegistrations.trimToSize();
42         }
43
44         final EnableNotification msg = new EnableNotification(isLeader);
45         for (ActorSelection dataChangeListener : actors) {
46             dataChangeListener.tell(msg, getSelf());
47         }
48     }
49
50     @Override
51     void onMessage(final RegisterDataTreeChangeListener registerTreeChangeListener, final boolean isLeader) {
52         LOG.debug("{}: registerTreeChangeListener for {}, leader: {}", persistenceId(), registerTreeChangeListener.getPath(), isLeader);
53
54         final ListenerRegistration<DOMDataTreeChangeListener> registration;
55         final DataTreeCandidate event;
56         if (!isLeader) {
57             LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
58
59             DelayedDataTreeListenerRegistration delayedReg =
60                     new DelayedDataTreeListenerRegistration(registerTreeChangeListener);
61             delayedRegistrations.add(delayedReg);
62             registration = delayedReg;
63             event = null;
64         } else {
65             final Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> res = createDelegate(registerTreeChangeListener);
66             registration = res.getKey();
67             event = res.getValue();
68         }
69
70         ActorRef listenerRegistration = createActor(DataTreeChangeListenerRegistrationActor.props(registration));
71
72         LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
73             persistenceId(), listenerRegistration.path());
74
75         tellSender(new RegisterDataTreeChangeListenerReply(listenerRegistration));
76         if (event != null) {
77             registration.getInstance().onDataTreeChanged(Collections.singletonList(event));
78         }
79     }
80
81     @Override
82     Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> createDelegate(final RegisterDataTreeChangeListener message) {
83         ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
84
85         // Notify the listener if notifications should be enabled or not
86         // If this shard is the leader then it will enable notifications else
87         // it will not
88         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
89
90         // Now store a reference to the data change listener so it can be notified
91         // at a later point if notifications should be enabled or disabled
92         actors.add(dataChangeListenerPath);
93
94         DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
95
96         LOG.debug("{}: Registering for path {}", persistenceId(), message.getPath());
97
98         return getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
99     }
100 }

©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.