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.Arrays;
16 import java.util.Collection;
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();
41 EntityOwnershipListenerSupport(ActorContext actorContext, String logId) {
42 this.actorContext = actorContext;
50 boolean hasCandidateForEntity(Entity entity) {
51 return entitiesWithCandidateSet.contains(entity);
54 void setHasCandidateForEntity(Entity entity) {
55 entitiesWithCandidateSet.add(entity);
58 void unsetHasCandidateForEntity(Entity entity) {
59 entitiesWithCandidateSet.remove(entity);
62 void addEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
63 LOG.debug("{}: Adding EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
65 addListener(listener, entityType, entityTypeListenerMap);
68 void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
69 LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
71 removeListener(listener, entityType, entityTypeListenerMap);
74 void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) {
75 notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner, entityTypeListenerMap);
78 void notifyEntityOwnershipListener(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
79 EntityOwnershipListener listener) {
80 notifyListeners(entity, wasOwner, isOwner, hasOwner, Arrays.asList(listener));
83 private <T> void notifyListeners(Entity entity, T mapKey, boolean wasOwner, boolean isOwner, boolean hasOwner,
84 Multimap<T, EntityOwnershipListener> listenerMap) {
85 Collection<EntityOwnershipListener> listeners = listenerMap.get(mapKey);
86 if(!listeners.isEmpty()) {
87 notifyListeners(entity, wasOwner, isOwner, hasOwner, listeners);
91 private void notifyListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
92 Collection<EntityOwnershipListener> listeners) {
93 EntityOwnershipChange changed = new EntityOwnershipChange(entity, wasOwner, isOwner, hasOwner);
94 for(EntityOwnershipListener listener: listeners) {
95 ActorRef listenerActor = listenerActorFor(listener);
97 LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
99 listenerActor.tell(changed, ActorRef.noSender());
103 private <T> void addListener(EntityOwnershipListener listener, T mapKey,
104 Multimap<T, EntityOwnershipListener> toListenerMap) {
105 if(toListenerMap.put(mapKey, listener)) {
106 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
107 if(listenerEntry == null) {
108 listenerActorMap.put(listener, new ListenerActorRefEntry());
110 listenerEntry.referenceCount++;
115 private <T> void removeListener(EntityOwnershipListener listener, T mapKey,
116 Multimap<T, EntityOwnershipListener> fromListenerMap) {
117 if(fromListenerMap.remove(mapKey, listener)) {
118 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
120 LOG.debug("{}: Found {}", logId, listenerEntry);
122 listenerEntry.referenceCount--;
123 if(listenerEntry.referenceCount <= 0) {
124 listenerActorMap.remove(listener);
126 if(listenerEntry.actorRef != null) {
127 LOG.debug("Killing EntityOwnershipListenerActor {}", listenerEntry.actorRef);
128 listenerEntry.actorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
134 private ActorRef listenerActorFor(EntityOwnershipListener listener) {
135 return listenerActorMap.get(listener).actorFor(listener);
138 private class ListenerActorRefEntry {
140 int referenceCount = 1;
142 ActorRef actorFor(EntityOwnershipListener listener) {
143 if(actorRef == null) {
144 actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener));
146 LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener);
153 public String toString() {
154 return "ListenerActorRefEntry [actorRef=" + actorRef + ", referenceCount=" + referenceCount + "]";