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