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