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