Fix warnings/javadocs in sal-distributed-datastore
[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.binding.api.DataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 /**
25  * Actor for a DataChangeListener.
26  *
27  * @Deprecated Replaced by {@link DataTreeChangeListener}
28  */
29 @Deprecated
30 public class DataChangeListener extends AbstractUntypedActor {
31     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
32     private boolean notificationsEnabled = false;
33
34     public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
35         this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
36     }
37
38     @Override
39     public void handleReceive(Object message) {
40         if (message instanceof DataChanged) {
41             dataChanged(message);
42         } else if (message instanceof EnableNotification) {
43             enableNotification((EnableNotification) message);
44         } else {
45             unknownMessage(message);
46         }
47     }
48
49     private void enableNotification(EnableNotification message) {
50         notificationsEnabled = message.isEnabled();
51         LOG.debug("{} notifications for listener {}", notificationsEnabled ? "Enabled" : "Disabled",
52                 listener);
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     private void dataChanged(Object message) {
57
58         // Do nothing if notifications are not enabled
59         if (!notificationsEnabled) {
60             LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
61             return;
62         }
63
64         DataChanged reply = (DataChanged) message;
65         AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
66
67         LOG.debug("Sending change notification {} to listener {}", change, listener);
68
69         try {
70             this.listener.onDataChanged(change);
71         } catch (RuntimeException e) {
72             LOG.error( String.format( "Error notifying listener %s", this.listener ), e );
73         }
74
75         if (isValidSender(getSender())) {
76             getSender().tell(DataChangedReply.INSTANCE, getSelf());
77         }
78     }
79
80     public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier,
81                                                             NormalizedNode<?, ?>> listener) {
82         return Props.create(new DataChangeListenerCreator(listener));
83     }
84
85     private static class DataChangeListenerCreator implements Creator<DataChangeListener> {
86         private static final long serialVersionUID = 1L;
87
88         final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
89
90         DataChangeListenerCreator(
91                 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
92             this.listener = listener;
93         }
94
95         @Override
96         public DataChangeListener create() throws Exception {
97             return new DataChangeListener(listener);
98         }
99     }
100 }

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