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);
63 public TipProducingDataTree getDataTree() {
67 SchemaContext getSchemaContext() {
71 void updateSchemaContext(final SchemaContext schemaContext) {
72 Preconditions.checkNotNull(schemaContext);
73 this.schemaContext = schemaContext;
74 dataTree.setSchemaContext(schemaContext);
77 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
78 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
80 chain = new ShardDataTreeTransactionChain(chainId, this);
81 transactionChains.put(chainId, chain);
87 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
88 if (Strings.isNullOrEmpty(chainId)) {
89 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
92 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
95 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
96 if (Strings.isNullOrEmpty(chainId)) {
97 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
101 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
104 public void notifyListeners(final DataTreeCandidate candidate) {
105 LOG.debug("Notifying listeners on candidate {}", candidate);
107 // DataTreeChanges first, as they are more light-weight
108 treeChangePublisher.publishChanges(candidate);
110 // DataChanges second, as they are heavier
111 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
114 void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
115 NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
117 if(currentState.isPresent()) {
118 ListenerTree localListenerTree = ListenerTree.create();
119 localListenerTree.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
120 listenerReg.getScope());
122 ResolveDataChangeEventsTask.create(currentState.get(), localListenerTree).resolve(MANAGER);
126 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
127 final Optional<DataTreeCandidate> currentState) {
128 if(currentState.isPresent()) {
129 ShardDataTreeChangePublisher localTreeChangePublisher = new ShardDataTreeChangePublisher();
130 localTreeChangePublisher.registerTreeChangeListener(path, listener);
131 localTreeChangePublisher.publishChanges(currentState.get());
135 void closeAllTransactionChains() {
136 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
140 transactionChains.clear();
143 void closeTransactionChain(final String transactionChainId) {
144 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
148 LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
152 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
153 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
154 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
155 final DataChangeScope scope) {
156 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
157 listenerTree.registerDataChangeListener(path, listener, scope);
159 return new SimpleEntry<>(reg, readCurrentData());
162 private Optional<DataTreeCandidate> readCurrentData() {
163 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH);
164 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
165 ROOT_PATH, currentState.get())) : Optional.<DataTreeCandidate>absent();
168 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
169 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
170 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(
173 return new SimpleEntry<>(reg, readCurrentData());
176 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
177 LOG.debug("Applying foreign transaction {}", identifier);
179 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
180 DataTreeCandidates.applyToModification(mod, foreign);
183 LOG.trace("Applying foreign modification {}", mod);
184 dataTree.validate(mod);
185 final DataTreeCandidate candidate = dataTree.prepare(mod);
186 dataTree.commit(candidate);
187 notifyListeners(candidate);
191 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
196 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
197 final DataTreeModification snapshot = transaction.getSnapshot();
199 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
202 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier path) {
203 return dataTree.takeSnapshot().readNode(path);
206 public DataTreeSnapshot takeSnapshot() {
207 return dataTree.takeSnapshot();
210 public DataTreeModification newModification() {
211 return dataTree.takeSnapshot().newModification();
214 public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException {
215 modification.ready();
216 dataTree.validate(modification);
217 DataTreeCandidateTip candidate = dataTree.prepare(modification);
218 dataTree.commit(candidate);