Execute the ShardedDOMDataTreeTransaction.submit() async.
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTree.java
1 /*
2  * Copyright (c) 2015 Cisco 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.mdsal.dom.broker;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingService;
26 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTable;
27 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTableEntry;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
29 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTreeShardingService {
35     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
36
37     @GuardedBy("this")
38     private final DOMDataTreePrefixTable<ShardRegistration<?>> shards = DOMDataTreePrefixTable.create();
39     @GuardedBy("this")
40     private final DOMDataTreePrefixTable<DOMDataTreeProducer> producers = DOMDataTreePrefixTable.create();
41
42     void removeShard(final ShardRegistration<?> reg) {
43         final DOMDataTreeIdentifier prefix = reg.getPrefix();
44         final ShardRegistration<?> parentReg;
45
46         synchronized (this) {
47             shards.remove(prefix);
48             parentReg = shards.lookup(prefix).getValue();
49
50             /*
51              * FIXME: adjust all producers and listeners. This is tricky, as we need different
52              * locking strategy, simply because we risk AB/BA deadlock with a producer being split
53              * off from a producer.
54              */
55         }
56
57         if (parentReg != null) {
58             parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
59         }
60     }
61
62     @Override
63     public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(final DOMDataTreeIdentifier prefix, final T shard, final DOMDataTreeProducer producer) throws DOMDataTreeShardingConflictException {
64
65         final DOMDataTreeIdentifier firstSubtree = Iterables.getOnlyElement(((ShardedDOMDataTreeProducer) producer).getSubtrees());
66         Preconditions.checkArgument(firstSubtree != null, "Producer that is used to verify namespace claim can only claim a single namespace");
67         Preconditions.checkArgument(prefix.equals(firstSubtree), "Trying to register shard to a different namespace than the producer has claimed");
68
69         final ShardRegistration<T> reg;
70         final ShardRegistration<?> parentReg;
71
72         synchronized (this) {
73             /*
74              * Lookup the parent shard (e.g. the one which currently matches the prefix),
75              * and if it exists, check if its registration prefix does not collide with
76              * this registration.
77              */
78             final DOMDataTreePrefixTableEntry<ShardRegistration<?>> parent = shards.lookup(prefix);
79             if (parent != null) {
80                 parentReg = parent.getValue();
81                 if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
82                     throw new DOMDataTreeShardingConflictException(String.format(
83                             "Prefix %s is already occupied by shard %s", prefix, parentReg.getInstance()));
84                 }
85             } else {
86                 parentReg = null;
87             }
88
89             // FIXME: wrap the shard in a proper adaptor based on implemented interface
90
91             reg = new ShardRegistration<T>(this, prefix, shard);
92
93             shards.store(prefix, reg);
94
95             ((ShardedDOMDataTreeProducer) producer).subshardAdded(Collections.singletonMap(prefix, shard));
96         }
97
98         // Notify the parent shard
99         if (parentReg != null) {
100             parentReg.getInstance().onChildAttached(prefix, shard);
101         }
102
103         return reg;
104     }
105
106     @GuardedBy("this")
107     private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
108
109         final DOMDataTreePrefixTableEntry<DOMDataTreeProducer> producerEntry = producers.lookup(subtree);
110         if (producerEntry != null) {
111             return producerEntry.getValue();
112         }
113         return null;
114     }
115
116     synchronized void destroyProducer(final ShardedDOMDataTreeProducer producer) {
117         for (final DOMDataTreeIdentifier s : producer.getSubtrees()) {
118             producers.remove(s);
119         }
120     }
121
122     @GuardedBy("this")
123     private DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
124         // Record the producer's attachment points
125         final DOMDataTreeProducer ret = ShardedDOMDataTreeProducer.create(this, subtrees, shardMap);
126         for (final DOMDataTreeIdentifier subtree : subtrees) {
127             producers.store(subtree, ret);
128         }
129
130         return ret;
131     }
132
133     @Override
134     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
135         Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
136
137         final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
138         for (final DOMDataTreeIdentifier subtree : subtrees) {
139             // Attempting to create a disconnected producer -- all subtrees have to be unclaimed
140             final DOMDataTreeProducer producer = findProducer(subtree);
141             Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", subtree, producer);
142
143             final DOMDataTreePrefixTableEntry<ShardRegistration<?>> possibleShardReg = shards.lookup(subtree);
144             if (possibleShardReg != null && possibleShardReg.getValue() != null) {
145                 shardMap.put(subtree, possibleShardReg.getValue().getInstance());
146             }
147         }
148
149         return createProducer(subtrees, shardMap);
150     }
151
152     synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent, final Collection<DOMDataTreeIdentifier> subtrees) {
153         Preconditions.checkNotNull(parent);
154
155         final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
156         for (final DOMDataTreeIdentifier s : subtrees) {
157             shardMap.put(s, shards.lookup(s).getValue().getInstance());
158         }
159
160         return createProducer(subtrees, shardMap);
161     }
162
163     @Override
164     public synchronized <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener,
165             final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges,
166             final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
167         Preconditions.checkNotNull(listener, "listener");
168         Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees must not be empty.");
169         final ShardedDOMDataTreeListenerContext<T> listenerContext =
170                 ShardedDOMDataTreeListenerContext.create(listener, subtrees, allowRxMerges);
171         try {
172             // FIXME: Add attachment of producers
173             for (final DOMDataTreeProducer producer : producers) {
174                 Preconditions.checkArgument(producer instanceof ShardedDOMDataTreeProducer);
175                 final ShardedDOMDataTreeProducer castedProducer = ((ShardedDOMDataTreeProducer) producer);
176                 simpleLoopCheck(subtrees, castedProducer.getSubtrees());
177                 // FIXME: We should also unbound listeners
178                 castedProducer.boundToListener(listenerContext);
179             }
180
181             for (final DOMDataTreeIdentifier subtree : subtrees) {
182                 final DOMDataTreeShard shard = shards.lookup(subtree).getValue().getInstance();
183                 // FIXME: What should we do if listener is wildcard? And shards are on per
184                 // node basis?
185                 Preconditions.checkArgument(shard instanceof DOMStoreTreeChangePublisher,
186                         "Subtree %s does not point to listenable subtree.", subtree);
187
188                 listenerContext.register(subtree, (DOMStoreTreeChangePublisher) shard);
189             }
190         } catch (final Exception e) {
191             listenerContext.close();
192             throw e;
193         }
194         return new AbstractListenerRegistration<T>(listener) {
195             @Override
196             protected void removeRegistration() {
197                 ShardedDOMDataTree.this.removeListener(listenerContext);
198             }
199         };
200     }
201
202     private static void simpleLoopCheck(final Collection<DOMDataTreeIdentifier> listen, final Set<DOMDataTreeIdentifier> writes)
203             throws DOMDataTreeLoopException {
204         for(final DOMDataTreeIdentifier listenPath : listen) {
205             for (final DOMDataTreeIdentifier writePath : writes) {
206                 if (listenPath.contains(writePath)) {
207                     throw new DOMDataTreeLoopException(String.format(
208                             "Listener must not listen on parent (%s), and also writes child (%s)", listenPath,
209                             writePath));
210                 } else if (writePath.contains(listenPath)) {
211                     throw new DOMDataTreeLoopException(
212                             String.format("Listener must not write parent (%s), and also listen on child (%s)",
213                                     writePath, listenPath));
214                 }
215             }
216         }
217     }
218
219     void removeListener(final ShardedDOMDataTreeListenerContext<?> listener) {
220         // FIXME: detach producers
221         listener.close();
222     }
223 }