89c9969d5e1e5179dccb4abd956e188588f57a08
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / 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.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.BiMap;
12 import com.google.common.collect.ImmutableBiMap;
13 import com.google.common.collect.ImmutableBiMap.Builder;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.LinkedList;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Queue;
23 import java.util.Set;
24 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeProducer;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeProducerBusyException;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeProducerException;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeShard;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Sharded DOM Data Tree Producer.
39  *
40  * @deprecated To be removed with {@link ShardedDOMDataTree}.
41  */
42 @Deprecated
43 @SuppressWarnings("checkstyle:IllegalCatch")
44 final class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
45     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
46     private final BiMap<DOMDataTreeShard, DOMStoreTransactionChain> shardToChain;
47     private final Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
48     private final ShardedDOMDataTree dataTree;
49
50     @GuardedBy("this")
51     private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
52     @GuardedBy("this")
53     private DOMDataWriteTransaction openTx;
54     @GuardedBy("this")
55     private boolean closed;
56
57     ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
58                                final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
59                                final Set<DOMDataTreeShard> shards) {
60         this.dataTree = Preconditions.checkNotNull(dataTree);
61
62         // Create shard -> chain map
63         final Builder<DOMDataTreeShard, DOMStoreTransactionChain> cb = ImmutableBiMap.builder();
64         final Queue<Exception> es = new LinkedList<>();
65
66         for (final DOMDataTreeShard s : shards) {
67             if (s instanceof DOMStore) {
68                 try {
69                     final DOMStoreTransactionChain c = ((DOMStore) s).createTransactionChain();
70                     LOG.trace("Using DOMStore chain {} to access shard {}", c, s);
71                     cb.put(s, c);
72                 } catch (final Exception e) {
73                     LOG.error("Failed to instantiate chain for shard {}", s, e);
74                     es.add(e);
75                 }
76             } else {
77                 LOG.error("Unhandled shard instance type {}", s.getClass());
78             }
79         }
80         this.shardToChain = cb.build();
81
82         // An error was encountered, close chains and report the error
83         if (shardToChain.size() != shards.size()) {
84             for (final DOMStoreTransactionChain c : shardToChain.values()) {
85                 try {
86                     c.close();
87                 } catch (final Exception e) {
88                     LOG.warn("Exception raised while closing chain {}", c, e);
89                 }
90             }
91
92             final IllegalStateException e = new IllegalStateException("Failed to completely allocate contexts",
93                                                                       es.poll());
94             while (!es.isEmpty()) {
95                 e.addSuppressed(es.poll());
96             }
97
98             throw e;
99         }
100
101         idToShard = ImmutableMap.copyOf(shardMap);
102     }
103
104     @Override
105     public synchronized DOMDataWriteTransaction createTransaction(final boolean isolated) {
106         Preconditions.checkState(!closed, "Producer is already closed");
107         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
108
109         // Allocate backing transactions
110         final Map<DOMDataTreeShard, DOMStoreWriteTransaction> shardToTx = new HashMap<>();
111         for (final Entry<DOMDataTreeShard, DOMStoreTransactionChain> e : shardToChain.entrySet()) {
112             shardToTx.put(e.getKey(), e.getValue().newWriteOnlyTransaction());
113         }
114
115         // Create the ID->transaction map
116         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMStoreWriteTransaction> b = ImmutableMap.builder();
117         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> e : idToShard.entrySet()) {
118             b.put(e.getKey(), shardToTx.get(e.getValue()));
119         }
120
121         final ShardedDOMDataWriteTransaction ret = new ShardedDOMDataWriteTransaction(this, b.build());
122         openTx = ret;
123         return ret;
124     }
125
126     @GuardedBy("this")
127     private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
128         for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
129             if (i.contains(subtree)) {
130                 return true;
131             }
132         }
133
134         return false;
135     }
136
137     @GuardedBy("this")
138     private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier identifier) {
139         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
140             if (e.getKey().contains(identifier)) {
141                 return e.getValue();
142             }
143         }
144
145         return null;
146     }
147
148     @Override
149     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
150         Preconditions.checkState(!closed, "Producer is already closed");
151         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
152
153         for (final DOMDataTreeIdentifier s : subtrees) {
154             // Check if the subtree was visible at any time
155             if (!haveSubtree(s)) {
156                 throw new IllegalArgumentException(
157                         String.format("Subtree %s was never available in producer %s", s, this));
158             }
159
160             // Check if the subtree has not been delegated to a child
161             final DOMDataTreeProducer child = lookupChild(s);
162             Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
163
164             // Check if part of the requested subtree is not delegated to a child.
165             for (final DOMDataTreeIdentifier c : children.keySet()) {
166                 if (s.contains(c)) {
167                     throw new IllegalArgumentException(
168                             String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s,
169                                           c));
170                 }
171             }
172         }
173
174         final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
175         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
176         cb.putAll(children);
177         for (final DOMDataTreeIdentifier s : subtrees) {
178             cb.put(s, ret);
179         }
180
181         children = cb.build();
182         return ret;
183     }
184
185     @Override
186     public synchronized void close() throws DOMDataTreeProducerException {
187         if (!closed) {
188             if (openTx != null) {
189                 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
190             }
191
192             closed = true;
193             dataTree.destroyProducer(this);
194         }
195     }
196
197     static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree,
198                                       final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
199         /*
200          * FIXME: we do not allow multiple multiple shards in a producer because we do not implement the
201          *        synchronization primitives yet
202          */
203         final Set<DOMDataTreeShard> shards = ImmutableSet.copyOf(shardMap.values());
204         if (shards.size() > 1) {
205             throw new UnsupportedOperationException("Cross-shard producers are not supported yet");
206         }
207
208         return new ShardedDOMDataTreeProducer(dataTree, shardMap, shards);
209     }
210
211     Set<DOMDataTreeIdentifier> getSubtrees() {
212         return idToShard.keySet();
213     }
214
215     synchronized void cancelTransaction(final ShardedDOMDataWriteTransaction transaction) {
216         if (!openTx.equals(transaction)) {
217             LOG.warn("Transaction {} is not open in producer {}", transaction, this);
218             return;
219         }
220
221         LOG.debug("Transaction {} cancelled", transaction);
222         openTx = null;
223     }
224 }