2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.broker.impl;
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;
21 import java.util.Map.Entry;
22 import java.util.Queue;
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;
38 * Sharded DOM Data Tree Producer.
40 * @deprecated To be removed with {@link ShardedDOMDataTree}.
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;
51 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
53 private DOMDataWriteTransaction openTx;
55 private boolean closed;
57 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
58 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
59 final Set<DOMDataTreeShard> shards) {
60 this.dataTree = Preconditions.checkNotNull(dataTree);
62 // Create shard -> chain map
63 final Builder<DOMDataTreeShard, DOMStoreTransactionChain> cb = ImmutableBiMap.builder();
64 final Queue<Exception> es = new LinkedList<>();
66 for (final DOMDataTreeShard s : shards) {
67 if (s instanceof DOMStore) {
69 final DOMStoreTransactionChain c = ((DOMStore) s).createTransactionChain();
70 LOG.trace("Using DOMStore chain {} to access shard {}", c, s);
72 } catch (final Exception e) {
73 LOG.error("Failed to instantiate chain for shard {}", s, e);
77 LOG.error("Unhandled shard instance type {}", s.getClass());
80 this.shardToChain = cb.build();
82 // An error was encountered, close chains and report the error
83 if (shardToChain.size() != shards.size()) {
84 for (final DOMStoreTransactionChain c : shardToChain.values()) {
87 } catch (final Exception e) {
88 LOG.warn("Exception raised while closing chain {}", c, e);
92 final IllegalStateException e = new IllegalStateException("Failed to completely allocate contexts",
94 while (!es.isEmpty()) {
95 e.addSuppressed(es.poll());
101 idToShard = ImmutableMap.copyOf(shardMap);
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);
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());
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()));
121 final ShardedDOMDataWriteTransaction ret = new ShardedDOMDataWriteTransaction(this, b.build());
127 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
128 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
129 if (i.contains(subtree)) {
138 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier identifier) {
139 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
140 if (e.getKey().contains(identifier)) {
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);
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));
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);
164 // Check if part of the requested subtree is not delegated to a child.
165 for (final DOMDataTreeIdentifier c : children.keySet()) {
167 throw new IllegalArgumentException(
168 String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s,
174 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
175 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
177 for (final DOMDataTreeIdentifier s : subtrees) {
181 children = cb.build();
186 public synchronized void close() throws DOMDataTreeProducerException {
188 if (openTx != null) {
189 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
193 dataTree.destroyProducer(this);
197 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree,
198 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
200 * FIXME: we do not allow multiple multiple shards in a producer because we do not implement the
201 * synchronization primitives yet
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");
208 return new ShardedDOMDataTreeProducer(dataTree, shardMap, shards);
211 Set<DOMDataTreeIdentifier> getSubtrees() {
212 return idToShard.keySet();
215 synchronized void cancelTransaction(final ShardedDOMDataWriteTransaction transaction) {
216 if (!openTx.equals(transaction)) {
217 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
221 LOG.debug("Transaction {} cancelled", transaction);