Merge "Stop SubtreeFitler from rereading reply even if no filter element is present."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxy.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.actor.PoisonPill;
13 import akka.dispatch.OnComplete;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Preconditions;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
21 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
23 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.Future;
28
29 /**
30  * Proxy class for holding required state to lazily instantiate a listener registration with an
31  * asynchronously-discovered actor.
32  *
33  * @param <T> listener type
34  */
35 final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> extends AbstractListenerRegistration<T> {
36     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerProxy.class);
37     private final ActorRef dataChangeListenerActor;
38     private final ActorContext actorContext;
39
40     @GuardedBy("this")
41     private ActorSelection listenerRegistrationActor;
42
43     public DataTreeChangeListenerProxy(final ActorContext actorContext, final T listener) {
44         super(listener);
45         this.actorContext = Preconditions.checkNotNull(actorContext);
46         this.dataChangeListenerActor = actorContext.getActorSystem().actorOf(
47             DataTreeChangeListenerActor.props(getInstance()).withDispatcher(actorContext.getNotificationDispatcherPath()));
48     }
49
50     @Override
51     protected synchronized void removeRegistration() {
52         if (listenerRegistrationActor != null) {
53             listenerRegistrationActor.tell(CloseDataTreeChangeListenerRegistration.getInstance(), ActorRef.noSender());
54             listenerRegistrationActor = null;
55         }
56
57         dataChangeListenerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
58     }
59
60     void init(final String shardName, final YangInstanceIdentifier treeId) {
61         Future<ActorRef> findFuture = actorContext.findLocalShardAsync(shardName);
62         findFuture.onComplete(new OnComplete<ActorRef>() {
63             @Override
64             public void onComplete(final Throwable failure, final ActorRef shard) {
65                 if (failure instanceof LocalShardNotFoundException) {
66                     LOG.debug("No local shard found for {} - DataTreeChangeListener {} at path {} " +
67                             "cannot be registered", shardName, getInstance(), treeId);
68                 } else if (failure != null) {
69                     LOG.error("Failed to find local shard {} - DataTreeChangeListener {} at path {} " +
70                             "cannot be registered: {}", shardName, getInstance(), treeId, failure);
71                 } else {
72                     doRegistration(shard, treeId);
73                 }
74             }
75         }, actorContext.getClientDispatcher());
76     }
77
78     private void setListenerRegistrationActor(final ActorSelection actor) {
79         if (actor == null) {
80             LOG.debug("Ignoring null actor on {}", this);
81             return;
82         }
83
84         synchronized (this) {
85             if (!isClosed()) {
86                 this.listenerRegistrationActor = actor;
87                 return;
88             }
89         }
90
91         // This registration has already been closed, notify the actor
92         actor.tell(CloseDataTreeChangeListenerRegistration.getInstance(), null);
93     }
94
95     private void doRegistration(final ActorRef shard, final YangInstanceIdentifier path) {
96
97         Future<Object> future = actorContext.executeOperationAsync(shard,
98                 new RegisterDataTreeChangeListener(path, dataChangeListenerActor),
99                 actorContext.getDatastoreContext().getShardInitializationTimeout());
100
101         future.onComplete(new OnComplete<Object>(){
102             @Override
103             public void onComplete(final Throwable failure, final Object result) {
104                 if (failure != null) {
105                     LOG.error("Failed to register DataTreeChangeListener {} at path {}",
106                             getInstance(), path.toString(), failure);
107                 } else {
108                     RegisterDataTreeChangeListenerReply reply = (RegisterDataTreeChangeListenerReply) result;
109                     setListenerRegistrationActor(actorContext.actorSelection(
110                             reply.getListenerRegistrationPath()));
111                 }
112             }
113         }, actorContext.getClientDispatcher());
114     }
115
116     @VisibleForTesting
117     ActorSelection getListenerRegistrationActor() {
118         return listenerRegistrationActor;
119     }
120
121     @VisibleForTesting
122     ActorRef getDataChangeListenerActor() {
123         return dataChangeListenerActor;
124     }
125 }