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