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.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
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.api.schema.tree.TreeType;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Internal shard state, similar to a DOMStore, but optimized for use in the actor system,
40 * e.g. it does not expose public interfaces and assumes it is only ever called from a
43 * This class is not part of the API contract and is subject to change at any time.
46 public class ShardDataTree extends ShardDataTreeTransactionParent {
47 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
48 private static final YangInstanceIdentifier ROOT_PATH = YangInstanceIdentifier.builder().build();
50 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
51 private final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher;
52 private final ShardDataChangeListenerPublisher dataChangeListenerPublisher;
53 private final TipProducingDataTree dataTree;
54 private final String logContext;
55 private SchemaContext schemaContext;
57 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType,
58 final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher,
59 final ShardDataChangeListenerPublisher dataChangeListenerPublisher, final String logContext) {
60 dataTree = InMemoryDataTreeFactory.getInstance().create(treeType);
61 updateSchemaContext(schemaContext);
62 this.treeChangeListenerPublisher = treeChangeListenerPublisher;
63 this.dataChangeListenerPublisher = dataChangeListenerPublisher;
64 this.logContext = logContext;
67 public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) {
68 this(schemaContext, treeType, new DefaultShardDataTreeChangeListenerPublisher(),
69 new DefaultShardDataChangeListenerPublisher(), "");
72 public TipProducingDataTree getDataTree() {
76 SchemaContext getSchemaContext() {
80 void updateSchemaContext(final SchemaContext schemaContext) {
81 Preconditions.checkNotNull(schemaContext);
82 this.schemaContext = schemaContext;
83 dataTree.setSchemaContext(schemaContext);
86 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
87 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
89 chain = new ShardDataTreeTransactionChain(chainId, this);
90 transactionChains.put(chainId, chain);
96 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
97 if (Strings.isNullOrEmpty(chainId)) {
98 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
101 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
104 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
105 if (Strings.isNullOrEmpty(chainId)) {
106 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
110 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
113 public void notifyListeners(final DataTreeCandidate candidate) {
114 treeChangeListenerPublisher.publishChanges(candidate, logContext);
115 dataChangeListenerPublisher.publishChanges(candidate, logContext);
118 void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
119 NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
121 if(currentState.isPresent()) {
122 ShardDataChangeListenerPublisher localPublisher = dataChangeListenerPublisher.newInstance();
123 localPublisher.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
124 listenerReg.getScope());
125 localPublisher.publishChanges(currentState.get(), logContext);
129 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
130 final Optional<DataTreeCandidate> currentState) {
131 if(currentState.isPresent()) {
132 ShardDataTreeChangeListenerPublisher localPublisher = treeChangeListenerPublisher.newInstance();
133 localPublisher.registerTreeChangeListener(path, listener);
134 localPublisher.publishChanges(currentState.get(), logContext);
138 void closeAllTransactionChains() {
139 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
143 transactionChains.clear();
146 void closeTransactionChain(final String transactionChainId) {
147 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
151 LOG.debug("{}: Closing non-existent transaction chain {}", logContext, transactionChainId);
155 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
156 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
157 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
158 final DataChangeScope scope) {
159 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
160 dataChangeListenerPublisher.registerDataChangeListener(path, listener, scope);
162 return new SimpleEntry<>(reg, readCurrentData());
165 private Optional<DataTreeCandidate> readCurrentData() {
166 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH);
167 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
168 ROOT_PATH, currentState.get())) : Optional.<DataTreeCandidate>absent();
171 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
172 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
173 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangeListenerPublisher.registerTreeChangeListener(
176 return new SimpleEntry<>(reg, readCurrentData());
179 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
180 LOG.debug("{}: Applying foreign transaction {}", logContext, identifier);
182 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
183 DataTreeCandidates.applyToModification(mod, foreign);
186 LOG.trace("{}: Applying foreign modification {}", logContext, mod);
187 dataTree.validate(mod);
188 final DataTreeCandidate candidate = dataTree.prepare(mod);
189 dataTree.commit(candidate);
190 notifyListeners(candidate);
194 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
199 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
200 final DataTreeModification snapshot = transaction.getSnapshot();
202 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
205 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier path) {
206 return dataTree.takeSnapshot().readNode(path);
209 public DataTreeSnapshot takeSnapshot() {
210 return dataTree.takeSnapshot();
213 public DataTreeModification newModification() {
214 return dataTree.takeSnapshot().newModification();
217 public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException {
218 modification.ready();
219 dataTree.validate(modification);
220 DataTreeCandidateTip candidate = dataTree.prepare(modification);
221 dataTree.commit(candidate);