872feeac9da203b3f3bf84e76ef1be61382697cc
[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 akka.japi.Creator;
13 import com.google.common.base.Preconditions;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
16 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
17 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
18 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
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 boolean notificationsEnabled = false;
34
35     public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
36         this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
37     }
38
39     @Override
40     public void handleReceive(Object message) {
41         if (message instanceof DataChanged) {
42             dataChanged(message);
43         } else if (message instanceof EnableNotification) {
44             enableNotification((EnableNotification) message);
45         } else {
46             unknownMessage(message);
47         }
48     }
49
50     private void enableNotification(EnableNotification message) {
51         notificationsEnabled = message.isEnabled();
52         LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
53                 listener);
54     }
55
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     private void dataChanged(Object message) {
58
59         // Do nothing if notifications are not enabled
60         if (!notificationsEnabled) {
61             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
62             return;
63         }
64
65         DataChanged reply = (DataChanged) message;
66         AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
67
68         LOG.debug("Sending change notification {} to listener {}", change, listener);
69
70         try {
71             this.listener.onDataChanged(change);
72         } catch (RuntimeException e) {
73             LOG.error(String.format("Error notifying listener %s", this.listener), e);
74         }
75
76         if (isValidSender(getSender())) {
77             getSender().tell(DataChangedReply.INSTANCE, getSelf());
78         }
79     }
80
81     public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier,
82                                                             NormalizedNode<?, ?>> listener) {
83         return Props.create(new DataChangeListenerCreator(listener));
84     }
85
86     private static class DataChangeListenerCreator implements Creator<DataChangeListener> {
87         private static final long serialVersionUID = 1L;
88
89         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but we don't "
90                 + "create remote instances of this actor and thus don't need it to be Serializable.")
91         final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
92
93         DataChangeListenerCreator(
94                 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
95             this.listener = listener;
96         }
97
98         @Override
99         public DataChangeListener create() throws Exception {
100             return new DataChangeListener(listener);
101         }
102     }
103 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.