Keep DataChange registrations and notifications local
[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 org.opendaylight.controller.cluster.datastore.messages.DataChanged;
14 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
15 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 public class DataChangeListener extends AbstractUntypedActor {
23     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
24     private final SchemaContext schemaContext;
25     private final YangInstanceIdentifier pathId;
26     private boolean notificationsEnabled = false;
27
28     public DataChangeListener(SchemaContext schemaContext,
29                               AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, YangInstanceIdentifier pathId) {
30         this.listener = listener;
31         this.schemaContext = schemaContext;
32         this.pathId  = pathId;
33     }
34
35     @Override public void handleReceive(Object message) throws Exception {
36         if(message.getClass().equals(DataChanged.SERIALIZABLE_CLASS)){
37             dataChanged(message);
38         } else if(message instanceof EnableNotification){
39             enableNotification((EnableNotification) message);
40         }
41     }
42
43     private void enableNotification(EnableNotification message) {
44         notificationsEnabled = message.isEnabled();
45     }
46
47     public void dataChanged(Object message) {
48
49         // Do nothing if notifications are not enabled
50         if(!notificationsEnabled){
51             return;
52         }
53
54         DataChanged reply = DataChanged.fromSerialize(schemaContext,message, pathId);
55         AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>
56             change = reply.getChange();
57         this.listener.onDataChanged(change);
58
59         if(getSender() != null){
60             getSender().tell(new DataChangedReply().toSerializable(), getSelf());
61         }
62     }
63
64     public static Props props(final SchemaContext schemaContext, final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final YangInstanceIdentifier pathId) {
65         return Props.create(new Creator<DataChangeListener>() {
66             @Override
67             public DataChangeListener create() throws Exception {
68                 return new DataChangeListener(schemaContext,listener,pathId );
69             }
70
71         });
72
73     }
74 }