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.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.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 public class DataChangeListener extends AbstractUntypedActor {
24 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
25 private final SchemaContext schemaContext;
26 private final YangInstanceIdentifier pathId;
27 private boolean notificationsEnabled = false;
29 public DataChangeListener(SchemaContext schemaContext,
30 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, YangInstanceIdentifier pathId) {
32 this.schemaContext = Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
33 this.listener = Preconditions.checkNotNull(listener, "listener should not be null");
34 this.pathId = Preconditions.checkNotNull(pathId, "pathId should not be null");
37 @Override public void handleReceive(Object message) throws Exception {
38 if(message instanceof DataChanged){
40 } else if(message instanceof EnableNotification){
41 enableNotification((EnableNotification) message);
45 private void enableNotification(EnableNotification message) {
46 notificationsEnabled = message.isEnabled();
49 private void dataChanged(Object message) {
51 // Do nothing if notifications are not enabled
52 if(!notificationsEnabled){
56 DataChanged reply = (DataChanged) message;
57 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>
58 change = reply.getChange();
59 this.listener.onDataChanged(change);
61 if(getSender() != null){
62 getSender().tell(new DataChangedReply(), getSelf());
66 public static Props props(final SchemaContext schemaContext, final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final YangInstanceIdentifier pathId) {
67 return Props.create(new Creator<DataChangeListener>() {
69 public DataChangeListener create() throws Exception {
70 return new DataChangeListener(schemaContext,listener,pathId );