Bump versions to 4.0.0-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / RoleChangeListenerActor.java
1 /*
2  * Copyright (c) 2017 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.sharding;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
15 import org.opendaylight.controller.cluster.dom.api.LeaderLocation;
16 import org.opendaylight.controller.cluster.dom.api.LeaderLocationListener;
17 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
18 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
19 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
20
21 /**
22  * Proxy actor which acts as a facade for user-provided
23  * {@link LeaderLocationListener}. It subscribes for {@link LeaderStateChanged}
24  * notifications in its pre start hook and translates them to
25  * {@link LeaderLocationListener#onLeaderLocationChanged(LeaderLocation)}
26  * events.
27  */
28 @Deprecated(forRemoval = true)
29 public final class RoleChangeListenerActor extends AbstractUntypedActor {
30     private final LeaderLocationListener leaderLocationListener;
31     private final ActorRef roleChangeNotifier;
32
33     private RoleChangeListenerActor(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) {
34         this.roleChangeNotifier = requireNonNull(roleChangeNotifier);
35         this.leaderLocationListener = requireNonNull(listener);
36     }
37
38     @Override
39     public void preStart() throws Exception {
40         super.preStart();
41         roleChangeNotifier.tell(new RegisterRoleChangeListener(), getSelf());
42     }
43
44     @Override
45     protected void handleReceive(final Object message) {
46         if (message instanceof RoleChangeNotification) {
47             ignoreMessage(message);
48         } else if (message instanceof LeaderStateChanged) {
49             onLeaderStateChanged((LeaderStateChanged) message);
50         } else {
51             unknownMessage(message);
52         }
53     }
54
55     private void onLeaderStateChanged(final LeaderStateChanged message) {
56         final LeaderLocation newLocation;
57         if (message.getLeaderId() == null) {
58             newLocation = LeaderLocation.UNKNOWN;
59         } else if (message.getMemberId().equals(message.getLeaderId())) {
60             newLocation = LeaderLocation.LOCAL;
61         } else {
62             newLocation = LeaderLocation.REMOTE;
63         }
64
65         // TODO should we wrap this in try catch block?
66         leaderLocationListener.onLeaderLocationChanged(newLocation);
67     }
68
69     public static Props props(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) {
70         return Props.create(RoleChangeListenerActor.class, roleChangeNotifier, listener);
71     }
72 }