NormalizedNodeAggregator should also report empty
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeCohortRegistrationProxy.java
1 /*
2  * Copyright (c) 2016 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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import java.util.concurrent.TimeUnit;
17 import org.checkerframework.checker.lock.qual.GuardedBy;
18 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
23 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Future;
27 import scala.concurrent.duration.FiniteDuration;
28
29 public class DataTreeCohortRegistrationProxy<C extends DOMDataTreeCommitCohort> extends AbstractObjectRegistration<C>
30         implements DOMDataTreeCommitCohortRegistration<C> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCohortRegistrationProxy.class);
33     private static final Timeout TIMEOUT = new Timeout(new FiniteDuration(5, TimeUnit.SECONDS));
34     private final DOMDataTreeIdentifier subtree;
35     private final ActorRef actor;
36     private final ActorUtils actorUtils;
37     @GuardedBy("this")
38     private ActorRef cohortRegistry;
39
40     DataTreeCohortRegistrationProxy(final ActorUtils actorUtils, final DOMDataTreeIdentifier subtree,
41             final C cohort) {
42         super(cohort);
43         this.subtree = requireNonNull(subtree);
44         this.actorUtils = requireNonNull(actorUtils);
45         this.actor = actorUtils.getActorSystem().actorOf(DataTreeCohortActor.props(getInstance(),
46                 subtree.getRootIdentifier()).withDispatcher(actorUtils.getNotificationDispatcherPath()));
47     }
48
49     public void init(final String shardName) {
50         // FIXME: Add late binding to shard.
51         Future<ActorRef> findFuture = actorUtils.findLocalShardAsync(shardName);
52         findFuture.onComplete(new OnComplete<ActorRef>() {
53             @Override
54             public void onComplete(final Throwable failure, final ActorRef shard) {
55                 if (failure instanceof LocalShardNotFoundException) {
56                     LOG.debug("No local shard found for {} - DataTreeChangeListener {} at path {} "
57                             + "cannot be registered", shardName, getInstance(), subtree);
58                 } else if (failure != null) {
59                     LOG.error("Failed to find local shard {} - DataTreeChangeListener {} at path {} "
60                             + "cannot be registered", shardName, getInstance(), subtree, failure);
61                 } else {
62                     performRegistration(shard);
63                 }
64             }
65         }, actorUtils.getClientDispatcher());
66     }
67
68     private synchronized void performRegistration(final ActorRef shard) {
69         if (isClosed()) {
70             return;
71         }
72         cohortRegistry = shard;
73         Future<Object> future =
74                 Patterns.ask(shard, new DataTreeCohortActorRegistry.RegisterCohort(subtree, actor), TIMEOUT);
75         future.onComplete(new OnComplete<>() {
76
77             @Override
78             public void onComplete(final Throwable failure, final Object val) {
79                 if (failure != null) {
80                     LOG.error("Unable to register {} as commit cohort", getInstance(), failure);
81                 }
82                 if (isClosed()) {
83                     removeRegistration();
84                 }
85             }
86
87         }, actorUtils.getClientDispatcher());
88     }
89
90     @Override
91     protected synchronized void removeRegistration() {
92         if (cohortRegistry != null) {
93             cohortRegistry.tell(new DataTreeCohortActorRegistry.RemoveCohort(actor), ActorRef.noSender());
94         }
95     }
96 }