Merge "Model dom-broker statistics"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / notifications / RoleChangeNotifier.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.notifications;
10
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;
16 import java.util.Map;
17 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
18
19 /**
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.
22  * <p/>
23  * The RoleChangeNotifier is instantiated by the Shard and injected into the RaftActor.
24  */
25 public class RoleChangeNotifier extends AbstractUntypedActor implements AutoCloseable {
26
27     private String memberId;
28     private Map<ActorPath, ActorRef> registeredListeners = Maps.newHashMap();
29     private RoleChangeNotification latestRoleChangeNotification = null;
30
31     public RoleChangeNotifier(String memberId) {
32         this.memberId = memberId;
33     }
34
35     public static Props getProps(final String memberId) {
36         return Props.create(RoleChangeNotifier.class, memberId);
37     }
38
39     @Override
40     public void preStart() throws Exception {
41         super.preStart();
42         LOG.info("RoleChangeNotifier:{} created and ready for shard:{}",
43             Serialization.serializedActorPath(getSelf()), memberId);
44     }
45
46     @Override
47     protected void handleReceive(Object message) throws Exception {
48         if (message instanceof RegisterRoleChangeListener) {
49             // register listeners for this shard
50
51             ActorRef curRef = registeredListeners.get(getSender().path());
52             if (curRef != null) {
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());
57             }
58             registeredListeners.put(getSender().path(), getSender());
59
60             LOG.info("RoleChangeNotifier for {} , registered listener {}", memberId,
61                 getSender().path().toString());
62
63             getSender().tell(new RegisterRoleChangeListenerReply(), getSelf());
64
65             if (latestRoleChangeNotification != null) {
66                 getSender().tell(latestRoleChangeNotification, getSelf());
67             }
68
69
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;
73
74             LOG.info("RoleChangeNotifier for {} , received role change from {} to {}", memberId,
75                 roleChanged.getOldRole(), roleChanged.getNewRole());
76
77             latestRoleChangeNotification =
78                 new RoleChangeNotification(roleChanged.getMemberId(),
79                     roleChanged.getOldRole(), roleChanged.getNewRole());
80
81             for (ActorRef listener: registeredListeners.values()) {
82                 listener.tell(latestRoleChangeNotification, getSelf());
83             }
84         }
85     }
86
87     @Override
88     public void close() throws Exception {
89         registeredListeners.clear();
90     }
91 }
92