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;
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;
23 public class DataChangeListener extends AbstractUntypedActor {
24 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
25 private boolean notificationsEnabled = false;
27 public DataChangeListener(AsyncDataChangeListener<YangInstanceIdentifier,
28 NormalizedNode<?, ?>> listener) {
29 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
32 @Override public void handleReceive(Object message) throws Exception {
33 if(message instanceof DataChanged){
35 } else if(message instanceof EnableNotification){
36 enableNotification((EnableNotification) message);
40 private void enableNotification(EnableNotification message) {
41 notificationsEnabled = message.isEnabled();
44 private void dataChanged(Object message) {
46 // Do nothing if notifications are not enabled
47 if(!notificationsEnabled){
51 DataChanged reply = (DataChanged) message;
52 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>
53 change = reply.getChange();
54 this.listener.onDataChanged(change);
56 // It seems the sender is never null but it doesn't hurt to check. If the caller passes in
57 // a null sender (ActorRef.noSender()), akka translates that to the deadLetters actor.
58 if(getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
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);