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