2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.Props;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
14 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
15 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
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.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 * Actor for a DataChangeListener.
28 * @deprecated Replaced by {@link DataTreeChangeListener}
31 public class DataChangeListener extends AbstractUntypedActor {
32 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
33 private final YangInstanceIdentifier registeredPath;
34 private boolean notificationsEnabled = false;
35 private long notificationCount;
37 public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
38 final YangInstanceIdentifier registeredPath) {
39 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
40 this.registeredPath = Preconditions.checkNotNull(registeredPath);
44 public void handleReceive(Object message) {
45 if (message instanceof DataChanged) {
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 private void enableNotification(EnableNotification message) {
58 notificationsEnabled = message.isEnabled();
59 LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
63 @SuppressWarnings("checkstyle:IllegalCatch")
64 private void dataChanged(Object message) {
66 // Do nothing if notifications are not enabled
67 if (!notificationsEnabled) {
68 LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
72 DataChanged reply = (DataChanged) message;
73 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
75 LOG.debug("Sending change notification {} to listener {}", change, listener);
80 this.listener.onDataChanged(change);
81 } catch (RuntimeException e) {
82 LOG.error(String.format("Error notifying listener %s", this.listener), e);
85 if (isValidSender(getSender())) {
86 getSender().tell(DataChangedReply.INSTANCE, getSelf());
90 public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
91 final YangInstanceIdentifier registeredPath) {
92 return Props.create(DataChangeListener.class, listener, registeredPath);