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);
68 void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
69 LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
71 removeListener(listener, entityType);
74 void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) {
75 notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner);
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 void notifyListeners(Entity entity, String mapKey, boolean wasOwner, boolean isOwner, boolean hasOwner) {
84 Collection<EntityOwnershipListener> listeners = entityTypeListenerMap.get(mapKey);
85 if(!listeners.isEmpty()) {
86 notifyListeners(entity, wasOwner, isOwner, hasOwner, listeners);
90 private void notifyListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
91 Collection<EntityOwnershipListener> listeners) {
92 EntityOwnershipChange changed = new EntityOwnershipChange(entity, wasOwner, isOwner, hasOwner);
93 for(EntityOwnershipListener listener: listeners) {
94 ActorRef listenerActor = listenerActorFor(listener);
96 LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
98 listenerActor.tell(changed, ActorRef.noSender());
102 private void addListener(EntityOwnershipListener listener, String mapKey) {
103 if (entityTypeListenerMap.put(mapKey, listener)) {
104 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
105 if(listenerEntry == null) {
106 listenerActorMap.put(listener, new ListenerActorRefEntry());
108 listenerEntry.referenceCount++;
113 private void removeListener(EntityOwnershipListener listener, String mapKey) {
114 if (entityTypeListenerMap.remove(mapKey, listener)) {
115 ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
117 LOG.debug("{}: Found {}", logId, listenerEntry);
119 listenerEntry.referenceCount--;
120 if(listenerEntry.referenceCount <= 0) {
121 listenerActorMap.remove(listener);
123 if(listenerEntry.actorRef != null) {
124 LOG.debug("Killing EntityOwnershipListenerActor {}", listenerEntry.actorRef);
125 listenerEntry.actorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
131 private ActorRef listenerActorFor(EntityOwnershipListener listener) {
132 return listenerActorMap.get(listener).actorFor(listener);
135 private class ListenerActorRefEntry {
137 int referenceCount = 1;
139 ActorRef actorFor(EntityOwnershipListener listener) {
140 if(actorRef == null) {
141 actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener));
143 LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener);
150 public String toString() {
151 return "ListenerActorRefEntry [actorRef=" + actorRef + ", referenceCount=" + referenceCount + "]";