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.mdsal.dom.broker;
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;
20 import java.util.Map.Entry;
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;
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;
39 private final BiMap<DOMDataTreeShard, DOMDataTreeShardProducer> shardToProducer;
40 private final BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer;
43 private DOMDataTreeCursorAwareTransaction openTx;
45 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
47 private boolean closed;
50 private ShardedDOMDataTreeListenerContext<?> attachedListener;
52 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
53 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
54 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
55 this.dataTree = Preconditions.checkNotNull(dataTree);
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);
65 // id mapped to producers
66 for (final DOMDataTreeIdentifier id : entry.getValue()) {
67 idToProducerBuilder.put(id, producer);
70 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
73 this.shardToProducer = builder.build();
74 this.idToProducer = idToProducerBuilder.build();
75 idToShard = ImmutableMap.copyOf(shardMap);
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());
85 return new ShardedDOMDataTreeProducer(dataTree, shardMap, shardToIdetifiers);
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);
93 this.openTx = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children);
99 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
100 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
101 if (i.contains(subtree)) {
110 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
111 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
112 if (e.getKey().contains(s)) {
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);
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);
132 // Check if part of the requested subtree is not delegated to a child.
133 for (final DOMDataTreeIdentifier c : children.keySet()) {
135 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
140 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
141 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
143 for (final DOMDataTreeIdentifier s : subtrees) {
147 children = cb.build();
151 boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
152 for (final DOMDataTreeIdentifier c : children.keySet()) {
153 if (c.contains(path)) {
162 public synchronized void close() throws DOMDataTreeProducerException {
164 if (openTx != null) {
165 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
169 dataTree.destroyProducer(this);
173 Set<DOMDataTreeIdentifier> getSubtrees() {
174 return idToShard.keySet();
177 synchronized void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
178 if (!openTx.equals(transaction)) {
179 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
183 LOG.debug("Transaction {} cancelled", transaction);
187 synchronized void transactionSubmitted(final ShardedDOMDataTreeWriteTransaction transaction) {
188 Preconditions.checkState(openTx.equals(transaction));
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;