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.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;
21 * ListenerRegistrationProxy acts as a proxy for a ListenerRegistration that was done on a remote shard
23 * Registering a DataChangeListener on the Data Store creates a new instance of the ListenerRegistrationProxy
24 * The ListenerRegistrationProxy talks to a remote ListenerRegistration actor.
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;
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;
42 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
43 DataChangeListenerRegistrationProxy(
44 L listener, ActorRef dataChangeListenerActor) {
45 this(null, listener, dataChangeListenerActor);
49 public Object getInstance() {
53 public void setListenerRegistrationActor(ActorSelection listenerRegistrationActor) {
54 boolean sendCloseMessage = false;
57 sendCloseMessage = true;
59 this.listenerRegistrationActor = listenerRegistrationActor;
62 if(sendCloseMessage) {
63 listenerRegistrationActor.tell(new
64 CloseDataChangeListenerRegistration().toSerializable(), null);
67 this.listenerRegistrationActor = listenerRegistrationActor;
70 public ActorSelection getListenerRegistrationActor() {
71 return listenerRegistrationActor;
77 boolean sendCloseMessage;
79 sendCloseMessage = !closed && listenerRegistrationActor != null;
82 if(sendCloseMessage) {
83 listenerRegistrationActor.tell(new
84 CloseDataChangeListenerRegistration().toSerializable(), null);
87 dataChangeListenerActor.tell(PoisonPill.getInstance(), null);