BUG 2970 : Recovery fails with SchemaValidationException when removing modules
[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.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;
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 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;
53
54     ShardDataTree(final SchemaContext schemaContext) {
55         dataTree = InMemoryDataTreeFactory.getInstance().create();
56         updateSchemaContext(schemaContext);
57
58     }
59
60     TipProducingDataTree getDataTree() {
61         return dataTree;
62     }
63
64     SchemaContext getSchemaContext() {
65         return schemaContext;
66     }
67
68     void updateSchemaContext(final SchemaContext schemaContext) {
69         Preconditions.checkNotNull(schemaContext);
70         this.schemaContext = schemaContext;
71         dataTree.setSchemaContext(schemaContext);
72     }
73
74     private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
75         ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
76         if (chain == null) {
77             chain = new ShardDataTreeTransactionChain(chainId, this);
78             transactionChains.put(chainId, chain);
79         }
80
81         return chain;
82     }
83
84     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
85         if (Strings.isNullOrEmpty(chainId)) {
86             return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
87         }
88
89         return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
90     }
91
92     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
93         if (Strings.isNullOrEmpty(chainId)) {
94             return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
95                     .newModification());
96         }
97
98         return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
99     }
100
101     void notifyListeners(final DataTreeCandidate candidate) {
102         LOG.debug("Notifying listeners on candidate {}", candidate);
103
104         // DataTreeChanges first, as they are more light-weight
105         treeChangePublisher.publishChanges(candidate);
106
107         // DataChanges second, as they are heavier
108         ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER);
109     }
110
111     void closeAllTransactionChains() {
112         for (ShardDataTreeTransactionChain chain : transactionChains.values()) {
113             chain.close();
114         }
115
116         transactionChains.clear();
117     }
118
119     void closeTransactionChain(final String transactionChainId) {
120         final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
121         if (chain != null) {
122             chain.close();
123         } else {
124             LOG.debug("Closing non-existent transaction chain {}", transactionChainId);
125         }
126     }
127
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);
133
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();
139         } else {
140             event = null;
141         }
142
143         return new SimpleEntry<>(reg, event);
144     }
145
146     Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
147             final DOMDataTreeChangeListener listener) {
148         final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
149
150         final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
151         final DataTreeCandidate event;
152         if (currentState.isPresent()) {
153             event = DataTreeCandidates.fromNormalizedNode(path, currentState.get());
154         } else {
155             event = null;
156         }
157         return new SimpleEntry<>(reg, event);
158     }
159
160     void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException {
161         LOG.debug("Applying foreign transaction {}", identifier);
162
163         final DataTreeModification mod = dataTree.takeSnapshot().newModification();
164         DataTreeCandidates.applyToModification(mod, foreign);
165         mod.ready();
166
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);
172     }
173
174     @Override
175     void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
176         // Intentional no-op
177     }
178
179     @Override
180     ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
181         final DataTreeModification snapshot = transaction.getSnapshot();
182         snapshot.ready();
183         return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
184     }
185 }