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 akka.dispatch.OnComplete;
15 import com.google.common.annotations.VisibleForTesting;
16 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
18 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataChangeListener;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.concurrent.Future;
33 * ListenerRegistrationProxy acts as a proxy for a ListenerRegistration that was done on a remote shard
35 * Registering a DataChangeListener on the Data Store creates a new instance of the ListenerRegistrationProxy
36 * The ListenerRegistrationProxy talks to a remote ListenerRegistration actor.
39 @SuppressWarnings("rawtypes")
40 public class DataChangeListenerRegistrationProxy implements ListenerRegistration {
42 private static final Logger LOG = LoggerFactory.getLogger(DataChangeListenerRegistrationProxy.class);
44 private volatile ActorSelection listenerRegistrationActor;
45 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener;
46 private ActorRef dataChangeListenerActor;
47 private final String shardName;
48 private final ActorContext actorContext;
49 private boolean closed = false;
51 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
52 DataChangeListenerRegistrationProxy(String shardName, ActorContext actorContext, L listener) {
53 this.shardName = shardName;
54 this.actorContext = actorContext;
55 this.listener = listener;
59 ActorSelection getListenerRegistrationActor() {
60 return listenerRegistrationActor;
64 ActorRef getDataChangeListenerActor() {
65 return dataChangeListenerActor;
69 public Object getInstance() {
73 private void setListenerRegistrationActor(ActorSelection listenerRegistrationActor) {
74 if (listenerRegistrationActor == null) {
78 boolean sendCloseMessage = false;
81 sendCloseMessage = true;
83 this.listenerRegistrationActor = listenerRegistrationActor;
87 if (sendCloseMessage) {
88 listenerRegistrationActor.tell(CloseDataChangeListenerRegistration.INSTANCE, null);
92 public void init(final YangInstanceIdentifier path, final AsyncDataBroker.DataChangeScope scope) {
94 dataChangeListenerActor = actorContext.getActorSystem().actorOf(
95 DataChangeListener.props(listener).withDispatcher(actorContext.getNotificationDispatcherPath()));
97 Future<ActorRef> findFuture = actorContext.findLocalShardAsync(shardName);
98 findFuture.onComplete(new OnComplete<ActorRef>() {
100 public void onComplete(Throwable failure, ActorRef shard) {
101 if (failure instanceof LocalShardNotFoundException) {
102 LOG.debug("No local shard found for {} - DataChangeListener {} at path {} "
103 + "cannot be registered", shardName, listener, path);
104 } else if (failure != null) {
105 LOG.error("Failed to find local shard {} - DataChangeListener {} at path {} "
106 + "cannot be registered: {}", shardName, listener, path, failure);
108 doRegistration(shard, path, scope);
111 }, actorContext.getClientDispatcher());
114 private void doRegistration(ActorRef shard, final YangInstanceIdentifier path,
115 DataChangeScope scope) {
117 Future<Object> future = actorContext.executeOperationAsync(shard,
118 new RegisterChangeListener(path, dataChangeListenerActor, scope,
119 listener instanceof ClusteredDOMDataChangeListener),
120 actorContext.getDatastoreContext().getShardInitializationTimeout());
122 future.onComplete(new OnComplete<Object>() {
124 public void onComplete(Throwable failure, Object result) {
125 if (failure != null) {
126 LOG.error("Failed to register DataChangeListener {} at path {}",
127 listener, path.toString(), failure);
129 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
130 setListenerRegistrationActor(actorContext.actorSelection(
131 reply.getListenerRegistrationPath()));
134 }, actorContext.getClientDispatcher());
138 public void close() {
140 boolean sendCloseMessage;
141 synchronized (this) {
142 sendCloseMessage = !closed && listenerRegistrationActor != null;
146 if (sendCloseMessage) {
147 listenerRegistrationActor.tell(CloseDataChangeListenerRegistration.INSTANCE, ActorRef.noSender());
148 listenerRegistrationActor = null;
151 if (dataChangeListenerActor != null) {
152 dataChangeListenerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
153 dataChangeListenerActor = null;