f0c6595ac4cbbb8d2bdfea00503728b09c21d326
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataChangeListener.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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;
24
25 /**
26  * Actor for a DataChangeListener.
27  *
28  * @deprecated Replaced by {@link DataTreeChangeListener}
29  */
30 @Deprecated
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;
36
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);
41     }
42
43     @Override
44     public void handleReceive(Object message) {
45         if (message instanceof DataChanged) {
46             dataChanged(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());
52         } else {
53             unknownMessage(message);
54         }
55     }
56
57     private void enableNotification(EnableNotification message) {
58         notificationsEnabled = message.isEnabled();
59         LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
60                 listener);
61     }
62
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     private void dataChanged(Object message) {
65
66         // Do nothing if notifications are not enabled
67         if (!notificationsEnabled) {
68             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
69             return;
70         }
71
72         DataChanged reply = (DataChanged) message;
73         AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
74
75         LOG.debug("Sending change notification {} to listener {}", change, listener);
76
77         notificationCount++;
78
79         try {
80             this.listener.onDataChanged(change);
81         } catch (RuntimeException e) {
82             LOG.error(String.format("Error notifying listener %s", this.listener), e);
83         }
84
85         if (isValidSender(getSender())) {
86             getSender().tell(DataChangedReply.INSTANCE, getSelf());
87         }
88     }
89
90     public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
91             final YangInstanceIdentifier registeredPath) {
92         return Props.create(DataChangeListener.class, listener, registeredPath);
93     }
94 }