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 akka.japi.Creator;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
15 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
16 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
17 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
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;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * @Deprecated Replaced by {@link DataTreeChangeListener}
29 public class DataChangeListener extends AbstractUntypedActor {
30 private static final Logger LOG = LoggerFactory.getLogger(DataChangeListener.class);
32 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
33 private boolean notificationsEnabled = false;
35 public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier,
36 NormalizedNode<?, ?>> listener) {
37 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
41 public void handleReceive(Object message) throws Exception {
42 if(message instanceof DataChanged){
44 } else if(message instanceof EnableNotification){
45 enableNotification((EnableNotification) message);
49 private void enableNotification(EnableNotification message) {
50 notificationsEnabled = message.isEnabled();
51 LOG.debug("{} notifications for listener {}", (notificationsEnabled ? "Enabled" : "Disabled"),
55 private void dataChanged(Object message) {
57 // Do nothing if notifications are not enabled
58 if(!notificationsEnabled) {
59 LOG.debug("Notifications not enabled for listener {} - dropping change notification",
64 DataChanged reply = (DataChanged) message;
65 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
67 LOG.debug("Sending change notification {} to listener {}", change, listener);
70 this.listener.onDataChanged(change);
71 } catch (RuntimeException e) {
72 LOG.error( String.format( "Error notifying listener %s", this.listener ), e );
75 // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
76 // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
77 if(getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
78 getSender().tell(DataChangedReply.INSTANCE, getSelf());
82 public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier,
83 NormalizedNode<?, ?>> listener) {
84 return Props.create(new DataChangeListenerCreator(listener));
87 private static class DataChangeListenerCreator implements Creator<DataChangeListener> {
88 private static final long serialVersionUID = 1L;
90 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
92 DataChangeListenerCreator(
93 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
94 this.listener = listener;
98 public DataChangeListener create() throws Exception {
99 return new DataChangeListener(listener);