Merge "Remove sonar.profile since it's deprecated in Sonar 4.5"
[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 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;
24
25 public class DataChangeListener extends AbstractUntypedActor {
26     private static final Logger LOG = LoggerFactory.getLogger(DataChangeListener.class);
27
28     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
29     private boolean notificationsEnabled = false;
30
31     public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier,
32                                                       NormalizedNode<?, ?>> listener) {
33         this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
34     }
35
36     @Override
37     public void handleReceive(Object message) throws Exception {
38         if(message instanceof DataChanged){
39             dataChanged(message);
40         } else if(message instanceof EnableNotification){
41             enableNotification((EnableNotification) message);
42         }
43     }
44
45     private void enableNotification(EnableNotification message) {
46         notificationsEnabled = message.isEnabled();
47         LOG.debug("{} notifications for listener {}", (notificationsEnabled ? "Enabled" : "Disabled"),
48                 listener);
49     }
50
51     private void dataChanged(Object message) {
52
53         // Do nothing if notifications are not enabled
54         if(!notificationsEnabled) {
55             LOG.debug("Notifications not enabled for listener {} - dropping change notification",
56                     listener);
57             return;
58         }
59
60         DataChanged reply = (DataChanged) message;
61         AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
62
63         LOG.debug("Sending change notification {} to listener {}", change, listener);
64
65         this.listener.onDataChanged(change);
66
67         // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
68         // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
69         if(getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
70             getSender().tell(new DataChangedReply(), getSelf());
71         }
72     }
73
74     public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier,
75                                                             NormalizedNode<?, ?>> listener) {
76         return Props.create(new DataChangeListenerCreator(listener));
77     }
78
79     private static class DataChangeListenerCreator implements Creator<DataChangeListener> {
80         private static final long serialVersionUID = 1L;
81
82         final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
83
84         DataChangeListenerCreator(
85                 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
86             this.listener = listener;
87         }
88
89         @Override
90         public DataChangeListener create() throws Exception {
91             return new DataChangeListener(listener);
92         }
93     }
94 }

©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.