2 * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.cluster.example;
11 import akka.actor.ActorRef;
12 import akka.actor.Cancellable;
13 import akka.actor.Props;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.HashMap;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
19 import org.opendaylight.controller.cluster.example.messages.RegisterListener;
20 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
21 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
22 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
23 import scala.concurrent.Await;
24 import scala.concurrent.duration.FiniteDuration;
27 * This is a sample implementation of a Role Change Listener which is an actor, which registers itself
28 * to the ClusterRoleChangeNotifier.
31 * The Role Change listener receives a SetNotifiers message with the notifiers to register itself with.
34 * It kicks of a scheduler which sends registration messages to the notifiers, till it gets a
35 * RegisterRoleChangeListenerReply.
38 * If all the notifiers have been regsitered with, then it cancels the scheduler.
39 * It starts the scheduler again when it receives a new registration
41 public class ExampleRoleChangeListener extends AbstractUntypedActor implements AutoCloseable {
42 // the akka url should be set to the notifiers actor-system and domain.
43 private static final String NOTIFIER_AKKA_URL = "akka://raft-test@127.0.0.1:2550/user/";
45 private final Map<String, Boolean> notifierRegistrationStatus = new HashMap<>();
46 private Cancellable registrationSchedule = null;
47 private static final FiniteDuration DURATION = new FiniteDuration(100, TimeUnit.MILLISECONDS);
48 private static final FiniteDuration SCHEDULER_DURATION = new FiniteDuration(1, TimeUnit.SECONDS);
49 private static final String[] SHARDS_TO_MONITOR = new String[] {"example"};
51 public ExampleRoleChangeListener(String memberName) {
52 scheduleRegistrationListener(SCHEDULER_DURATION);
53 populateRegistry(memberName);
56 public static Props getProps(final String memberName) {
57 return Props.create(ExampleRoleChangeListener.class, memberName);
61 protected void handleReceive(Object message) {
62 if (message instanceof RegisterListener) {
63 // called by the scheduler at intervals to register any unregistered notifiers
64 sendRegistrationRequests();
66 } else if (message instanceof RegisterRoleChangeListenerReply) {
67 // called by the Notifier
68 handleRegisterRoleChangeListenerReply(getSender().path().toString());
70 } else if (message instanceof RoleChangeNotification) {
71 // called by the Notifier
72 RoleChangeNotification notification = (RoleChangeNotification) message;
74 LOG.info("Role Change Notification received for member:{}, old role:{}, new role:{}",
75 notification.getMemberId(), notification.getOldRole(), notification.getNewRole());
77 // the apps dependent on such notifications can be called here
78 //TODO: add implementation here
83 private void scheduleRegistrationListener(FiniteDuration interval) {
84 LOG.debug("--->scheduleRegistrationListener called.");
85 registrationSchedule = getContext().system().scheduler().schedule(
86 interval, interval, getSelf(), new RegisterListener(),
87 getContext().system().dispatcher(), getSelf());
91 private void populateRegistry(String memberName) {
92 String notifier = new StringBuilder().append(NOTIFIER_AKKA_URL).append(memberName)
93 .append("/").append(memberName).append("-notifier").toString();
95 if (!notifierRegistrationStatus.containsKey(notifier)) {
96 notifierRegistrationStatus.put(notifier, false);
99 if (!registrationSchedule.isCancelled()) {
100 scheduleRegistrationListener(SCHEDULER_DURATION);
104 @SuppressWarnings("checkstyle:IllegalCatch")
105 @SuppressFBWarnings("REC_CATCH_EXCEPTION")
106 private void sendRegistrationRequests() {
107 for (Map.Entry<String, Boolean> entry : notifierRegistrationStatus.entrySet()) {
108 if (!entry.getValue()) {
110 LOG.debug("{} registering with {}", getSelf().path().toString(), entry.getKey());
111 ActorRef notifier = Await.result(
112 getContext().actorSelection(entry.getKey()).resolveOne(DURATION), DURATION);
114 notifier.tell(new RegisterRoleChangeListener(), getSelf());
116 } catch (Exception e) {
117 LOG.error("ERROR!! Unable to send registration request to notifier {}", entry.getKey());
123 private void handleRegisterRoleChangeListenerReply(String senderId) {
124 if (notifierRegistrationStatus.containsKey(senderId)) {
125 notifierRegistrationStatus.put(senderId, true);
127 //cancel the schedule when listener is registered with all notifiers
128 if (!registrationSchedule.isCancelled()) {
129 boolean cancelScheduler = true;
130 for (Boolean value : notifierRegistrationStatus.values()) {
131 cancelScheduler = cancelScheduler & value;
133 if (cancelScheduler) {
134 registrationSchedule.cancel();
139 "Unexpected, RegisterRoleChangeListenerReply received from notifier which is not known to Listener:{}",
146 public void close() {
147 registrationSchedule.cancel();