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.Preconditions;
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.cluster.access.concepts.LocalHistoryIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
25 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
26 import org.opendaylight.yangtools.concepts.Identifier;
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.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
38 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Internal shard state, similar to a DOMStore, but optimized for use in the actor system,
45 * e.g. it does not expose public interfaces and assumes it is only ever called from a
48 * This class is not part of the API contract and is subject to change at any time.
51 public class ShardDataTree extends ShardDataTreeTransactionParent {
52 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
54 private final Map<LocalHistoryIdentifier, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
55 private final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher;
56 private final ShardDataChangeListenerPublisher dataChangeListenerPublisher;
57 private final TipProducingDataTree dataTree;
58 private final String logContext;
59 private SchemaContext schemaContext;
61 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType,
62 final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher,
63 final ShardDataChangeListenerPublisher dataChangeListenerPublisher, final String logContext) {
64 dataTree = InMemoryDataTreeFactory.getInstance().create(treeType);
65 updateSchemaContext(schemaContext);
67 this.treeChangeListenerPublisher = Preconditions.checkNotNull(treeChangeListenerPublisher);
68 this.dataChangeListenerPublisher = Preconditions.checkNotNull(dataChangeListenerPublisher);
69 this.logContext = Preconditions.checkNotNull(logContext);
72 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) {
73 this(schemaContext, treeType, new DefaultShardDataTreeChangeListenerPublisher(),
74 new DefaultShardDataChangeListenerPublisher(), "");
77 public TipProducingDataTree getDataTree() {
81 SchemaContext getSchemaContext() {
85 void updateSchemaContext(final SchemaContext schemaContext) {
86 dataTree.setSchemaContext(schemaContext);
87 this.schemaContext = Preconditions.checkNotNull(schemaContext);
90 ShardDataTreeSnapshot takeRecoverySnapshot() {
91 return new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get());
94 void applyRecoveryTransaction(final ReadWriteShardDataTreeTransaction transaction) throws DataValidationFailedException {
95 final DataTreeModification snapshot = transaction.getSnapshot();
98 dataTree.validate(snapshot);
99 dataTree.commit(dataTree.prepare(snapshot));
102 private ShardDataTreeTransactionChain ensureTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier) {
103 ShardDataTreeTransactionChain chain = transactionChains.get(localHistoryIdentifier);
105 chain = new ShardDataTreeTransactionChain(localHistoryIdentifier, this);
106 transactionChains.put(localHistoryIdentifier, chain);
112 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
113 if (txId.getHistoryId().getHistoryId() == 0) {
114 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
117 return ensureTransactionChain(txId.getHistoryId()).newReadOnlyTransaction(txId);
120 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
121 if (txId.getHistoryId().getHistoryId() == 0) {
122 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
126 return ensureTransactionChain(txId.getHistoryId()).newReadWriteTransaction(txId);
129 public void notifyListeners(final DataTreeCandidate candidate) {
130 treeChangeListenerPublisher.publishChanges(candidate, logContext);
131 dataChangeListenerPublisher.publishChanges(candidate, logContext);
134 void notifyOfInitialData(final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
135 NormalizedNode<?, ?>>> listenerReg, final Optional<DataTreeCandidate> currentState) {
136 if (currentState.isPresent()) {
137 ShardDataChangeListenerPublisher localPublisher = dataChangeListenerPublisher.newInstance();
138 localPublisher.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
139 listenerReg.getScope());
140 localPublisher.publishChanges(currentState.get(), logContext);
144 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
145 final Optional<DataTreeCandidate> currentState) {
146 if (currentState.isPresent()) {
147 ShardDataTreeChangeListenerPublisher localPublisher = treeChangeListenerPublisher.newInstance();
148 localPublisher.registerTreeChangeListener(path, listener);
149 localPublisher.publishChanges(currentState.get(), logContext);
153 void closeAllTransactionChains() {
154 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
158 transactionChains.clear();
161 void closeTransactionChain(final LocalHistoryIdentifier transactionChainId) {
162 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
166 LOG.debug("{}: Closing non-existent transaction chain {}", logContext, transactionChainId);
170 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
171 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
172 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
173 final DataChangeScope scope) {
174 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
175 dataChangeListenerPublisher.registerDataChangeListener(path, listener, scope);
177 return new SimpleEntry<>(reg, readCurrentData());
180 private Optional<DataTreeCandidate> readCurrentData() {
181 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
182 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
183 YangInstanceIdentifier.EMPTY, currentState.get())) : Optional.<DataTreeCandidate>absent();
186 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
187 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
188 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangeListenerPublisher.registerTreeChangeListener(
191 return new SimpleEntry<>(reg, readCurrentData());
194 void applyForeignCandidate(final Identifier identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
195 LOG.debug("{}: Applying foreign transaction {}", logContext, identifier);
197 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
198 DataTreeCandidates.applyToModification(mod, foreign);
201 LOG.trace("{}: Applying foreign modification {}", logContext, mod);
202 dataTree.validate(mod);
203 final DataTreeCandidate candidate = dataTree.prepare(mod);
204 dataTree.commit(candidate);
205 notifyListeners(candidate);
209 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
214 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
215 final DataTreeModification snapshot = transaction.getSnapshot();
217 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
220 public Optional<NormalizedNode<?, ?>> readNode(final YangInstanceIdentifier path) {
221 return dataTree.takeSnapshot().readNode(path);
224 public DataTreeSnapshot takeSnapshot() {
225 return dataTree.takeSnapshot();
228 public DataTreeModification newModification() {
229 return dataTree.takeSnapshot().newModification();
233 // FIXME: This should be removed, it violates encapsulation
234 public DataTreeCandidate commit(final DataTreeModification modification) throws DataValidationFailedException {
235 modification.ready();
236 dataTree.validate(modification);
237 DataTreeCandidateTip candidate = dataTree.prepare(modification);
238 dataTree.commit(candidate);