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.ImmutableSet;
17 import com.google.common.collect.Multimap;
18 import java.util.Collection;
19 import java.util.Collections;
21 import java.util.Map.Entry;
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;
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;
40 private BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer = ImmutableBiMap.of();
41 private Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
44 private DOMDataTreeCursorAwareTransaction openTx;
46 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
48 private boolean closed;
51 private ShardedDOMDataTreeListenerContext<?> attachedListener;
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);
61 idToShard = ImmutableMap.copyOf(shardMap);
62 this.subtrees = ImmutableSet.copyOf(subtrees);
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());
74 return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
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());
84 this.idToProducer = mapIdsToProducer(shardToIdentifiers);
85 idToShard = ImmutableMap.copyOf(shardMap);
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);
99 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
103 return idToProducerBuilder.build();
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);
111 this.openTx = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children);
117 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
118 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
119 if (i.contains(subtree)) {
128 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
129 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
130 if (e.getKey().contains(s)) {
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);
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);
150 // Check if part of the requested subtree is not delegated to a child.
151 for (final DOMDataTreeIdentifier c : children.keySet()) {
153 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
158 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
159 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
161 for (final DOMDataTreeIdentifier s : subtrees) {
165 children = cb.build();
169 boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
170 for (final DOMDataTreeIdentifier c : children.keySet()) {
171 if (c.contains(path)) {
180 public synchronized void close() throws DOMDataTreeProducerException {
182 if (openTx != null) {
183 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
187 dataTree.destroyProducer(this);
191 protected Set<DOMDataTreeIdentifier> getSubtrees() {
195 synchronized void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
196 if (!openTx.equals(transaction)) {
197 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
201 LOG.debug("Transaction {} cancelled", transaction);
205 synchronized void transactionSubmitted(final ShardedDOMDataTreeWriteTransaction transaction) {
206 Preconditions.checkState(openTx.equals(transaction));
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;