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

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