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.base.Optional;
11 import com.google.common.base.Preconditions;
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.DataChangeListenerRegistration;
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.DataTreeCandidateTip;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
35 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Internal shard state, similar to a DOMStore, but optimized for use in the actor system,
42 * e.g. it does not expose public interfaces and assumes it is only ever called from a
45 * This class is not part of the API contract and is subject to change at any time.
48 public class ShardDataTree extends ShardDataTreeTransactionParent {
49 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
50 private static final YangInstanceIdentifier ROOT_PATH = YangInstanceIdentifier.builder().build();
51 private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager();
52 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
53 private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher();
54 private final ListenerTree listenerTree = ListenerTree.create();
55 private final TipProducingDataTree dataTree;
56 private SchemaContext schemaContext;
58 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) {
59 dataTree = InMemoryDataTreeFactory.getInstance().create(treeType);
60 updateSchemaContext(schemaContext);
64 * @deprecated Use {@link #ShardDataTree(SchemaContext, TreeType)} instead.
67 public ShardDataTree(final SchemaContext schemaContext) {
68 this(schemaContext, TreeType.OPERATIONAL);
71 public TipProducingDataTree getDataTree() {
75 SchemaContext getSchemaContext() {
79 void updateSchemaContext(final SchemaContext schemaContext) {
80 Preconditions.checkNotNull(schemaContext);
81 this.schemaContext = schemaContext;
82 dataTree.setSchemaContext(schemaContext);
85 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
86 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
88 chain = new ShardDataTreeTransactionChain(chainId, this);
89 transactionChains.put(chainId, chain);
95 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
96 if (Strings.isNullOrEmpty(chainId)) {
97 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
100 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
103 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
104 if (Strings.isNullOrEmpty(chainId)) {
105 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
109 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
112 public void notifyListeners(final DataTreeCandidate candidate) {
113 LOG.debug("Notifying listeners on candidate {}", candidate);
115 // DataTreeChanges first, as they are more light-weight
116 treeChangePublisher.publishChanges(candidate);
118 // DataChanges second, as they are heavier
119 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
122 void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
123 NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
125 if(currentState.isPresent()) {
126 ListenerTree localListenerTree = ListenerTree.create();
127 localListenerTree.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
128 listenerReg.getScope());
130 ResolveDataChangeEventsTask.create(currentState.get(), localListenerTree).resolve(MANAGER);
134 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
135 final Optional<DataTreeCandidate> currentState) {
136 if(currentState.isPresent()) {
137 ShardDataTreeChangePublisher localTreeChangePublisher = new ShardDataTreeChangePublisher();
138 localTreeChangePublisher.registerTreeChangeListener(path, listener);
139 localTreeChangePublisher.publishChanges(currentState.get());
143 void closeAllTransactionChains() {
144 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
148 transactionChains.clear();
151 void closeTransactionChain(final String transactionChainId) {
152 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
156 LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
160 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
161 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
162 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
163 final DataChangeScope scope) {
164 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
165 listenerTree.registerDataChangeListener(path, listener, scope);
167 return new SimpleEntry<>(reg, readCurrentData());
170 private Optional<DataTreeCandidate> readCurrentData() {
171 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH);
172 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
173 ROOT_PATH, currentState.get())) : Optional.<DataTreeCandidate>absent();
176 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
177 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
178 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(
181 return new SimpleEntry<>(reg, readCurrentData());
184 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
185 LOG.debug("Applying foreign transaction {}", identifier);
187 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
188 DataTreeCandidates.applyToModification(mod, foreign);
191 LOG.trace("Applying foreign modification {}", mod);
192 dataTree.validate(mod);
193 final DataTreeCandidate candidate = dataTree.prepare(mod);
194 dataTree.commit(candidate);
195 notifyListeners(candidate);
199 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
204 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
205 final DataTreeModification snapshot = transaction.getSnapshot();
207 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
210 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier path) {
211 return dataTree.takeSnapshot().readNode(path);
214 public DataTreeSnapshot takeSnapshot() {
215 return dataTree.takeSnapshot();
218 public DataTreeModification newModification() {
219 return dataTree.takeSnapshot().newModification();
222 public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException {
223 modification.ready();
224 dataTree.validate(modification);
225 DataTreeCandidateTip candidate = dataTree.prepare(modification);
226 dataTree.commit(candidate);