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 java.util.AbstractMap.SimpleEntry;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
23 import org.opendaylight.yangtools.concepts.Identifier;
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);
51 private final Map<LocalHistoryIdentifier, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
52 private final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher;
53 private final ShardDataChangeListenerPublisher dataChangeListenerPublisher;
54 private final TipProducingDataTree dataTree;
55 private final String logContext;
56 private SchemaContext schemaContext;
58 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType,
59 final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher,
60 final ShardDataChangeListenerPublisher dataChangeListenerPublisher, final String logContext) {
61 dataTree = InMemoryDataTreeFactory.getInstance().create(treeType);
62 updateSchemaContext(schemaContext);
63 this.treeChangeListenerPublisher = treeChangeListenerPublisher;
64 this.dataChangeListenerPublisher = dataChangeListenerPublisher;
65 this.logContext = logContext;
68 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) {
69 this(schemaContext, treeType, new DefaultShardDataTreeChangeListenerPublisher(),
70 new DefaultShardDataChangeListenerPublisher(), "");
73 public TipProducingDataTree getDataTree() {
77 SchemaContext getSchemaContext() {
81 void updateSchemaContext(final SchemaContext schemaContext) {
82 Preconditions.checkNotNull(schemaContext);
83 this.schemaContext = schemaContext;
84 dataTree.setSchemaContext(schemaContext);
87 private ShardDataTreeTransactionChain ensureTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier) {
88 ShardDataTreeTransactionChain chain = transactionChains.get(localHistoryIdentifier);
90 chain = new ShardDataTreeTransactionChain(localHistoryIdentifier, this);
91 transactionChains.put(localHistoryIdentifier, chain);
97 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
98 if (txId.getHistoryId().getHistoryId() == 0) {
99 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
102 return ensureTransactionChain(txId.getHistoryId()).newReadOnlyTransaction(txId);
105 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
106 if (txId.getHistoryId().getHistoryId() == 0) {
107 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
111 return ensureTransactionChain(txId.getHistoryId()).newReadWriteTransaction(txId);
114 public void notifyListeners(final DataTreeCandidate candidate) {
115 treeChangeListenerPublisher.publishChanges(candidate, logContext);
116 dataChangeListenerPublisher.publishChanges(candidate, logContext);
119 void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
120 NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
122 if(currentState.isPresent()) {
123 ShardDataChangeListenerPublisher localPublisher = dataChangeListenerPublisher.newInstance();
124 localPublisher.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
125 listenerReg.getScope());
126 localPublisher.publishChanges(currentState.get(), logContext);
130 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
131 final Optional<DataTreeCandidate> currentState) {
132 if(currentState.isPresent()) {
133 ShardDataTreeChangeListenerPublisher localPublisher = treeChangeListenerPublisher.newInstance();
134 localPublisher.registerTreeChangeListener(path, listener);
135 localPublisher.publishChanges(currentState.get(), logContext);
139 void closeAllTransactionChains() {
140 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
144 transactionChains.clear();
147 void closeTransactionChain(final LocalHistoryIdentifier transactionChainId) {
148 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
152 LOG.debug("{}: Closing non-existent transaction chain {}", logContext, transactionChainId);
156 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
157 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
158 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
159 final DataChangeScope scope) {
160 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
161 dataChangeListenerPublisher.registerDataChangeListener(path, listener, scope);
163 return new SimpleEntry<>(reg, readCurrentData());
166 private Optional<DataTreeCandidate> readCurrentData() {
167 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
168 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
169 YangInstanceIdentifier.EMPTY, currentState.get())) : Optional.<DataTreeCandidate>absent();
172 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
173 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
174 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangeListenerPublisher.registerTreeChangeListener(
177 return new SimpleEntry<>(reg, readCurrentData());
180 void applyForeignCandidate(final Identifier identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
181 LOG.debug("{}: Applying foreign transaction {}", logContext, identifier);
183 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
184 DataTreeCandidates.applyToModification(mod, foreign);
187 LOG.trace("{}: Applying foreign modification {}", logContext, mod);
188 dataTree.validate(mod);
189 final DataTreeCandidate candidate = dataTree.prepare(mod);
190 dataTree.commit(candidate);
191 notifyListeners(candidate);
195 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
200 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
201 final DataTreeModification snapshot = transaction.getSnapshot();
203 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
206 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier path) {
207 return dataTree.takeSnapshot().readNode(path);
210 public DataTreeSnapshot takeSnapshot() {
211 return dataTree.takeSnapshot();
214 public DataTreeModification newModification() {
215 return dataTree.takeSnapshot().newModification();
218 // FIXME: This should be removed, it violates encapsulation
219 public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException {
220 modification.ready();
221 dataTree.validate(modification);
222 DataTreeCandidateTip candidate = dataTree.prepare(modification);
223 dataTree.commit(candidate);