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 org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED;
11 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import com.google.common.base.Preconditions;
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 java.util.Collection;
23 import java.util.Optional;
24 import java.util.UUID;
25 import javax.annotation.concurrent.GuardedBy;
26 import org.kohsuke.MetaInfServices;
27 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
28 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
29 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
30 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
31 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
32 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
33 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
34 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
35 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
36 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Simple {@link DOMEntityOwnershipService} operating as an isolated island. It has no awareness of the world outside
45 * @author Robert Varga
48 public final class SimpleDOMEntityOwnershipService implements DOMEntityOwnershipService {
49 private static final Logger LOG = LoggerFactory.getLogger(SimpleDOMEntityOwnershipService.class);
51 @GuardedBy("entities")
52 private final Table<String, YangInstanceIdentifier, DOMEntity> entities = HashBasedTable.create();
54 @GuardedBy("listeners")
55 private final Multimap<String, DOMEntityOwnershipListener> listeners = ArrayListMultimap.create(0, 1);
57 private final UUID uuid;
60 SimpleDOMEntityOwnershipService(final UUID uuid) {
61 this.uuid = Preconditions.checkNotNull(uuid);
64 public SimpleDOMEntityOwnershipService() {
65 this(UUID.randomUUID());
69 public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
70 throws CandidateAlreadyRegisteredException {
71 synchronized (entities) {
72 final DOMEntity prev = entities.get(entity.getType(), entity.getIdentifier());
74 throw new CandidateAlreadyRegisteredException(prev);
77 entities.put(entity.getType(), entity.getIdentifier(), entity);
78 LOG.debug("{}: registered candidate {}", uuid, entity);
81 notifyListeners(entity, LOCAL_OWNERSHIP_GRANTED);
82 return new EntityRegistration(entity);
86 public DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
87 final DOMEntityOwnershipListener listener) {
89 final Collection<DOMEntity> owned;
90 synchronized (entities) {
91 owned = ImmutableList.copyOf(entities.row(entityType).values());
92 LOG.trace("{}: acquired candidates {} for new listener {}", uuid, owned, listener);
95 synchronized (listeners) {
96 listeners.put(entityType, listener);
99 for (DOMEntity entity : owned) {
100 notifyListener(listener, new DOMEntityOwnershipChange(entity, LOCAL_OWNERSHIP_GRANTED));
102 LOG.debug("{}: registered listener {}", uuid, listener);
103 return new ListenerRegistration(entityType, listener);
107 public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
108 return isCandidateRegistered(forEntity) ? Optional.of(EntityOwnershipState.IS_OWNER) : Optional.empty();
112 public boolean isCandidateRegistered(final DOMEntity forEntity) {
113 synchronized (entities) {
114 return entities.contains(forEntity.getType(), forEntity.getIdentifier());
118 private void removeEntity(final DOMEntity entity) {
119 synchronized (entities) {
120 entities.remove(entity.getType(), entity.getIdentifier());
121 LOG.debug("{}: unregistered candidate {}", uuid, entity);
124 notifyListeners(entity, LOCAL_OWNERSHIP_LOST_NO_OWNER);
127 @SuppressWarnings("checkstyle:illegalCatch")
128 private void notifyListener(final DOMEntityOwnershipListener listener, final DOMEntityOwnershipChange change) {
130 LOG.trace("{} notifying listener {} change {}", uuid, listener, change);
131 listener.ownershipChanged(change);
132 } catch (RuntimeException e) {
133 LOG.warn("{}: Listener {} change {} failed", uuid, listener, change, e);
137 private void notifyListeners(final DOMEntity entity, final EntityOwnershipChangeState state) {
138 final DOMEntityOwnershipChange change = new DOMEntityOwnershipChange(entity, state);
140 final Collection<DOMEntityOwnershipListener> snap;
142 synchronized (listeners) {
143 snap = ImmutableList.copyOf(listeners.get(entity.getType()));
146 for (DOMEntityOwnershipListener listener : snap) {
147 notifyListener(listener, change);
151 void unregisterListener(final ListenerRegistration reg) {
152 synchronized (listeners) {
153 listeners.remove(reg.getEntityType(), reg.getInstance());
154 LOG.debug("{}: unregistered listener {}", uuid, reg.getInstance());
159 public String toString() {
160 final ToStringHelper h = MoreObjects.toStringHelper(SimpleDOMEntityOwnershipService.class).add("uuid", uuid);
162 synchronized (entities) {
163 h.add("entities", entities);
165 synchronized (listeners) {
166 h.add("listeners", listeners);
172 private final class EntityRegistration extends AbstractObjectRegistration<DOMEntity> implements
173 DOMEntityOwnershipCandidateRegistration {
174 EntityRegistration(final DOMEntity entity) {
179 protected void removeRegistration() {
180 removeEntity(getInstance());
184 private final class ListenerRegistration extends AbstractObjectRegistration<DOMEntityOwnershipListener>
185 implements DOMEntityOwnershipListenerRegistration {
186 private final String entityType;
188 ListenerRegistration(final String entityType, final DOMEntityOwnershipListener listener) {
190 this.entityType = Preconditions.checkNotNull(entityType);
194 public String getEntityType() {
199 protected void removeRegistration() {
200 unregisterListener(this);