Define DataStoreVersions.MAGNESIUM_VERSION
[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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorPath;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import akka.actor.Props;
16
17 /**
18  * Base class for factories instantiating delegates which are local to the
19  * shard leader.
20  *
21  * @param <D> delegate type
22  * @param <M> message type
23  */
24 abstract class LeaderLocalDelegateFactory<M> {
25     private final Shard shard;
26
27     protected LeaderLocalDelegateFactory(final Shard shard) {
28         this.shard = requireNonNull(shard);
29     }
30
31     protected final ActorRef getSelf() {
32         return shard.getSelf();
33     }
34
35     protected final Shard getShard() {
36         return shard;
37     }
38
39     protected final String persistenceId() {
40         return shard.persistenceId();
41     }
42
43     protected final void tellSender(final Object message) {
44         shard.getSender().tell(message, getSelf());
45     }
46
47     protected final ActorRef createActor(final Props props) {
48         return shard.getContext().actorOf(props);
49     }
50
51     protected final ActorSelection selectActor(final ActorRef ref) {
52         return shard.getContext().system().actorSelection(ref.path());
53     }
54
55     protected final ActorSelection selectActor(final ActorPath path) {
56         return shard.getContext().system().actorSelection(path);
57     }
58
59     /**
60      * Invoked whenever the local shard's leadership role changes.
61      *
62      * @param isLeader true if the shard has become leader, false if it has 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 }