BUG 6057: Rewrite ShardedDOMProducer to use new cursor api
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeProducer.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.ArrayListMultimap;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.ImmutableBiMap;
14 import com.google.common.collect.ImmutableBiMap.Builder;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Multimap;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
29 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
30 import org.opendaylight.mdsal.dom.store.inmemory.WriteableDOMDataTreeShard;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 final class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
35     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
36     private final Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
37     private final ShardedDOMDataTree dataTree;
38
39     private final BiMap<DOMDataTreeShard, DOMDataTreeShardProducer> shardToProducer;
40     private final BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer;
41
42     @GuardedBy("this")
43     private DOMDataTreeCursorAwareTransaction openTx;
44     @GuardedBy("this")
45     private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
46     @GuardedBy("this")
47     private boolean closed;
48
49     @GuardedBy("this")
50     private ShardedDOMDataTreeListenerContext<?> attachedListener;
51
52     ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
53                                final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
54                                final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
55         this.dataTree = Preconditions.checkNotNull(dataTree);
56
57         final Builder<DOMDataTreeShard, DOMDataTreeShardProducer> builder = ImmutableBiMap.builder();
58         final Builder<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducerBuilder = ImmutableBiMap.builder();
59         for (final Entry<DOMDataTreeShard, Collection<DOMDataTreeIdentifier>> entry : shardToId.asMap().entrySet()) {
60             if (entry.getKey() instanceof WriteableDOMDataTreeShard) {
61                 //create a single producer for all prefixes in a single shard
62                 final DOMDataTreeShardProducer producer = ((WriteableDOMDataTreeShard) entry.getKey()).createProducer(entry.getValue());
63                 builder.put(entry.getKey(), producer);
64
65                 // id mapped to producers
66                 for (final DOMDataTreeIdentifier id : entry.getValue()) {
67                     idToProducerBuilder.put(id, producer);
68                 }
69             } else {
70                 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
71             }
72         }
73         this.shardToProducer = builder.build();
74         this.idToProducer = idToProducerBuilder.build();
75         idToShard = ImmutableMap.copyOf(shardMap);
76     }
77
78     static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
79         final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdetifiers = ArrayListMultimap.create();
80         // map which identifier belongs to which shard
81         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
82             shardToIdetifiers.put(entry.getValue(), entry.getKey());
83         }
84
85         return new ShardedDOMDataTreeProducer(dataTree, shardMap, shardToIdetifiers);
86     }
87
88     @Override
89     public synchronized DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
90         Preconditions.checkState(!closed, "Producer is already closed");
91         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
92
93         this.openTx = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children);
94
95         return openTx;
96     }
97
98     @GuardedBy("this")
99     private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
100         for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
101             if (i.contains(subtree)) {
102                 return true;
103             }
104         }
105
106         return false;
107     }
108
109     @GuardedBy("this")
110     private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
111         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
112             if (e.getKey().contains(s)) {
113                 return e.getValue();
114             }
115         }
116
117         return null;
118     }
119
120     @Override
121     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
122         Preconditions.checkState(!closed, "Producer is already closed");
123         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
124
125         for (final DOMDataTreeIdentifier s : subtrees) {
126             // Check if the subtree was visible at any time
127             Preconditions.checkArgument(haveSubtree(s), "Subtree %s was never available in producer %s", s, this);
128             // Check if the subtree has not been delegated to a child
129             final DOMDataTreeProducer child = lookupChild(s);
130             Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
131
132             // Check if part of the requested subtree is not delegated to a child.
133             for (final DOMDataTreeIdentifier c : children.keySet()) {
134                 if (s.contains(c)) {
135                     throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
136                 }
137             }
138         }
139
140         final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
141         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
142         cb.putAll(children);
143         for (final DOMDataTreeIdentifier s : subtrees) {
144             cb.put(s, ret);
145         }
146
147         children = cb.build();
148         return ret;
149     }
150
151     boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
152         for (final DOMDataTreeIdentifier c : children.keySet()) {
153             if (c.contains(path)) {
154                 return true;
155             }
156         }
157         return false;
158     }
159
160
161     @Override
162     public synchronized void close() throws DOMDataTreeProducerException {
163         if (!closed) {
164             if (openTx != null) {
165                 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
166             }
167
168             closed = true;
169             dataTree.destroyProducer(this);
170         }
171     }
172
173     Set<DOMDataTreeIdentifier> getSubtrees() {
174         return idToShard.keySet();
175     }
176
177     synchronized void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
178         if (!openTx.equals(transaction)) {
179             LOG.warn("Transaction {} is not open in producer {}", transaction, this);
180             return;
181         }
182
183         LOG.debug("Transaction {} cancelled", transaction);
184         openTx = null;
185     }
186
187     synchronized void transactionSubmitted(final ShardedDOMDataTreeWriteTransaction transaction) {
188         Preconditions.checkState(openTx.equals(transaction));
189         openTx = null;
190     }
191
192     synchronized void boundToListener(final ShardedDOMDataTreeListenerContext<?> listener) {
193         // FIXME: Add option to dettach
194         Preconditions.checkState(this.attachedListener == null,
195                 "Producer %s is already attached to other listener.",
196                 listener.getListener());
197         this.attachedListener = listener;
198     }
199 }