Raise EOS unsuccessful request reporting to error
[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.DataTreeListenerInfo;
16 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
17 import org.opendaylight.controller.cluster.datastore.messages.GetInfo;
18 import org.opendaylight.controller.cluster.datastore.messages.OnInitialData;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21
22 /**
23  * Proxy actor which acts as a facade to the user-provided listener. Responsible for decapsulating
24  * DataTreeChanged messages and dispatching their context to the user.
25  */
26 final class DataTreeChangeListenerActor extends AbstractUntypedActor {
27     private final DOMDataTreeChangeListener listener;
28     private final YangInstanceIdentifier registeredPath;
29     private boolean notificationsEnabled = false;
30     private long notificationCount;
31     private String logContext = "";
32
33     private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
34             final YangInstanceIdentifier registeredPath) {
35         this.listener = Preconditions.checkNotNull(listener);
36         this.registeredPath = Preconditions.checkNotNull(registeredPath);
37     }
38
39     @Override
40     protected void handleReceive(final Object message) {
41         if (message instanceof DataTreeChanged) {
42             dataChanged((DataTreeChanged)message);
43         } else if (message instanceof OnInitialData) {
44             onInitialData();
45         } else if (message instanceof EnableNotification) {
46             enableNotification((EnableNotification) message);
47         } else if (message instanceof GetInfo) {
48             getSender().tell(new DataTreeListenerInfo(listener.toString(), registeredPath.toString(),
49                     notificationsEnabled, notificationCount), getSelf());
50         } else {
51             unknownMessage(message);
52         }
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     private void onInitialData() {
57         LOG.debug("{}: Notifying onInitialData to listener {}", logContext, listener);
58
59         try {
60             this.listener.onInitialData();
61         } catch (Exception e) {
62             LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
63         }
64     }
65
66     @SuppressWarnings("checkstyle:IllegalCatch")
67     private void dataChanged(final DataTreeChanged message) {
68         // Do nothing if notifications are not enabled
69         if (!notificationsEnabled) {
70             LOG.debug("{}: Notifications not enabled for listener {} - dropping change notification",
71                     logContext, listener);
72             return;
73         }
74
75         LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
76                 message.getChanges(), listener);
77
78         notificationCount++;
79
80         try {
81             this.listener.onDataTreeChanged(message.getChanges());
82         } catch (Exception e) {
83             LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
84         }
85
86         // TODO: do we really need this?
87         // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
88         // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
89         if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
90             getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
91         }
92     }
93
94     private void enableNotification(final EnableNotification message) {
95         logContext = message.getLogContext();
96         notificationsEnabled = message.isEnabled();
97         LOG.debug("{}: {} notifications for listener {}", logContext, notificationsEnabled ? "Enabled" : "Disabled",
98                 listener);
99     }
100
101     public static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
102         return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);
103     }
104 }