2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.sharding;
10 import static java.util.Objects.requireNonNull;
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;
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)}
28 @Deprecated(forRemoval = true)
29 public final class RoleChangeListenerActor extends AbstractUntypedActor {
30 private final LeaderLocationListener leaderLocationListener;
31 private final ActorRef roleChangeNotifier;
33 private RoleChangeListenerActor(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) {
34 this.roleChangeNotifier = requireNonNull(roleChangeNotifier);
35 this.leaderLocationListener = requireNonNull(listener);
39 public void preStart() throws Exception {
41 roleChangeNotifier.tell(new RegisterRoleChangeListener(), getSelf());
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);
51 unknownMessage(message);
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;
62 newLocation = LeaderLocation.REMOTE;
65 // TODO should we wrap this in try catch block?
66 leaderLocationListener.onLeaderLocationChanged(newLocation);
69 public static Props props(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) {
70 return Props.create(RoleChangeListenerActor.class, roleChangeNotifier, listener);