Serialization/Deserialization and a host of other fixes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistrationProxy.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.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.PoisonPill;
14 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 /**
21  * ListenerRegistrationProxy acts as a proxy for a ListenerRegistration that was done on a remote shard
22  * <p>
23  * Registering a DataChangeListener on the Data Store creates a new instance of the ListenerRegistrationProxy
24  * The ListenerRegistrationProxy talks to a remote ListenerRegistration actor.
25  * </p>
26  */
27 public class DataChangeListenerRegistrationProxy implements ListenerRegistration {
28     private final ActorSelection listenerRegistrationActor;
29     private final AsyncDataChangeListener listener;
30     private final ActorRef dataChangeListenerActor;
31
32     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
33     DataChangeListenerRegistrationProxy(
34         ActorSelection listenerRegistrationActor,
35         L listener, ActorRef dataChangeListenerActor) {
36         this.listenerRegistrationActor = listenerRegistrationActor;
37         this.listener = listener;
38         this.dataChangeListenerActor = dataChangeListenerActor;
39     }
40
41     @Override
42     public Object getInstance() {
43         return listener;
44     }
45
46     @Override
47     public void close() {
48         listenerRegistrationActor.tell(new CloseDataChangeListenerRegistration().toSerializable(), null);
49         dataChangeListenerActor.tell(PoisonPill.getInstance(), null);
50     }
51 }