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.DataTreeCandidates;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
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.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Internal shard state, similar to a DOMStore, but optimized for use in the actor system,
39 * e.g. it does not expose public interfaces and assumes it is only ever called from a
42 * This class is not part of the API contract and is subject to change at any time.
45 public class ShardDataTree extends ShardDataTreeTransactionParent {
46 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
47 private static final YangInstanceIdentifier ROOT_PATH = YangInstanceIdentifier.builder().build();
48 private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager();
49 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
50 private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher();
51 private final ListenerTree listenerTree = ListenerTree.create();
52 private final TipProducingDataTree dataTree;
53 private SchemaContext schemaContext;
55 public ShardDataTree(final SchemaContext schemaContext) {
56 dataTree = InMemoryDataTreeFactory.getInstance().create();
57 updateSchemaContext(schemaContext);
61 public TipProducingDataTree getDataTree() {
65 SchemaContext getSchemaContext() {
69 void updateSchemaContext(final SchemaContext schemaContext) {
70 Preconditions.checkNotNull(schemaContext);
71 this.schemaContext = schemaContext;
72 dataTree.setSchemaContext(schemaContext);
75 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
76 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
78 chain = new ShardDataTreeTransactionChain(chainId, this);
79 transactionChains.put(chainId, chain);
85 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
86 if (Strings.isNullOrEmpty(chainId)) {
87 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
90 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
93 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
94 if (Strings.isNullOrEmpty(chainId)) {
95 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
99 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
102 public void notifyListeners(final DataTreeCandidate candidate) {
103 LOG.debug("Notifying listeners on candidate {}", candidate);
105 // DataTreeChanges first, as they are more light-weight
106 treeChangePublisher.publishChanges(candidate);
108 // DataChanges second, as they are heavier
109 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
112 void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
113 NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
115 if(currentState.isPresent()) {
116 ListenerTree localListenerTree = ListenerTree.create();
117 localListenerTree.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
118 listenerReg.getScope());
120 ResolveDataChangeEventsTask.create(currentState.get(), localListenerTree).resolve(MANAGER);
124 void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
125 final Optional<DataTreeCandidate> currentState) {
126 if(currentState.isPresent()) {
127 ShardDataTreeChangePublisher localTreeChangePublisher = new ShardDataTreeChangePublisher();
128 localTreeChangePublisher.registerTreeChangeListener(path, listener);
129 localTreeChangePublisher.publishChanges(currentState.get());
133 void closeAllTransactionChains() {
134 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
138 transactionChains.clear();
141 void closeTransactionChain(final String transactionChainId) {
142 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
146 LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
150 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
151 Optional<DataTreeCandidate>> registerChangeListener(final YangInstanceIdentifier path,
152 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener,
153 final DataChangeScope scope) {
154 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
155 listenerTree.registerDataChangeListener(path, listener, scope);
157 return new SimpleEntry<>(reg, readCurrentData());
160 private Optional<DataTreeCandidate> readCurrentData() {
161 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH);
162 return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode(
163 ROOT_PATH, currentState.get())) : Optional.<DataTreeCandidate>absent();
166 public Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> registerTreeChangeListener(
167 final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) {
168 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(
171 return new SimpleEntry<>(reg, readCurrentData());
174 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
175 LOG.debug("Applying foreign transaction {}", identifier);
177 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
178 DataTreeCandidates.applyToModification(mod, foreign);
181 LOG.trace("Applying foreign modification {}", mod);
182 dataTree.validate(mod);
183 final DataTreeCandidate candidate = dataTree.prepare(mod);
184 dataTree.commit(candidate);
185 notifyListeners(candidate);
189 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
194 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
195 final DataTreeModification snapshot = transaction.getSnapshot();
197 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());