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