2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.Props;
12 import akka.japi.Creator;
14 import com.google.common.base.Preconditions;
15 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
17 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
18 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
19 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
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;
25 public class DataChangeListener extends AbstractUntypedActor {
26 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
27 private volatile boolean notificationsEnabled = false;
29 public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier,
30 NormalizedNode<?, ?>> listener) {
31 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
34 @Override public void handleReceive(Object message) throws Exception {
35 if(message instanceof DataChanged){
37 } else if(message instanceof EnableNotification){
38 enableNotification((EnableNotification) message);
42 private void enableNotification(EnableNotification message) {
43 notificationsEnabled = message.isEnabled();
46 private void dataChanged(Object message) {
48 // Do nothing if notifications are not enabled
49 if(!notificationsEnabled){
53 DataChanged reply = (DataChanged) message;
54 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>
55 change = reply.getChange();
56 this.listener.onDataChanged(change);
58 if(getSender() != null){
59 getSender().tell(new DataChangedReply(), getSelf());
63 public static Props props(final AsyncDataChangeListener<YangInstanceIdentifier,
64 NormalizedNode<?, ?>> listener) {
65 return Props.create(new DataChangeListenerCreator(listener));
68 private static class DataChangeListenerCreator implements Creator<DataChangeListener> {
69 private static final long serialVersionUID = 1L;
71 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
73 DataChangeListenerCreator(
74 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
75 this.listener = listener;
79 public DataChangeListener create() throws Exception {
80 return new DataChangeListener(listener);