Use a static YangInstanceIdentifier in AbstractEntityOwnerChangeListener
[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
41     EntityOwnershipListenerSupport(ActorContext actorContext, String logId) {
42         this.actorContext = actorContext;
43         this.logId = logId;
44     }
45
46     String getLogId() {
47         return logId;
48     }
49
50     boolean hasCandidateForEntity(Entity entity) {
51         return entitiesWithCandidateSet.contains(entity);
52     }
53
54     void setHasCandidateForEntity(Entity entity) {
55         entitiesWithCandidateSet.add(entity);
56     }
57
58     void unsetHasCandidateForEntity(Entity entity) {
59         entitiesWithCandidateSet.remove(entity);
60     }
61
62     void addEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
63         LOG.debug("{}: Adding EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
64
65         addListener(listener, entityType);
66     }
67
68     void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) {
69         LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType);
70
71         removeListener(listener, entityType);
72     }
73
74     void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) {
75         notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner);
76     }
77
78     void notifyEntityOwnershipListener(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner,
79             EntityOwnershipListener listener) {
80         notifyListeners(entity, wasOwner, isOwner, hasOwner, Collections.singleton(listener));
81     }
82
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);
87         }
88     }
89
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);
95
96             LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
97
98             listenerActor.tell(changed, ActorRef.noSender());
99         }
100     }
101
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());
107             } else {
108                 listenerEntry.referenceCount++;
109             }
110         }
111     }
112
113     private void removeListener(EntityOwnershipListener listener, String mapKey) {
114         if (entityTypeListenerMap.remove(mapKey, listener)) {
115             ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener);
116
117             LOG.debug("{}: Found {}", logId, listenerEntry);
118
119             listenerEntry.referenceCount--;
120             if(listenerEntry.referenceCount <= 0) {
121                 listenerActorMap.remove(listener);
122
123                 if(listenerEntry.actorRef != null) {
124                     LOG.debug("Killing EntityOwnershipListenerActor {}", listenerEntry.actorRef);
125                     listenerEntry.actorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
126                 }
127             }
128         }
129     }
130
131     private ActorRef listenerActorFor(EntityOwnershipListener listener) {
132         return listenerActorMap.get(listener).actorFor(listener);
133     }
134
135     private class ListenerActorRefEntry {
136         ActorRef actorRef;
137         int referenceCount = 1;
138
139         ActorRef actorFor(EntityOwnershipListener listener) {
140             if(actorRef == null) {
141                 actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener));
142
143                 LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener);
144             }
145
146             return actorRef;
147         }
148
149         @Override
150         public String toString() {
151             return "ListenerActorRefEntry [actorRef=" + actorRef + ", referenceCount=" + referenceCount + "]";
152         }
153     }
154 }