2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
11 import com.google.common.annotations.Beta;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Iterator;
19 import java.util.Map.Entry;
20 import java.util.concurrent.ExecutorService;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
26 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTable;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
37 public class InMemoryDOMDataTreeShard implements ReadableWriteableDOMDataTreeShard, SchemaContextListener {
39 private static final class SubshardProducerSpecification {
40 private final Collection<DOMDataTreeIdentifier> prefixes = new ArrayList<>(1);
41 private final ChildShardContext shard;
42 SubshardProducerSpecification(final ChildShardContext subshard) {
43 this.shard = Preconditions.checkNotNull(subshard);
46 void addPrefix(final DOMDataTreeIdentifier prefix) {
50 DOMDataTreeShardProducer createProducer() {
51 return shard.getShard().createProducer(prefixes);
54 public DOMDataTreeIdentifier getPrefix() {
55 return shard.getPrefix();
59 private final DOMDataTreePrefixTable<ChildShardContext> childShardsTable = DOMDataTreePrefixTable.create();
61 private final Map<DOMDataTreeIdentifier, ChildShardContext> childShards = new HashMap<>();
63 private final DOMDataTreeIdentifier prefix;
64 private final DataTree dataTree;
66 private SchemaContext schemaContext;
67 private InMemoryDOMDataTreeShardChangePublisher shardChangePublisher;
69 private InMemoryDOMDataTreeShard(final DOMDataTreeIdentifier prefix, final ExecutorService dataTreeChangeExecutor,
70 final int maxDataChangeListenerQueueSize) {
71 this.prefix = Preconditions.checkNotNull(prefix);
73 final TreeType treeType = treeTypeFor(prefix.getDatastoreType());
74 this.dataTree = InMemoryDataTreeFactory.getInstance().create(treeType, prefix.getRootIdentifier());
76 this.shardChangePublisher = new InMemoryDOMDataTreeShardChangePublisher(dataTreeChangeExecutor, maxDataChangeListenerQueueSize, dataTree, prefix.getRootIdentifier(), childShards);
79 public static InMemoryDOMDataTreeShard create(final DOMDataTreeIdentifier id, final ExecutorService dataTreeChangeExecutor,
80 final int maxDataChangeListenerQueueSize) {
81 return new InMemoryDOMDataTreeShard(id, dataTreeChangeExecutor, maxDataChangeListenerQueueSize);
85 public void onGlobalContextUpdated(final SchemaContext context) {
86 dataTree.setSchemaContext(context);
87 schemaContext = context;
91 public void onChildAttached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
92 Preconditions.checkArgument(child != this, "Attempted to attach child %s onto self", this);
93 reparentChildShards(prefix, child);
94 addChildShard(prefix, child);
98 public void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
99 childShards.remove(prefix);
103 public InMemoryDOMDataTreeShardProducer createProducer(final Collection<DOMDataTreeIdentifier> prefixes) {
104 for (DOMDataTreeIdentifier prodPrefix : prefixes) {
105 Preconditions.checkArgument(prefix.contains(prodPrefix), "Prefix %s is not contained under shart root",
108 return new InMemoryDOMDataTreeShardProducer(this, prefixes);
113 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(@Nonnull final YangInstanceIdentifier treeId, @Nonnull final L listener) {
114 return shardChangePublisher.registerTreeChangeListener(treeId, listener);
117 private void addChildShard(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
118 ChildShardContext context = createContextFor(prefix, child);
119 childShards.put(prefix, context);
120 childShardsTable.store(prefix, context);
123 private void reparentChildShards(final DOMDataTreeIdentifier newChildPrefix, final DOMDataTreeShard newChild) {
124 Iterator<Entry<DOMDataTreeIdentifier, ChildShardContext>> actualChildren = childShards.entrySet().iterator();
125 Map<DOMDataTreeIdentifier, ChildShardContext> reparented = new HashMap<>();
126 while (actualChildren.hasNext()) {
127 final Entry<DOMDataTreeIdentifier, ChildShardContext> actualChild = actualChildren.next();
128 final DOMDataTreeIdentifier actualPrefix = actualChild.getKey();
129 Preconditions.checkArgument(!newChildPrefix.equals(actualPrefix),
130 "Child shard with prefix %s already attached", newChildPrefix);
131 if (newChildPrefix.contains(actualPrefix)) {
132 ChildShardContext actualContext = actualChild.getValue();
133 actualChildren.remove();
134 newChild.onChildAttached(actualPrefix, actualContext.getShard());
135 reparented.put(actualChild.getKey(), actualContext);
136 childShardsTable.remove(actualPrefix);
139 updateProducersAndListeners(reparented);
142 private void updateProducersAndListeners(final Map<DOMDataTreeIdentifier, ChildShardContext> reparented) {
143 // FIXME: remove reparenting of producers, shards have to be registered from top to bottom
144 if (reparented.isEmpty()) {
145 //nothing was reparented no need to update anything
148 throw new UnsupportedOperationException();
151 private static ChildShardContext createContextFor(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
152 Preconditions.checkArgument(child instanceof WriteableDOMDataTreeShard,
153 "Child %s is not a writable shared", child);
154 return new ChildShardContext(prefix, (WriteableDOMDataTreeShard) child);
157 private static TreeType treeTypeFor(final LogicalDatastoreType dsType) {
160 return TreeType.CONFIGURATION;
162 return TreeType.OPERATIONAL;
164 throw new IllegalArgumentException("Unsupported Data Store type:" + dsType);
169 Map<DOMDataTreeIdentifier, DOMDataTreeShard> getChildShards() {
170 Map<DOMDataTreeIdentifier, DOMDataTreeShard> ret = new HashMap<>();
171 for (Entry<DOMDataTreeIdentifier, ChildShardContext> entry : childShards.entrySet()) {
172 ret.put(entry.getKey(), entry.getValue().getShard());
177 InmemoryDOMDataTreeShardWriteTransaction createTransaction(final InmemoryDOMDataTreeShardWriteTransaction previousTx) {
178 // FIXME: implement this
179 throw new UnsupportedOperationException();
182 InmemoryDOMDataTreeShardWriteTransaction createTransaction(final Collection<DOMDataTreeIdentifier> prefixes) {
184 Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
185 for (DOMDataTreeIdentifier producerPrefix : prefixes) {
186 for (ChildShardContext maybeAffected : childShards.values()) {
187 final DOMDataTreeIdentifier bindPath;
188 if (producerPrefix.contains(maybeAffected.getPrefix())) {
189 bindPath = maybeAffected.getPrefix();
190 } else if (maybeAffected.getPrefix().contains(producerPrefix)) {
191 // Bound path is inside subshard
192 bindPath = producerPrefix;
197 SubshardProducerSpecification spec = affectedSubshards.get(maybeAffected.getPrefix());
199 spec = new SubshardProducerSpecification(maybeAffected);
200 affectedSubshards.put(maybeAffected.getPrefix(), spec);
202 spec.addPrefix(bindPath);
206 ShardRootModificationContext rootContext = new ShardRootModificationContext(prefix,
207 (CursorAwareDataTreeSnapshot) dataTree.takeSnapshot());
208 ShardDataModificationBuilder builder = new ShardDataModificationBuilder(rootContext);
209 for (SubshardProducerSpecification spec : affectedSubshards.values()) {
210 ForeignShardModificationContext foreignContext =
211 new ForeignShardModificationContext(spec.getPrefix(), spec.createProducer());
212 builder.addSubshard(foreignContext);
213 builder.addSubshard(spec.getPrefix(), foreignContext);
216 return new InmemoryDOMDataTreeShardWriteTransaction(builder.build(), dataTree, shardChangePublisher);