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.controller.cluster.datastore.messages.OnInitialData;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 * Proxy actor which acts as a facade to the user-provided listener. Responsible for decapsulating
24 * DataTreeChanged messages and dispatching their context to the user.
26 final class DataTreeChangeListenerActor extends AbstractUntypedActor {
27 private final DOMDataTreeChangeListener listener;
28 private final YangInstanceIdentifier registeredPath;
29 private boolean notificationsEnabled = false;
30 private long notificationCount;
31 private String logContext = "";
33 private DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
34 final YangInstanceIdentifier registeredPath) {
35 this.listener = Preconditions.checkNotNull(listener);
36 this.registeredPath = Preconditions.checkNotNull(registeredPath);
40 protected void handleReceive(final Object message) {
41 if (message instanceof DataTreeChanged) {
42 dataChanged((DataTreeChanged)message);
43 } else if (message instanceof OnInitialData) {
45 } else if (message instanceof EnableNotification) {
46 enableNotification((EnableNotification) message);
47 } else if (message instanceof GetInfo) {
48 getSender().tell(new DataTreeListenerInfo(listener.toString(), registeredPath.toString(),
49 notificationsEnabled, notificationCount), getSelf());
51 unknownMessage(message);
55 @SuppressWarnings("checkstyle:IllegalCatch")
56 private void onInitialData() {
57 LOG.debug("{}: Notifying onInitialData to listener {}", logContext, listener);
60 this.listener.onInitialData();
61 } catch (Exception e) {
62 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
66 @SuppressWarnings("checkstyle:IllegalCatch")
67 private void dataChanged(final DataTreeChanged message) {
68 // Do nothing if notifications are not enabled
69 if (!notificationsEnabled) {
70 LOG.debug("{}: Notifications not enabled for listener {} - dropping change notification",
71 logContext, listener);
75 LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
76 message.getChanges(), listener);
81 this.listener.onDataTreeChanged(message.getChanges());
82 } catch (Exception e) {
83 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
86 // TODO: do we really need this?
87 // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
88 // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
89 if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
90 getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
94 private void enableNotification(final EnableNotification message) {
95 logContext = message.getLogContext();
96 notificationsEnabled = message.isEnabled();
97 LOG.debug("{}: {} notifications for listener {}", logContext, notificationsEnabled ? "Enabled" : "Disabled",
101 public static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
102 return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);