Merge "Moved legacy Notification implementation to compat package."
[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.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;
36
37 /**
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
40  * single thread.
41  *
42  * This class is not part of the API contract and is subject to change at any time.
43  */
44 @NotThreadSafe
45 @VisibleForTesting
46 public final class ShardDataTree extends ShardDataTreeTransactionParent {
47     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
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
54     ShardDataTree(final SchemaContext schemaContext) {
55         dataTree = InMemoryDataTreeFactory.getInstance().create();
56         if (schemaContext != null) {
57             dataTree.setSchemaContext(schemaContext);
58         }
59     }
60
61     TipProducingDataTree getDataTree() {
62         return dataTree;
63     }
64
65     void updateSchemaContext(final SchemaContext schemaContext) {
66         dataTree.setSchemaContext(schemaContext);
67     }
68
69     private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
70         ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
71         if (chain == null) {
72             chain = new ShardDataTreeTransactionChain(chainId, this);
73             transactionChains.put(chainId, chain);
74         }
75
76         return chain;
77     }
78
79     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
80         if (Strings.isNullOrEmpty(chainId)) {
81             return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
82         }
83
84         return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
85     }
86
87     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
88         if (Strings.isNullOrEmpty(chainId)) {
89             return new ReadWriteShardDataTreeTransaction(this, txId, dataTree.takeSnapshot().newModification());
90         }
91
92         return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
93     }
94
95     void notifyListeners(final DataTreeCandidate candidate) {
96         LOG.debug("Notifying listeners on candidate {}", candidate);
97
98         // DataTreeChanges first, as they are more light-weight
99         treeChangePublisher.publishChanges(candidate);
100
101         // DataChanges second, as they are heavier
102         ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
103     }
104
105     void closeAllTransactionChains() {
106         for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
107             chain.close();
108         }
109
110         transactionChains.clear();
111     }
112
113     void closeTransactionChain(final String transactionChainId) {
114         final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
115         if (chain != null) {
116             chain.close();
117         } else {
118             LOG.warn("Closing non-existent transaction chain {}", transactionChainId);
119         }
120     }
121
122     Entry<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>, DOMImmutableDataChangeEvent> registerChangeListener(
123             final YangInstanceIdentifier path,
124             final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, final DataChangeScope scope) {
125         final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> reg =
126                 listenerTree.registerDataChangeListener(path, listener, scope);
127
128         final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
129         final DOMImmutableDataChangeEvent event;
130         if (currentState.isPresent()) {
131             final NormalizedNode<?, ?> data = currentState.get();
132             event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE).setAfter(data).addCreated(path, data).build();
133         } else {
134             event = null;
135         }
136
137         return new SimpleEntry<>(reg, event);
138     }
139
140     Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
141             final DOMDataTreeChangeListener listener) {
142         final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
143
144         final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
145         final DataTreeCandidate event;
146         if (currentState.isPresent()) {
147             event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
148         } else {
149             event = null;
150         }
151         return new SimpleEntry<>(reg, event);
152     }
153
154     void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
155         LOG.debug("Applying foreign transaction {}", identifier);
156
157         final DataTreeModification mod = dataTree.takeSnapshot().newModification();
158         DataTreeCandidates.applyToModification(mod, foreign);
159         mod.ready();
160
161         LOG.trace("Applying foreign modification {}", mod);
162         dataTree.validate(mod);
163         final DataTreeCandidate candidate = dataTree.prepare(mod);
164         dataTree.commit(candidate);
165         notifyListeners(candidate);
166     }
167
168     @Override
169     void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
170         // Intentional no-op
171     }
172
173     @Override
174     ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
175         final DataTreeModification snapshot = transaction.getSnapshot();
176         snapshot.ready();
177         return new SimpleShardDataTreeCohort(this, snapshot);
178     }
179
180 }

©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.