c0fe55f12d14ee092a8d34ac6cf41882523fb929
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipListenerSupport.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.entityownership;
9
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;
19 import java.util.Map;
20 import java.util.Set;
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;
26
27 /**
28  * Manages EntityOwnershipListener registrations and notifications for the EntityOwnershipShard.
29  *
30  * @author Thomas Pantelis
31  */
32 class EntityOwnershipListenerSupport {
33     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnershipListenerSupport.class);
34
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;
41
42     EntityOwnershipListenerSupport(ActorContext actorContext, String logId) {
43         this.actorContext = actorContext;
44         this.logId = logId;
45     }
46
47     String getLogId() {
48         return logId;
49     }
50
51     /**
52      * Set the in-jeopardy flag and indicate its previous state.
53      *
54      * @param inJeopardy new value of the in-jeopardy flag
55      * @return Previous value of the flag.
56      */
57     boolean setInJeopardy(final boolean inJeopardy) {
58         final boolean wasInJeopardy = this.inJeopardy;
59         this.inJeopardy = inJeopardy;
60         return wasInJeopardy;
61     }
62
63     boolean hasCandidateForEntity(Entity entity) {
64         return entitiesWithCandidateSet.contains(entity);
65     }
66
67     void setHasCandidateForEntity(Entity entity) {
68         entitiesWithCandidateSet.add(entity);
69     }
70
71     void unsetHasCandidateForEntity(Entity entity) {
72         entitiesWithCandidateSet.remove(entity);
73     }
74
75     void addEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
76         LOG.debug("{}: Adding EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
77
78         addListener(listener, entityType);
79     }
80
81     void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
82         LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
83
84         removeListener(listener, entityType);
85     }
86
87     void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) {
88         notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner);
89     }
90
91     void notifyEntityOwnershipListener(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
92             EntityOwnershipListener listener) {
93         notifyListeners(entity, wasOwner, isOwner, hasOwner, Collections.singleton(listener));
94     }
95
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);
100         }
101     }
102
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);
108
109             LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
110
111             listenerActor.tell(changed, ActorRef.noSender());
112         }
113     }
114
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());
120             } else {
121                 listenerEntry.referenceCount++;
122             }
123         }
124     }
125
126     private void removeListener(EntityOwnershipListener listener, String mapKey) {
127         if (entityTypeListenerMap.remove(mapKey, listener)) {
128             ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
129
130             LOG.debug("{}: Found {}", logId, listenerEntry);
131
132             listenerEntry.referenceCount--;
133             if(listenerEntry.referenceCount <= 0) {
134                 listenerActorMap.remove(listener);
135
136                 if(listenerEntry.actorRef != null) {
137                     LOG.debug("Killing EntityOwnershipListenerActor {}", listenerEntry.actorRef);
138                     listenerEntry.actorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
139                 }
140             }
141         }
142     }
143
144     private ActorRef listenerActorFor(EntityOwnershipListener listener) {
145         return listenerActorMap.get(listener).actorFor(listener);
146     }
147
148     private class ListenerActorRefEntry {
149         ActorRef actorRef;
150         int referenceCount = 1;
151
152         ActorRef actorFor(EntityOwnershipListener listener) {
153             if(actorRef == null) {
154                 actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener));
155
156                 LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener);
157             }
158
159             return actorRef;
160         }
161
162         @Override
163         public String toString() {
164             return "ListenerActorRefEntry [actorRef=" + actorRef + ", referenceCount=" + referenceCount + "]";
165         }
166     }
167 }