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.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.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
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.
47 public final class ShardDataTree extends ShardDataTreeTransactionParent {
48 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
49 private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager();
50 private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
51 private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher();
52 private final ListenerTree listenerTree = ListenerTree.create();
53 private final TipProducingDataTree dataTree;
55 ShardDataTree(final SchemaContext schemaContext) {
56 dataTree = InMemoryDataTreeFactory.getInstance().create();
57 if (schemaContext != null) {
58 dataTree.setSchemaContext(schemaContext);
62 TipProducingDataTree getDataTree() {
66 void updateSchemaContext(final SchemaContext schemaContext) {
67 dataTree.setSchemaContext(schemaContext);
70 private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
71 ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
73 chain = new ShardDataTreeTransactionChain(chainId, this);
74 transactionChains.put(chainId, chain);
80 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
81 if (Strings.isNullOrEmpty(chainId)) {
82 return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
85 return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
88 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
89 if (Strings.isNullOrEmpty(chainId)) {
90 return new ReadWriteShardDataTreeTransaction(this, txId, dataTree.takeSnapshot().newModification());
93 return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
96 void notifyListeners(final DataTreeCandidateTip candidate) {
97 LOG.debug("Notifying listeners on candidate {}", candidate);
99 // DataTreeChanges first, as they are more light-weight
100 treeChangePublisher.publishChanges(candidate);
102 // DataChanges second, as they are heavier
103 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
106 void closeAllTransactionChains() {
107 for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
111 transactionChains.clear();
114 void closeTransactionChain(final String transactionChainId) {
115 final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
119 LOG.warn("Closing non-existent transaction chain {}", transactionChainId);
123 Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> registerChangeListener(
124 final YangInstanceIdentifier path,
125 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final DataChangeScope scope) {
126 final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
127 listenerTree.registerDataChangeListener(path, listener, scope);
129 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
130 final DOMImmutableDataChangeEvent event;
131 if (currentState.isPresent()) {
132 final NormalizedNode<?, ?> data = currentState.get();
133 event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE).setAfter(data).addCreated(path, data).build();
138 return new SimpleEntry<>(reg, event);
141 Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
142 final DOMDataTreeChangeListener listener) {
143 final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
145 final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
146 final DataTreeCandidate event;
147 if (currentState.isPresent()) {
148 event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
152 return new SimpleEntry<>(reg, event);
156 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
161 DOMStoreThreePhaseCommitCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
162 final DataTreeModification snapshot = transaction.getSnapshot();
164 return new ShardDataTreeCohort(this, snapshot);