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.cluster.datastore;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Strings;
13 import java.util.AbstractMap.SimpleEntry;
14 import java.util.HashMap;
16 import java.util.Map.Entry;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent;
22 import org.opendaylight.controller.md.sal.dom.store.impl.ResolveDataChangeEventsTask;
23 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Internal shard state, similar to a DOMStore, but optimized for use in the actor system,
39 * e.g. it does not expose public interfaces and assumes it is only ever called from a
42 * This class is not part of the API contract and is subject to change at any time.
46 public final class ShardDataTree extends ShardDataTreeTransactionParent {
47 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
48 private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager();
49 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
50 private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher();
51 private final ListenerTree listenerTree = ListenerTree.create();
52 private final TipProducingDataTree dataTree;
54 ShardDataTree(final SchemaContext schemaContext) {
55 dataTree = InMemoryDataTreeFactory.getInstance().create();
56 if (schemaContext != null) {
57 dataTree.setSchemaContext(schemaContext);
61 TipProducingDataTree getDataTree() {
65 void updateSchemaContext(final SchemaContext schemaContext) {
66 dataTree.setSchemaContext(schemaContext);
69 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
70 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
72 chain = new ShardDataTreeTransactionChain(chainId, this);
73 transactionChains.put(chainId, chain);
79 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
80 if (Strings.isNullOrEmpty(chainId)) {
81 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
84 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
87 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
88 if (Strings.isNullOrEmpty(chainId)) {
89 return new ReadWriteShardDataTreeTransaction(this, txId, dataTree.takeSnapshot().newModification());
92 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
95 void notifyListeners(final DataTreeCandidate candidate) {
96 LOG.debug("Notifying listeners on candidate {}", candidate);
98 // DataTreeChanges first, as they are more light-weight
99 treeChangePublisher.publishChanges(candidate);
101 // DataChanges second, as they are heavier
102 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
105 void closeAllTransactionChains() {
106 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
110 transactionChains.clear();
113 void closeTransactionChain(final String transactionChainId) {
114 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
118 LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
122 Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> registerChangeListener(
123 final YangInstanceIdentifier path,
124 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final DataChangeScope scope) {
125 final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
126 listenerTree.registerDataChangeListener(path, listener, scope);
128 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
129 final DOMImmutableDataChangeEvent event;
130 if (currentState.isPresent()) {
131 final NormalizedNode<?, ?> data = currentState.get();
132 event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE).setAfter(data).addCreated(path, data).build();
137 return new SimpleEntry<>(reg, event);
140 Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
141 final DOMDataTreeChangeListener listener) {
142 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
144 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
145 final DataTreeCandidate event;
146 if (currentState.isPresent()) {
147 event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
151 return new SimpleEntry<>(reg, event);
154 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
155 LOG.debug("Applying foreign transaction {}", identifier);
157 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
158 DataTreeCandidates.applyToModification(mod, foreign);
161 LOG.trace("Applying foreign modification {}", mod);
162 dataTree.validate(mod);
163 final DataTreeCandidate candidate = dataTree.prepare(mod);
164 dataTree.commit(candidate);
165 notifyListeners(candidate);
169 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
174 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
175 final DataTreeModification snapshot = transaction.getSnapshot();
177 return new SimpleShardDataTreeCohort(this, snapshot);