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.Props;
13 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
14 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
15 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
16 import org.opendaylight.controller.cluster.datastore.messages.DataTreeListenerInfo;
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.mdsal.dom.api.DOMDataTreeChangeListener;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 * Proxy actor which acts as a facade to the user-provided listener. Responsible for decapsulating
25 * DataTreeChanged messages and dispatching their context to the user.
27 class DataTreeChangeListenerActor extends AbstractUntypedActor {
28 private final DOMDataTreeChangeListener listener;
29 private final YangInstanceIdentifier registeredPath;
31 private boolean notificationsEnabled = false;
32 private long notificationCount;
33 private String logContext = "";
35 DataTreeChangeListenerActor(final DOMDataTreeChangeListener listener,
36 final YangInstanceIdentifier registeredPath) {
37 this.listener = requireNonNull(listener);
38 this.registeredPath = requireNonNull(registeredPath);
42 protected final void handleReceive(final Object message) {
43 if (message instanceof DataTreeChanged) {
44 dataTreeChanged((DataTreeChanged) message);
45 } else if (message instanceof OnInitialData) {
46 onInitialData((OnInitialData) message);
47 } else if (message instanceof EnableNotification) {
48 enableNotification((EnableNotification) message);
49 } else if (message instanceof GetInfo) {
50 getSender().tell(new DataTreeListenerInfo(listener.toString(), registeredPath.toString(),
51 notificationsEnabled, notificationCount), getSelf());
53 unknownMessage(message);
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 void onInitialData(final OnInitialData message) {
59 LOG.debug("{}: Notifying onInitialData to listener {}", logContext, listener);
62 this.listener.onInitialData();
63 } catch (Exception e) {
64 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
68 @SuppressWarnings("checkstyle:IllegalCatch")
69 void dataTreeChanged(final DataTreeChanged message) {
70 // Do nothing if notifications are not enabled
71 if (!notificationsEnabled) {
72 LOG.debug("{}: Notifications not enabled for listener {} - dropping change notification",
73 logContext, listener);
77 LOG.debug("{}: Sending {} change notification(s) {} to listener {}", logContext, message.getChanges().size(),
78 message.getChanges(), listener);
83 this.listener.onDataTreeChanged(message.getChanges());
84 } catch (Exception e) {
85 LOG.error("{}: Error notifying listener {}", logContext, this.listener, e);
88 // TODO: do we really need this?
89 // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
90 // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
91 if (getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
92 getSender().tell(DataTreeChangedReply.getInstance(), getSelf());
96 private void enableNotification(final EnableNotification message) {
97 logContext = message.getLogContext();
98 notificationsEnabled = message.isEnabled();
99 LOG.debug("{}: {} notifications for listener {}", logContext, notificationsEnabled ? "Enabled" : "Disabled",
103 static Props props(final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath) {
104 return Props.create(DataTreeChangeListenerActor.class, listener, registeredPath);