Add OnDemandShardState to report additional Shard state
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerActor.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.Props;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
13 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
14 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
15 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Proxy actor which acts as a facade to the user-provided listener. Responsible for decapsulating
23  * DataTreeChanged messages and dispatching their context to the user.
24  */
25 final class DataTreeChangeListenerActor extends AbstractUntypedActor {
26     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerActor.class);
27     private final DOMDataTreeChangeListener listener;
28     private final YangInstanceIdentifier registeredPath;
29     private boolean notificationsEnabled = false;
30
31     private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
32             final YangInstanceIdentifier registeredPath) {
33         this.listener = Preconditions.checkNotNull(listener);
34         this.registeredPath = Preconditions.checkNotNull(registeredPath);
35     }
36
37     @Override
38     protected void handleReceive(final Object message) {
39         if (message instanceof DataTreeChanged) {
40             dataChanged((DataTreeChanged)message);
41         } else if (message instanceof EnableNotification) {
42             enableNotification((EnableNotification) message);
43         } else {
44             unknownMessage(message);
45         }
46     }
47
48     private void dataChanged(final DataTreeChanged message) {
49         // Do nothing if notifications are not enabled
50         if (!notificationsEnabled) {
51             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
52             return;
53         }
54
55         LOG.debug("Sending change notification {} to listener {}", message.getChanges(), listener);
56
57         try {
58             this.listener.onDataTreeChanged(message.getChanges());
59         } catch (Exception e) {
60             LOG.error("Error notifying listener {}", this.listener, e);
61         }
62
63         // TODO: do we really need this?
64         // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
65         // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
66         if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
67             getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
68         }
69     }
70
71     private void enableNotification(final EnableNotification message) {
72         notificationsEnabled = message.isEnabled();
73         LOG.debug("{} notifications for listener {}", (notificationsEnabled ? "Enabled" : "Disabled"),
74                 listener);
75     }
76
77     public static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
78         return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);
79     }
80 }