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.EnableNotification;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 * Actor for a DataChangeListener.
26 * @deprecated Replaced by {@link DataTreeChangeListener}
29 public class DataChangeListener extends AbstractUntypedActor {
30 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
31 private final YangInstanceIdentifier registeredPath;
32 private boolean notificationsEnabled = false;
34 public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
35 final YangInstanceIdentifier registeredPath) {
36 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
37 this.registeredPath = Preconditions.checkNotNull(registeredPath);
41 public void handleReceive(Object message) {
42 if (message instanceof DataChanged) {
44 } else if (message instanceof EnableNotification) {
45 enableNotification((EnableNotification) message);
47 unknownMessage(message);
51 private void enableNotification(EnableNotification message) {
52 notificationsEnabled = message.isEnabled();
53 LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 private void dataChanged(Object message) {
60 // Do nothing if notifications are not enabled
61 if (!notificationsEnabled) {
62 LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
66 DataChanged reply = (DataChanged) message;
67 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
69 LOG.debug("Sending change notification {} to listener {}", change, listener);
72 this.listener.onDataChanged(change);
73 } catch (RuntimeException e) {
74 LOG.error(String.format("Error notifying listener %s", this.listener), e);
77 if (isValidSender(getSender())) {
78 getSender().tell(DataChangedReply.INSTANCE, getSelf());
82 public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
83 final YangInstanceIdentifier registeredPath) {
84 return Props.create(DataChangeListener.class, listener, registeredPath);