Cleanup: Remove passing around of DataPersistenceProvider
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / InMemoryDOMDataStore.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.store.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicLong;
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.common.api.data.OptimisticLockFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
24 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
25 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
34 import org.opendaylight.yangtools.concepts.Identifiable;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
37 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
38 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
47 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * In-memory DOM Data Store
55  *
56  * Implementation of {@link DOMStore} which uses {@link DataTree} and other
57  * classes such as {@link SnapshotBackedWriteTransaction}.
58  * {@link SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
59  * to implement {@link DOMStore} contract.
60  *
61  */
62 public class InMemoryDOMDataStore extends TransactionReadyPrototype implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
63     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
64     private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
65     private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
66
67     private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
68             new Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>() {
69                 @Override
70                 public void invokeListener(final DataChangeListenerRegistration<?> listener,
71                                            final DOMImmutableDataChangeEvent notification ) {
72                     final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
73                     if (inst != null) {
74                         inst.onDataChanged(notification);
75                     }
76                 }
77             };
78
79     private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
80     private final ListenerTree listenerTree = ListenerTree.create();
81     private final AtomicLong txCounter = new AtomicLong(0);
82
83     private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
84     private final InMemoryDOMStoreTreeChangePublisher changePublisher;
85     private final ExecutorService dataChangeListenerExecutor;
86     private final boolean debugTransactions;
87     private final String name;
88
89     private volatile AutoCloseable closeable;
90
91     public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
92         this(name, dataChangeListenerExecutor, InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
93     }
94
95     public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor,
96             final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
97         this.name = Preconditions.checkNotNull(name);
98         this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
99         this.debugTransactions = debugTransactions;
100
101         dataChangeListenerNotificationManager =
102                 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
103                         DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
104                         "DataChangeListenerQueueMgr");
105         changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor, maxDataChangeListenerQueueSize);
106     }
107
108     public void setCloseable(final AutoCloseable closeable) {
109         this.closeable = closeable;
110     }
111
112     public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
113         return dataChangeListenerNotificationManager;
114     }
115
116     @Override
117     public final String getIdentifier() {
118         return name;
119     }
120
121     @Override
122     public DOMStoreReadTransaction newReadOnlyTransaction() {
123         return new SnapshotBackedReadTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot());
124     }
125
126     @Override
127     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
128         return new SnapshotBackedReadWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
129     }
130
131     @Override
132     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
133         return new SnapshotBackedWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
134     }
135
136     @Override
137     public DOMStoreTransactionChain createTransactionChain() {
138         return new DOMStoreTransactionChainImpl(this);
139     }
140
141     @Override
142     public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
143         dataTree.setSchemaContext(ctx);
144     }
145
146     @Override
147     public void close() {
148         ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
149
150         if(closeable != null) {
151             try {
152                 closeable.close();
153             } catch(Exception e) {
154                 LOG.debug("Error closing instance", e);
155             }
156         }
157     }
158
159     public final boolean getDebugTransactions() {
160         return debugTransactions;
161     }
162
163     final DataTreeSnapshot takeSnapshot() {
164         return dataTree.takeSnapshot();
165     }
166
167     @Override
168     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
169             final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
170
171         /*
172          * Make sure commit is not occurring right now. Listener has to be
173          * registered and its state capture enqueued at a consistent point.
174          *
175          * FIXME: improve this to read-write lock, such that multiple listener
176          * registrations can occur simultaneously
177          */
178         final DataChangeListenerRegistration<L> reg;
179         synchronized (this) {
180             LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
181
182             reg = listenerTree.registerDataChangeListener(path, listener, scope);
183
184             Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
185             if (currentState.isPresent()) {
186                 final NormalizedNode<?, ?> data = currentState.get();
187
188                 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
189                         .setAfter(data) //
190                         .addCreated(path, data) //
191                         .build();
192
193                 dataChangeListenerNotificationManager.submitNotification(reg, event);
194             }
195         }
196
197         return new AbstractListenerRegistration<L>(listener) {
198             @Override
199             protected void removeRegistration() {
200                 synchronized (InMemoryDOMDataStore.this) {
201                     reg.close();
202                 }
203             }
204         };
205     }
206
207     @Override
208     public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
209         return changePublisher.registerTreeChangeListener(treeId, listener);
210     }
211
212     @Override
213     protected void transactionAborted(final SnapshotBackedWriteTransaction tx) {
214         LOG.debug("Tx: {} is closed.", tx.getIdentifier());
215     }
216
217     @Override
218     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction tx, final DataTreeModification tree) {
219         LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), tree);
220         return new ThreePhaseCommitImpl(tx, tree);
221     }
222
223     Object nextIdentifier() {
224         return name + "-" + txCounter.getAndIncrement();
225     }
226
227     private static void warnDebugContext(AbstractDOMStoreTransaction<?> transaction) {
228         final Throwable ctx = transaction.getDebugContext();
229         if (ctx != null) {
230             LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
231         }
232     }
233
234     private final class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
235         private final SnapshotBackedWriteTransaction transaction;
236         private final DataTreeModification modification;
237
238         private ResolveDataChangeEventsTask listenerResolver;
239         private DataTreeCandidate candidate;
240
241         public ThreePhaseCommitImpl(final SnapshotBackedWriteTransaction writeTransaction, final DataTreeModification modification) {
242             this.transaction = writeTransaction;
243             this.modification = modification;
244         }
245
246         @Override
247         public ListenableFuture<Boolean> canCommit() {
248             try {
249                 dataTree.validate(modification);
250                 LOG.debug("Store Transaction: {} can be committed", transaction.getIdentifier());
251                 return CAN_COMMIT_FUTURE;
252             } catch (ConflictingModificationAppliedException e) {
253                 LOG.warn("Store Tx: {} Conflicting modification for {}.", transaction.getIdentifier(),
254                         e.getPath());
255                 warnDebugContext(transaction);
256                 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
257             } catch (DataValidationFailedException e) {
258                 LOG.warn("Store Tx: {} Data Precondition failed for {}.", transaction.getIdentifier(),
259                         e.getPath(), e);
260                 warnDebugContext(transaction);
261
262                 // For debugging purposes, allow dumping of the modification. Coupled with the above
263                 // precondition log, it should allow us to understand what went on.
264                 LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, dataTree);
265
266                 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
267             } catch (Exception e) {
268                 LOG.warn("Unexpected failure in validation phase", e);
269                 return Futures.immediateFailedFuture(e);
270             }
271         }
272
273         @Override
274         public ListenableFuture<Void> preCommit() {
275             try {
276                 candidate = dataTree.prepare(modification);
277                 listenerResolver = ResolveDataChangeEventsTask.create(candidate, listenerTree);
278                 return SUCCESSFUL_FUTURE;
279             } catch (Exception e) {
280                 LOG.warn("Unexpected failure in pre-commit phase", e);
281                 return Futures.immediateFailedFuture(e);
282             }
283         }
284
285         @Override
286         public ListenableFuture<Void> abort() {
287             candidate = null;
288             return SUCCESSFUL_FUTURE;
289         }
290
291         @Override
292         public ListenableFuture<Void> commit() {
293             checkState(candidate != null, "Proposed subtree must be computed");
294
295             /*
296              * The commit has to occur atomically with regard to listener
297              * registrations.
298              */
299             synchronized (InMemoryDOMDataStore.this) {
300                 dataTree.commit(candidate);
301                 changePublisher.publishChange(candidate);
302                 listenerResolver.resolve(dataChangeListenerNotificationManager);
303             }
304
305             return SUCCESSFUL_FUTURE;
306         }
307     }
308 }

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