03978f2b66387bcd483b05bcc58c4afb9ac7db8b
[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 akka.japi.Creator;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
14 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
15 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
16 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
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 boolean notificationsEnabled = false;
29
30     private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener) {
31         this.listener = Preconditions.checkNotNull(listener);
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     private void dataChanged(final DataTreeChanged message) {
46         // Do nothing if notifications are not enabled
47         if (!notificationsEnabled) {
48             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
49             return;
50         }
51
52         LOG.debug("Sending change notification {} to listener {}", message.getChanges(), listener);
53
54         try {
55             this.listener.onDataTreeChanged(message.getChanges());
56         } catch (Exception e) {
57             LOG.error("Error notifying listener {}", this.listener, e);
58         }
59
60         // TODO: do we really need this?
61         // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
62         // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
63         if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
64             getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
65         }
66     }
67
68     private void enableNotification(final EnableNotification message) {
69         notificationsEnabled = message.isEnabled();
70         LOG.debug("{} notifications for listener {}", (notificationsEnabled ? "Enabled" : "Disabled"),
71                 listener);
72     }
73
74     public static Props props(final DOMDataTreeChangeListener listener) {
75         return Props.create(new DataTreeChangeListenerCreator(listener));
76     }
77
78     private static final class DataTreeChangeListenerCreator implements Creator<DataTreeChangeListenerActor> {
79         private static final long serialVersionUID = 1L;
80         private final DOMDataTreeChangeListener listener;
81
82         DataTreeChangeListenerCreator(final DOMDataTreeChangeListener listener) {
83             this.listener = Preconditions.checkNotNull(listener);
84         }
85
86         @Override
87         public DataTreeChangeListenerActor create() {
88             return new DataTreeChangeListenerActor(listener);
89         }
90     }
91 }

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