CDS: use internal DataTree instance
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTree.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
15 import java.util.Map;
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;
37
38 /**
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
41  * single thread.
42  *
43  * This class is not part of the API contract and is subject to change at any time.
44  */
45 @NotThreadSafe
46 @VisibleForTesting
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;
54
55     ShardDataTree(final SchemaContext schemaContext) {
56         dataTree = InMemoryDataTreeFactory.getInstance().create();
57         if (schemaContext != null) {
58             dataTree.setSchemaContext(schemaContext);
59         }
60     }
61
62     TipProducingDataTree getDataTree() {
63         return dataTree;
64     }
65
66     void updateSchemaContext(final SchemaContext schemaContext) {
67         dataTree.setSchemaContext(schemaContext);
68     }
69
70     private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
71         ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
72         if (chain == null) {
73             chain = new ShardDataTreeTransactionChain(chainId, this);
74             transactionChains.put(chainId, chain);
75         }
76
77         return chain;
78     }
79
80     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
81         if (Strings.isNullOrEmpty(chainId)) {
82             return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
83         }
84
85         return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
86     }
87
88     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
89         if (Strings.isNullOrEmpty(chainId)) {
90             return new ReadWriteShardDataTreeTransaction(this, txId, dataTree.takeSnapshot().newModification());
91         }
92
93         return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
94     }
95
96     void notifyListeners(final DataTreeCandidateTip candidate) {
97         LOG.debug("Notifying listeners on candidate {}", candidate);
98
99         // DataTreeChanges first, as they are more light-weight
100         treeChangePublisher.publishChanges(candidate);
101
102         // DataChanges second, as they are heavier
103         ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
104     }
105
106     void closeAllTransactionChains() {
107         for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
108             chain.close();
109         }
110
111         transactionChains.clear();
112     }
113
114     void closeTransactionChain(final String transactionChainId) {
115         final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
116         if (chain != null) {
117             chain.close();
118         } else {
119             LOG.warn("Closing non-existent transaction chain {}", transactionChainId);
120         }
121     }
122
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);
128
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();
134         } else {
135             event = null;
136         }
137
138         return new SimpleEntry<>(reg, event);
139     }
140
141     Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
142             final DOMDataTreeChangeListener listener) {
143         final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
144
145         final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
146         final DataTreeCandidate event;
147         if (currentState.isPresent()) {
148             event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
149         } else {
150             event = null;
151         }
152         return new SimpleEntry<>(reg, event);
153     }
154
155     @Override
156     void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
157         // Intentional no-op
158     }
159
160     @Override
161     DOMStoreThreePhaseCommitCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
162         final DataTreeModification snapshot = transaction.getSnapshot();
163         snapshot.ready();
164         return new ShardDataTreeCohort(this, snapshot);
165     }
166 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.