2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static java.util.Objects.requireNonNull;
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;
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.
28 class DataTreeChangeListenerActor extends AbstractUntypedActor {
29 private final DOMDataTreeChangeListener listener;
30 private final YangInstanceIdentifier registeredPath;
32 private boolean notificationsEnabled = false;
33 private long notificationCount;
34 private String logContext = "";
36 DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
37 final YangInstanceIdentifier registeredPath) {
38 this.listener = requireNonNull(listener);
39 this.registeredPath = requireNonNull(registeredPath);
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());
54 unknownMessage(message);
58 @SuppressWarnings("checkstyle:IllegalCatch")
59 void onInitialData(final OnInitialData message) {
60 LOG.debug("{}: Notifying onInitialData to listener {}", logContext, listener);
63 this.listener.onInitialData();
64 } catch (Exception e) {
65 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
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);
78 LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
79 message.getChanges(), listener);
84 this.listener.onDataTreeChanged(message.getChanges());
85 } catch (Exception e) {
86 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
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());
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",
105 static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
106 return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);