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 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;
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.
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 = "";
32 private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
33 final YangInstanceIdentifier registeredPath) {
34 this.listener = Preconditions.checkNotNull(listener);
35 this.registeredPath = Preconditions.checkNotNull(registeredPath);
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());
48 unknownMessage(message);
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);
61 LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
62 message.getChanges(), listener);
67 this.listener.onDataTreeChanged(message.getChanges());
68 } catch (Exception e) {
69 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
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());
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",
87 public static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
88 return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);