2 * Copyright (c) 2015 Brocade Communications 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.datastore.entityownership;
10 import akka.actor.ActorContext;
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import com.google.common.collect.HashMultimap;
14 import com.google.common.collect.Multimap;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.IdentityHashMap;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Manages EntityOwnershipListener registrations and notifications for the EntityOwnershipShard.
30 * @author Thomas Pantelis
32 class EntityOwnershipListenerSupport {
33 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnershipListenerSupport.class);
35 private final String logId;
36 private final ActorContext actorContext;
37 private final Map<EntityOwnershipListener, ListenerActorRefEntry> listenerActorMap = new IdentityHashMap<>();
38 private final Set<Entity> entitiesWithCandidateSet = new HashSet<>();
39 private final Multimap<String, EntityOwnershipListener> entityTypeListenerMap = HashMultimap.create();
40 private volatile boolean inJeopardy = false;
42 EntityOwnershipListenerSupport(ActorContext actorContext, String logId) {
43 this.actorContext = actorContext;
52 * Set the in-jeopardy flag and indicate its previous state.
54 * @param inJeopardy new value of the in-jeopardy flag
55 * @return Previous value of the flag.
57 boolean setInJeopardy(final boolean inJeopardy) {
58 final boolean wasInJeopardy = this.inJeopardy;
59 this.inJeopardy = inJeopardy;
63 boolean hasCandidateForEntity(Entity entity) {
64 return entitiesWithCandidateSet.contains(entity);
67 void setHasCandidateForEntity(Entity entity) {
68 entitiesWithCandidateSet.add(entity);
71 void unsetHasCandidateForEntity(Entity entity) {
72 entitiesWithCandidateSet.remove(entity);
75 void addEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
76 LOG.debug("{}: Adding EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
78 addListener(listener, entityType);
81 void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
82 LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
84 removeListener(listener, entityType);
87 void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) {
88 notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner);
91 void notifyEntityOwnershipListener(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
92 EntityOwnershipListener listener) {
93 notifyListeners(entity, wasOwner, isOwner, hasOwner, Collections.singleton(listener));
96 private void notifyListeners(Entity entity, String mapKey, boolean wasOwner, boolean isOwner, boolean hasOwner) {
97 Collection<EntityOwnershipListener> listeners = entityTypeListenerMap.get(mapKey);
98 if(!listeners.isEmpty()) {
99 notifyListeners(entity, wasOwner, isOwner, hasOwner, listeners);
103 private void notifyListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
104 Collection<EntityOwnershipListener> listeners) {
105 EntityOwnershipChange changed = new EntityOwnershipChange(entity, wasOwner, isOwner, hasOwner, inJeopardy);
106 for(EntityOwnershipListener listener: listeners) {
107 ActorRef listenerActor = listenerActorFor(listener);
109 LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
111 listenerActor.tell(changed, ActorRef.noSender());
115 private void addListener(EntityOwnershipListener listener, String mapKey) {
116 if (entityTypeListenerMap.put(mapKey, listener)) {
117 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
118 if(listenerEntry == null) {
119 listenerActorMap.put(listener, new ListenerActorRefEntry());
121 listenerEntry.referenceCount++;
126 private void removeListener(EntityOwnershipListener listener, String mapKey) {
127 if (entityTypeListenerMap.remove(mapKey, listener)) {
128 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
130 LOG.debug("{}: Found {}", logId, listenerEntry);
132 listenerEntry.referenceCount--;
133 if(listenerEntry.referenceCount <= 0) {
134 listenerActorMap.remove(listener);
136 if(listenerEntry.actorRef != null) {
137 LOG.debug("Killing EntityOwnershipListenerActor {}", listenerEntry.actorRef);
138 listenerEntry.actorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
144 private ActorRef listenerActorFor(EntityOwnershipListener listener) {
145 return listenerActorMap.get(listener).actorFor(listener);
148 private class ListenerActorRefEntry {
150 int referenceCount = 1;
152 ActorRef actorFor(EntityOwnershipListener listener) {
153 if(actorRef == null) {
154 actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener));
156 LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener);
163 public String toString() {
164 return "ListenerActorRefEntry [actorRef=" + actorRef + ", referenceCount=" + referenceCount + "]";