2 * Copyright (c) 2014 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.notifications;
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import akka.serialization.Serialization;
15 import com.google.common.collect.Maps;
17 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
20 * The RoleChangeNotifier is responsible for receiving Raft role change messages and notifying
21 * the listeners (within the same node), which are registered with it.
23 * The RoleChangeNotifier is instantiated by the Shard and injected into the RaftActor.
25 public class RoleChangeNotifier extends AbstractUntypedActor implements AutoCloseable {
27 private String memberId;
28 private Map<ActorPath, ActorRef> registeredListeners = Maps.newHashMap();
29 private RoleChangeNotification latestRoleChangeNotification = null;
31 public RoleChangeNotifier(String memberId) {
32 this.memberId = memberId;
35 public static Props getProps(final String memberId) {
36 return Props.create(RoleChangeNotifier.class, memberId);
40 public void preStart() throws Exception {
42 LOG.info("RoleChangeNotifier:{} created and ready for shard:{}",
43 Serialization.serializedActorPath(getSelf()), memberId);
47 protected void handleReceive(Object message) throws Exception {
48 if (message instanceof RegisterRoleChangeListener) {
49 // register listeners for this shard
51 ActorRef curRef = registeredListeners.get(getSender().path());
53 // ActorPaths would pass equal even if the unique id of the actors are different
54 // if a listener actor is re-registering after reincarnation, then removing the existing
55 // entry so the actor path with correct unique id is registered.
56 registeredListeners.remove(getSender().path());
58 registeredListeners.put(getSender().path(), getSender());
60 LOG.info("RoleChangeNotifier for {} , registered listener {}", memberId,
61 getSender().path().toString());
63 getSender().tell(new RegisterRoleChangeListenerReply(), getSelf());
65 if (latestRoleChangeNotification != null) {
66 getSender().tell(latestRoleChangeNotification, getSelf());
70 } else if (message instanceof RoleChanged) {
71 // this message is sent by RaftActor. Notify registered listeners when this message is received.
72 RoleChanged roleChanged = (RoleChanged) message;
74 LOG.info("RoleChangeNotifier for {} , received role change from {} to {}", memberId,
75 roleChanged.getOldRole(), roleChanged.getNewRole());
77 latestRoleChangeNotification =
78 new RoleChangeNotification(roleChanged.getMemberId(),
79 roleChanged.getOldRole(), roleChanged.getNewRole());
81 for (ActorRef listener: registeredListeners.values()) {
82 listener.tell(latestRoleChangeNotification, getSelf());
88 public void close() throws Exception {
89 registeredListeners.clear();