2 * Copyright (c) 2015 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.dom.broker;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.HashMap;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingService;
24 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTable;
25 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTableEntry;
26 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
27 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTreeShardingService {
33 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
36 private final DOMDataTreePrefixTable<ShardRegistration<?>> shards = DOMDataTreePrefixTable.create();
38 private final DOMDataTreePrefixTable<DOMDataTreeProducer> producers = DOMDataTreePrefixTable.create();
41 void removeShard(final ShardRegistration<?> reg) {
42 final DOMDataTreeIdentifier prefix = reg.getPrefix();
43 final ShardRegistration<?> parentReg;
46 shards.remove(prefix);
47 parentReg = shards.lookup(prefix).getValue();
50 * FIXME: adjust all producers and listeners. This is tricky, as we need different
51 * locking strategy, simply because we risk AB/BA deadlock with a producer being split
52 * off from a producer.
56 if (parentReg != null) {
57 parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
62 public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(final DOMDataTreeIdentifier prefix, final T shard) throws DOMDataTreeShardingConflictException {
63 final ShardRegistration<T> reg;
64 final ShardRegistration<?> parentReg;
68 * Lookup the parent shard (e.g. the one which currently matches the prefix),
69 * and if it exists, check if its registration prefix does not collide with
72 final DOMDataTreePrefixTableEntry<ShardRegistration<?>> parent = shards.lookup(prefix);
74 parentReg = parent.getValue();
75 if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
76 throw new DOMDataTreeShardingConflictException(String.format(
77 "Prefix %s is already occupied by shard %s", prefix, parentReg.getInstance()));
83 // FIXME: wrap the shard in a proper adaptor based on implemented interface
85 reg = new ShardRegistration<T>(this, prefix, shard);
87 shards.store(prefix, reg);
89 // FIXME: update any producers/registrations
92 // Notify the parent shard
93 if (parentReg != null) {
94 parentReg.getInstance().onChildAttached(prefix, shard);
101 private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
103 DOMDataTreePrefixTableEntry<DOMDataTreeProducer> producerEntry = producers.lookup(subtree);
104 if (producerEntry != null) {
105 return producerEntry.getValue();
110 synchronized void destroyProducer(final ShardedDOMDataTreeProducer producer) {
111 for (final DOMDataTreeIdentifier s : producer.getSubtrees()) {
117 private DOMDataTreeProducer createProducer(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
118 // Record the producer's attachment points
119 final DOMDataTreeProducer ret = ShardedDOMDataTreeProducer.create(this, shardMap);
120 for (final DOMDataTreeIdentifier s : shardMap.keySet()) {
121 producers.store(s, ret);
128 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
129 Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
131 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
132 for (final DOMDataTreeIdentifier s : subtrees) {
133 // Attempting to create a disconnected producer -- all subtrees have to be unclaimed
134 final DOMDataTreeProducer producer = findProducer(s);
135 Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", s, producer);
137 shardMap.put(s, shards.lookup(s).getValue().getInstance());
140 return createProducer(shardMap);
143 synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent, final Collection<DOMDataTreeIdentifier> subtrees) {
144 Preconditions.checkNotNull(parent);
146 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
147 for (final DOMDataTreeIdentifier s : subtrees) {
148 shardMap.put(s, shards.lookup(s).getValue().getInstance());
151 return createProducer(shardMap);
155 public synchronized <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener,
156 final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges,
157 final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
158 Preconditions.checkNotNull(listener, "listener");
159 Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees must not be empty.");
160 final ShardedDOMDataTreeListenerContext<T> listenerContext =
161 ShardedDOMDataTreeListenerContext.create(listener, subtrees, allowRxMerges);
163 // FIXME: Add attachment of producers
164 for (DOMDataTreeProducer producer : producers) {
165 Preconditions.checkArgument(producer instanceof ShardedDOMDataTreeProducer);
166 ShardedDOMDataTreeProducer castedProducer = ((ShardedDOMDataTreeProducer) producer);
167 simpleLoopCheck(subtrees, castedProducer.getSubtrees());
168 // FIXME: We should also unbound listeners
169 castedProducer.boundToListener(listenerContext);
172 for (DOMDataTreeIdentifier subtree : subtrees) {
173 DOMDataTreeShard shard = shards.lookup(subtree).getValue().getInstance();
174 // FIXME: What should we do if listener is wildcard? And shards are on per
176 Preconditions.checkArgument(shard instanceof DOMStoreTreeChangePublisher,
177 "Subtree %s does not point to listenable subtree.", subtree);
179 listenerContext.register(subtree, (DOMStoreTreeChangePublisher) shard);
181 } catch (Exception e) {
182 listenerContext.close();
185 return new AbstractListenerRegistration<T>(listener) {
187 protected void removeRegistration() {
188 ShardedDOMDataTree.this.removeListener(listenerContext);
193 private static void simpleLoopCheck(Collection<DOMDataTreeIdentifier> listen, Set<DOMDataTreeIdentifier> writes)
194 throws DOMDataTreeLoopException {
195 for(DOMDataTreeIdentifier listenPath : listen) {
196 for (DOMDataTreeIdentifier writePath : writes) {
197 if (listenPath.contains(writePath)) {
198 throw new DOMDataTreeLoopException(String.format(
199 "Listener must not listen on parent (%s), and also writes child (%s)", listenPath,
201 } else if (writePath.contains(listenPath)) {
202 throw new DOMDataTreeLoopException(
203 String.format("Listener must not write parent (%s), and also listen on child (%s)",
204 writePath, listenPath));
210 void removeListener(ShardedDOMDataTreeListenerContext<?> listener) {
211 // FIXME: detach producers