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.DOMImmutableDataChangeEvent;
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 ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager();
48 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
49 private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher();
50 private final ListenerTree listenerTree = ListenerTree.create();
51 private final TipProducingDataTree dataTree;
52 private SchemaContext schemaContext;
54 ShardDataTree(final SchemaContext schemaContext) {
55 dataTree = InMemoryDataTreeFactory.getInstance().create();
56 updateSchemaContext(schemaContext);
60 TipProducingDataTree getDataTree() {
64 SchemaContext getSchemaContext() {
68 void updateSchemaContext(final SchemaContext schemaContext) {
69 Preconditions.checkNotNull(schemaContext);
70 this.schemaContext = schemaContext;
71 dataTree.setSchemaContext(schemaContext);
74 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
75 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
77 chain = new ShardDataTreeTransactionChain(chainId, this);
78 transactionChains.put(chainId, chain);
84 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
85 if (Strings.isNullOrEmpty(chainId)) {
86 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
89 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
92 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
93 if (Strings.isNullOrEmpty(chainId)) {
94 return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
98 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
101 void notifyListeners(final DataTreeCandidate candidate) {
102 LOG.debug("Notifying listeners on candidate {}", candidate);
104 // DataTreeChanges first, as they are more light-weight
105 treeChangePublisher.publishChanges(candidate);
107 // DataChanges second, as they are heavier
108 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
111 void closeAllTransactionChains() {
112 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
116 transactionChains.clear();
119 void closeTransactionChain(final String transactionChainId) {
120 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
124 LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
128 Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> registerChangeListener(
129 final YangInstanceIdentifier path,
130 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final DataChangeScope scope) {
131 final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
132 listenerTree.registerDataChangeListener(path, listener, scope);
134 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
135 final DOMImmutableDataChangeEvent event;
136 if (currentState.isPresent()) {
137 final NormalizedNode<?, ?> data = currentState.get();
138 event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE).setAfter(data).addCreated(path, data).build();
143 return new SimpleEntry<>(reg, event);
146 Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
147 final DOMDataTreeChangeListener listener) {
148 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
150 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
151 final DataTreeCandidate event;
152 if (currentState.isPresent()) {
153 event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
157 return new SimpleEntry<>(reg, event);
160 void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
161 LOG.debug("Applying foreign transaction {}", identifier);
163 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
164 DataTreeCandidates.applyToModification(mod, foreign);
167 LOG.trace("Applying foreign modification {}", mod);
168 dataTree.validate(mod);
169 final DataTreeCandidate candidate = dataTree.prepare(mod);
170 dataTree.commit(candidate);
171 notifyListeners(candidate);
175 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
180 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
181 final DataTreeModification snapshot = transaction.getSnapshot();
183 return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());