Add OnDemandShardState to report additional Shard state
[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 com.google.common.base.Optional;
13 import com.google.common.collect.Sets;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24
25 final class DataTreeChangeListenerSupport extends AbstractDataListenerSupport<DOMDataTreeChangeListener,
26         RegisterDataTreeChangeListener, DelayedDataTreeListenerRegistration,
27         ListenerRegistration<DOMDataTreeChangeListener>> {
28
29     private final Set<ActorSelection> listenerActors = Sets.newConcurrentHashSet();
30
31     DataTreeChangeListenerSupport(final Shard shard) {
32         super(shard);
33     }
34
35     Collection<ActorSelection> getListenerActors() {
36         return Collections.unmodifiableCollection(listenerActors);
37     }
38
39     @Override
40     ListenerRegistration<DOMDataTreeChangeListener> createDelegate(
41             final RegisterDataTreeChangeListener message) {
42         final ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
43
44         // Notify the listener if notifications should be enabled or not
45         // If this shard is the leader then it will enable notifications else
46         // it will not
47         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
48
49         // Now store a reference to the data change listener so it can be notified
50         // at a later point if notifications should be enabled or disabled
51         if (!message.isRegisterOnAllInstances()) {
52             addListenerActor(dataChangeListenerPath);
53         }
54
55         DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
56
57         log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
58
59         Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
60                 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
61
62         getShard().getDataStore().notifyOfInitialData(message.getPath(),
63                 regEntry.getKey().getInstance(), regEntry.getValue());
64
65         listenerActors.add(dataChangeListenerPath);
66         final ListenerRegistration<DOMDataTreeChangeListener> delegate = regEntry.getKey();
67         return new ListenerRegistration<DOMDataTreeChangeListener>() {
68             @Override
69             public DOMDataTreeChangeListener getInstance() {
70                 return delegate.getInstance();
71             }
72
73             @Override
74             public void close() {
75                 listenerActors.remove(dataChangeListenerPath);
76                 delegate.close();
77             }
78         };
79     }
80
81     @Override
82     protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(
83             RegisterDataTreeChangeListener message) {
84         return new DelayedDataTreeListenerRegistration(message);
85     }
86
87     @Override
88     protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
89         return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
90     }
91
92     @Override
93     protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
94         return new RegisterDataTreeChangeListenerReply(registrationActor);
95     }
96
97     @Override
98     protected String logName() {
99         return "registerTreeChangeListener";
100     }
101 }

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