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