0a338428082e4a86d632817988c26336c143cb04
[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.CloseDataTreeNotificationListenerRegistration;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
21 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
22 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
23 import org.opendaylight.mdsal.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     private final YangInstanceIdentifier registeredPath;
41
42     @GuardedBy("this")
43     private ActorSelection listenerRegistrationActor;
44
45     DataTreeChangeListenerProxy(final ActorContext actorContext, final T listener,
46             final YangInstanceIdentifier registeredPath) {
47         super(listener);
48         this.actorContext = Preconditions.checkNotNull(actorContext);
49         this.registeredPath = Preconditions.checkNotNull(registeredPath);
50         this.dataChangeListenerActor = actorContext.getActorSystem().actorOf(
51                 DataTreeChangeListenerActor.props(getInstance(), registeredPath)
52                     .withDispatcher(actorContext.getNotificationDispatcherPath()));
53
54         LOG.debug("{}: Created actor {} for DTCL {}", actorContext.getDatastoreContext().getLogicalStoreType(),
55                 dataChangeListenerActor, listener);
56     }
57
58     @Override
59     protected synchronized void removeRegistration() {
60         if (listenerRegistrationActor != null) {
61             listenerRegistrationActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(),
62                     ActorRef.noSender());
63             listenerRegistrationActor = null;
64         }
65
66         dataChangeListenerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
67     }
68
69     void init(final String shardName) {
70         Future<ActorRef> findFuture = actorContext.findLocalShardAsync(shardName);
71         findFuture.onComplete(new OnComplete<ActorRef>() {
72             @Override
73             public void onComplete(final Throwable failure, final ActorRef shard) {
74                 if (failure instanceof LocalShardNotFoundException) {
75                     LOG.debug("{}: No local shard found for {} - DataTreeChangeListener {} at path {} "
76                             + "cannot be registered", logContext(), shardName, getInstance(), registeredPath);
77                 } else if (failure != null) {
78                     LOG.error("{}: Failed to find local shard {} - DataTreeChangeListener {} at path {} "
79                             + "cannot be registered: {}", logContext(), shardName, getInstance(), registeredPath,
80                             failure);
81                 } else {
82                     doRegistration(shard);
83                 }
84             }
85         }, actorContext.getClientDispatcher());
86     }
87
88     private void setListenerRegistrationActor(final ActorSelection actor) {
89         if (actor == null) {
90             LOG.debug("{}: Ignoring null actor on {}", logContext(), this);
91             return;
92         }
93
94         synchronized (this) {
95             if (!isClosed()) {
96                 this.listenerRegistrationActor = actor;
97                 return;
98             }
99         }
100
101         // This registration has already been closed, notify the actor
102         actor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), null);
103     }
104
105     private void doRegistration(final ActorRef shard) {
106
107         Future<Object> future = actorContext.executeOperationAsync(shard,
108                 new RegisterDataTreeChangeListener(registeredPath, dataChangeListenerActor,
109                         getInstance() instanceof ClusteredDOMDataTreeChangeListener),
110                 actorContext.getDatastoreContext().getShardInitializationTimeout());
111
112         future.onComplete(new OnComplete<Object>() {
113             @Override
114             public void onComplete(final Throwable failure, final Object result) {
115                 if (failure != null) {
116                     LOG.error("{}: Failed to register DataTreeChangeListener {} at path {}", logContext(),
117                             getInstance(), registeredPath, failure);
118                 } else {
119                     RegisterDataTreeNotificationListenerReply reply = (RegisterDataTreeNotificationListenerReply)result;
120                     setListenerRegistrationActor(actorContext.actorSelection(
121                             reply.getListenerRegistrationPath()));
122                 }
123             }
124         }, actorContext.getClientDispatcher());
125     }
126
127     @VisibleForTesting
128     synchronized ActorSelection getListenerRegistrationActor() {
129         return listenerRegistrationActor;
130     }
131
132     @VisibleForTesting
133     ActorRef getDataChangeListenerActor() {
134         return dataChangeListenerActor;
135     }
136
137     private String logContext() {
138         return actorContext.getDatastoreContext().getLogicalStoreType().toString();
139     }
140 }