Merge "Fixed for bug : 1171 - issue while creating subnet"
[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.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 public class DataChangeListener extends AbstractUntypedActor {
22     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
23     private final SchemaContext schemaContext;
24     private final YangInstanceIdentifier pathId;
25
26     public DataChangeListener(SchemaContext schemaContext,
27                               AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, YangInstanceIdentifier pathId) {
28         this.listener = listener;
29         this.schemaContext = schemaContext;
30         this.pathId  = pathId;
31     }
32
33     @Override public void handleReceive(Object message) throws Exception {
34         if(message.getClass().equals(DataChanged.SERIALIZABLE_CLASS)){
35             DataChanged reply = DataChanged.fromSerialize(schemaContext,message, pathId);
36             AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>
37                 change = reply.getChange();
38             this.listener.onDataChanged(change);
39
40             if(getSender() != null){
41                 getSender().tell(new DataChangedReply().toSerializable(), getSelf());
42             }
43
44         }
45     }
46
47     public static Props props(final SchemaContext schemaContext, final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final YangInstanceIdentifier pathId) {
48         return Props.create(new Creator<DataChangeListener>() {
49             @Override
50             public DataChangeListener create() throws Exception {
51                 return new DataChangeListener(schemaContext,listener,pathId );
52             }
53
54         });
55
56     }
57 }