Fix warnings/javadocs in sal-distributed-datastore
[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
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 boolean notificationsEnabled = false;
26
27     private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener) {
28         this.listener = Preconditions.checkNotNull(listener);
29     }
30
31     @Override
32     protected void handleReceive(final Object message) {
33         if (message instanceof DataTreeChanged) {
34             dataChanged((DataTreeChanged)message);
35         } else if (message instanceof EnableNotification) {
36             enableNotification((EnableNotification) message);
37         } else {
38             unknownMessage(message);
39         }
40     }
41
42     @SuppressWarnings("checkstyle:IllegalCatch")
43     private void dataChanged(final DataTreeChanged message) {
44         // Do nothing if notifications are not enabled
45         if (!notificationsEnabled) {
46             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
47             return;
48         }
49
50         LOG.debug("Sending change notification {} to listener {}", message.getChanges(), listener);
51
52         try {
53             this.listener.onDataTreeChanged(message.getChanges());
54         } catch (Exception e) {
55             LOG.error("Error notifying listener {}", this.listener, e);
56         }
57
58         // TODO: do we really need this?
59         // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
60         // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
61         if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
62             getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
63         }
64     }
65
66     private void enableNotification(final EnableNotification message) {
67         notificationsEnabled = message.isEnabled();
68         LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
69                 listener);
70     }
71
72     public static Props props(final DOMDataTreeChangeListener listener) {
73         return Props.create(new DataTreeChangeListenerCreator(listener));
74     }
75
76     private static final class DataTreeChangeListenerCreator implements Creator<DataTreeChangeListenerActor> {
77         private static final long serialVersionUID = 1L;
78         private final DOMDataTreeChangeListener listener;
79
80         DataTreeChangeListenerCreator(final DOMDataTreeChangeListener listener) {
81             this.listener = Preconditions.checkNotNull(listener);
82         }
83
84         @Override
85         public DataTreeChangeListenerActor create() {
86             return new DataTreeChangeListenerActor(listener);
87         }
88     }
89 }

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