2 * Copyright (c) 2017 Cisco 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.mdsal.eos.dom.simple;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED;
12 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.MoreObjects;
16 import com.google.common.base.MoreObjects.ToStringHelper;
17 import com.google.common.collect.ArrayListMultimap;
18 import com.google.common.collect.HashBasedTable;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Multimap;
21 import com.google.common.collect.Table;
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23 import java.util.Collection;
24 import java.util.Optional;
25 import java.util.UUID;
26 import org.checkerframework.checker.lock.qual.GuardedBy;
27 import org.kohsuke.MetaInfServices;
28 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
29 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
30 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
31 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
32 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
33 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
35 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
36 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
37 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.osgi.service.component.annotations.Component;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 @Component(immediate = true)
45 public class SimpleDOMEntityOwnershipService implements DOMEntityOwnershipService {
46 private static final Logger LOG = LoggerFactory.getLogger(SimpleDOMEntityOwnershipService.class);
48 @GuardedBy("entities")
49 private final Table<String, YangInstanceIdentifier, DOMEntity> entities = HashBasedTable.create();
51 @GuardedBy("listeners")
52 private final Multimap<String, DOMEntityOwnershipListener> listeners = ArrayListMultimap.create(0, 1);
54 private final UUID uuid;
57 SimpleDOMEntityOwnershipService(final UUID uuid) {
58 this.uuid = requireNonNull(uuid);
61 public SimpleDOMEntityOwnershipService() {
62 this(UUID.randomUUID());
66 public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
67 throws CandidateAlreadyRegisteredException {
68 synchronized (entities) {
69 final DOMEntity prev = entities.get(entity.getType(), entity.getIdentifier());
71 throw new CandidateAlreadyRegisteredException(prev);
74 entities.put(entity.getType(), entity.getIdentifier(), entity);
75 LOG.debug("{}: registered candidate {}", uuid, entity);
78 notifyListeners(entity, LOCAL_OWNERSHIP_GRANTED);
79 return new EntityRegistration(entity);
83 public DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
84 final DOMEntityOwnershipListener listener) {
86 final Collection<DOMEntity> owned;
87 synchronized (entities) {
88 owned = ImmutableList.copyOf(entities.row(entityType).values());
89 LOG.trace("{}: acquired candidates {} for new listener {}", uuid, owned, listener);
92 synchronized (listeners) {
93 listeners.put(entityType, listener);
96 for (DOMEntity entity : owned) {
97 notifyListener(listener, new DOMEntityOwnershipChange(entity, LOCAL_OWNERSHIP_GRANTED));
99 LOG.debug("{}: registered listener {}", uuid, listener);
100 return new ListenerRegistration(entityType, listener);
104 public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
105 return isCandidateRegistered(forEntity) ? Optional.of(EntityOwnershipState.IS_OWNER) : Optional.empty();
109 public boolean isCandidateRegistered(final DOMEntity forEntity) {
110 synchronized (entities) {
111 return entities.contains(forEntity.getType(), forEntity.getIdentifier());
115 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
116 justification = "https://github.com/spotbugs/spotbugs/issues/811")
117 private void removeEntity(final DOMEntity entity) {
118 synchronized (entities) {
119 entities.remove(entity.getType(), entity.getIdentifier());
120 LOG.debug("{}: unregistered candidate {}", uuid, entity);
123 notifyListeners(entity, LOCAL_OWNERSHIP_LOST_NO_OWNER);
126 @SuppressWarnings("checkstyle:illegalCatch")
127 private void notifyListener(final DOMEntityOwnershipListener listener, final DOMEntityOwnershipChange change) {
129 LOG.trace("{} notifying listener {} change {}", uuid, listener, change);
130 listener.ownershipChanged(change);
131 } catch (RuntimeException e) {
132 LOG.warn("{}: Listener {} change {} failed", uuid, listener, change, e);
136 private void notifyListeners(final DOMEntity entity, final EntityOwnershipChangeState state) {
137 final DOMEntityOwnershipChange change = new DOMEntityOwnershipChange(entity, state);
139 final Collection<DOMEntityOwnershipListener> snap;
141 synchronized (listeners) {
142 snap = ImmutableList.copyOf(listeners.get(entity.getType()));
145 for (DOMEntityOwnershipListener listener : snap) {
146 notifyListener(listener, change);
150 void unregisterListener(final ListenerRegistration reg) {
151 synchronized (listeners) {
152 listeners.remove(reg.getEntityType(), reg.getInstance());
153 LOG.debug("{}: unregistered listener {}", uuid, reg.getInstance());
158 public String toString() {
159 final ToStringHelper h = MoreObjects.toStringHelper(SimpleDOMEntityOwnershipService.class).add("uuid", uuid);
161 synchronized (entities) {
162 h.add("entities", entities);
164 synchronized (listeners) {
165 h.add("listeners", listeners);
171 private final class EntityRegistration extends AbstractObjectRegistration<DOMEntity> implements
172 DOMEntityOwnershipCandidateRegistration {
173 EntityRegistration(final DOMEntity entity) {
178 protected void removeRegistration() {
179 removeEntity(getInstance());
183 private final class ListenerRegistration extends AbstractObjectRegistration<DOMEntityOwnershipListener>
184 implements DOMEntityOwnershipListenerRegistration {
185 private final String entityType;
187 ListenerRegistration(final String entityType, final DOMEntityOwnershipListener listener) {
189 this.entityType = requireNonNull(entityType);
193 public String getEntityType() {
198 protected void removeRegistration() {
199 unregisterListener(this);