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