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