Add OnDemandShardState to report additional Shard state
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LeaderLocalDelegateFactory.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.ActorPath;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.Props;
14 import com.google.common.base.Preconditions;
15
16 /**
17  * Base class for factories instantiating delegates which are local to the
18  * shard leader.
19  *
20  * @param <D> delegate type
21  * @param <M> message type
22  */
23 abstract class LeaderLocalDelegateFactory<M, D> extends DelegateFactory<M, D> {
24     private final Shard shard;
25
26     protected LeaderLocalDelegateFactory(final Shard shard) {
27         this.shard = Preconditions.checkNotNull(shard);
28     }
29
30     protected final ActorRef getSelf() {
31         return shard.getSelf();
32     }
33
34     protected final Shard getShard() {
35         return shard;
36     }
37
38     protected final String persistenceId() {
39         return shard.persistenceId();
40     }
41
42     protected final void tellSender(final Object message) {
43         shard.getSender().tell(message, getSelf());
44     }
45
46     protected final ActorRef createActor(final Props props) {
47         return shard.getContext().actorOf(props);
48     }
49
50     protected final ActorSelection selectActor(ActorRef ref) {
51         return shard.getContext().system().actorSelection(ref.path());
52     }
53
54     protected final ActorSelection selectActor(ActorPath path) {
55         return shard.getContext().system().actorSelection(path);
56     }
57
58     /**
59      * Invoked whenever the local shard's leadership role changes.
60      *
61      * @param isLeader true if the shard has become leader, false if it has
62  *                 become a follower.
63      * @param hasLeader true if the shard knows about leader ID
64      */
65     abstract void onLeadershipChange(boolean isLeader, boolean hasLeader);
66
67     abstract void onMessage(M message, boolean isLeader, boolean hasLeader);
68 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.