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.EnableNotification;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.
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 = "";
29 private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
30 final YangInstanceIdentifier registeredPath) {
31 this.listener = Preconditions.checkNotNull(listener);
32 this.registeredPath = Preconditions.checkNotNull(registeredPath);
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);
42 unknownMessage(message);
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);
55 LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
56 message.getChanges(), listener);
59 this.listener.onDataTreeChanged(message.getChanges());
60 } catch (Exception e) {
61 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
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());
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",
79 public static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
80 return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);