Merge "Fixed test which tested incorrect string formating"
[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 volatile ActorSelection listenerRegistrationActor;
29     private final AsyncDataChangeListener listener;
30     private final ActorRef dataChangeListenerActor;
31     private boolean closed = false;
32
33     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
34     DataChangeListenerRegistrationProxy(
35         ActorSelection listenerRegistrationActor,
36         L listener, ActorRef dataChangeListenerActor) {
37         this.listenerRegistrationActor = listenerRegistrationActor;
38         this.listener = listener;
39         this.dataChangeListenerActor = dataChangeListenerActor;
40     }
41
42     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
43     DataChangeListenerRegistrationProxy(
44         L listener, ActorRef dataChangeListenerActor) {
45         this(null, listener, dataChangeListenerActor);
46     }
47
48     @Override
49     public Object getInstance() {
50         return listener;
51     }
52
53     public void setListenerRegistrationActor(ActorSelection listenerRegistrationActor) {
54         boolean sendCloseMessage = false;
55         synchronized(this) {
56             if(closed) {
57                 sendCloseMessage = true;
58             } else {
59                 this.listenerRegistrationActor = listenerRegistrationActor;
60             }
61         }
62         if(sendCloseMessage) {
63             listenerRegistrationActor.tell(new
64                 CloseDataChangeListenerRegistration().toSerializable(), null);
65         }
66
67         this.listenerRegistrationActor = listenerRegistrationActor;
68     }
69
70     public ActorSelection getListenerRegistrationActor() {
71         return listenerRegistrationActor;
72     }
73
74     @Override
75     public void close() {
76
77         boolean sendCloseMessage;
78         synchronized(this) {
79             sendCloseMessage = !closed && listenerRegistrationActor != null;
80             closed = true;
81         }
82         if(sendCloseMessage) {
83             listenerRegistrationActor.tell(new
84                 CloseDataChangeListenerRegistration().toSerializable(), null);
85         }
86
87         dataChangeListenerActor.tell(PoisonPill.getInstance(), null);
88     }
89 }