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