Merge "BUG-1568 Remove ganymed Ssh client adapter"
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
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.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
23 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
24 import org.opendaylight.yangtools.util.concurrent.NotificationManager;
25 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
32 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
35 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
39 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
40 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
41 import org.opendaylight.yangtools.concepts.Identifiable;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import javax.annotation.concurrent.GuardedBy;
51
52 import java.util.concurrent.Callable;
53 import java.util.concurrent.ExecutorService;
54 import java.util.concurrent.TimeUnit;
55 import java.util.concurrent.atomic.AtomicLong;
56
57 import static com.google.common.base.Preconditions.checkState;
58
59 /**
60  * In-memory DOM Data Store
61  *
62  * Implementation of {@link DOMStore} which uses {@link DataTree} and other
63  * classes such as {@link SnapshotBackedWriteTransaction}.
64  * {@link SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
65  * to implement {@link DOMStore} contract.
66  *
67  */
68 public class InMemoryDOMDataStore implements DOMStore, Identifiable<String>, SchemaContextListener,
69         TransactionReadyPrototype,AutoCloseable {
70     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
71
72     @SuppressWarnings("rawtypes")
73     private static final QueuedNotificationManager.Invoker<AsyncDataChangeListener,
74                                        AsyncDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
75             new QueuedNotificationManager.Invoker<AsyncDataChangeListener,
76                                                   AsyncDataChangeEvent>() {
77
78                 @SuppressWarnings("unchecked")
79                 @Override
80                 public void invokeListener( AsyncDataChangeListener listener,
81                                             AsyncDataChangeEvent notification ) {
82                     listener.onDataChanged(notification);
83                 }
84             };
85
86     private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
87     private final ListenerTree listenerTree = ListenerTree.create();
88     private final AtomicLong txCounter = new AtomicLong(0);
89     private final ListeningExecutorService listeningExecutor;
90
91     @SuppressWarnings("rawtypes")
92     private final NotificationManager<AsyncDataChangeListener,AsyncDataChangeEvent>
93                                                               dataChangeListenerNotificationManager;
94     private final ExecutorService dataChangeListenerExecutor;
95
96     private final String name;
97
98     public InMemoryDOMDataStore(final String name, final ListeningExecutorService listeningExecutor,
99             final ExecutorService dataChangeListenerExecutor) {
100         this(name, listeningExecutor, dataChangeListenerExecutor,
101                 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE);
102     }
103
104     public InMemoryDOMDataStore(final String name, final ListeningExecutorService listeningExecutor,
105             final ExecutorService dataChangeListenerExecutor, int maxDataChangeListenerQueueSize) {
106         this.name = Preconditions.checkNotNull(name);
107         this.listeningExecutor = Preconditions.checkNotNull(listeningExecutor);
108
109         this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
110
111         dataChangeListenerNotificationManager =
112                 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
113                         DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
114                         "DataChangeListenerQueueMgr");
115     }
116
117     @Override
118     public final String getIdentifier() {
119         return name;
120     }
121
122     @Override
123     public DOMStoreReadTransaction newReadOnlyTransaction() {
124         return new SnapshotBackedReadTransaction(nextIdentifier(), dataTree.takeSnapshot());
125     }
126
127     @Override
128     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
129         return new SnapshotBackedReadWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this);
130     }
131
132     @Override
133     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
134         return new SnapshotBackedWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this);
135     }
136
137     @Override
138     public DOMStoreTransactionChain createTransactionChain() {
139         return new DOMStoreTransactionChainImpl();
140     }
141
142     @Override
143     public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
144         dataTree.setSchemaContext(ctx);
145     }
146
147     @Override
148     public void close() {
149         ExecutorServiceUtil.tryGracefulShutdown(listeningExecutor, 30, TimeUnit.SECONDS);
150         ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
151     }
152     @Override
153     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
154             final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
155
156         /*
157          * Make sure commit is not occurring right now. Listener has to be
158          * registered and its state capture enqueued at a consistent point.
159          *
160          * FIXME: improve this to read-write lock, such that multiple listener
161          * registrations can occur simultaneously
162          */
163         final DataChangeListenerRegistration<L> reg;
164         synchronized (this) {
165             LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
166
167             reg = listenerTree.registerDataChangeListener(path, listener, scope);
168
169             Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
170             if (currentState.isPresent()) {
171                 final NormalizedNode<?, ?> data = currentState.get();
172
173                 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
174                         .setAfter(data) //
175                         .addCreated(path, data) //
176                         .build();
177
178                 new ChangeListenerNotifyTask(reg, event,
179                         dataChangeListenerNotificationManager).run();
180             }
181         }
182
183         return new AbstractListenerRegistration<L>(listener) {
184             @Override
185             protected void removeRegistration() {
186                 synchronized (InMemoryDOMDataStore.this) {
187                     reg.close();
188                 }
189             }
190         };
191     }
192
193     @Override
194     public DOMStoreThreePhaseCommitCohort ready(final SnapshotBackedWriteTransaction writeTx) {
195         LOG.debug("Tx: {} is submitted. Modifications: {}", writeTx.getIdentifier(), writeTx.getMutatedView());
196         return new ThreePhaseCommitImpl(writeTx);
197     }
198
199     private Object nextIdentifier() {
200         return name + "-" + txCounter.getAndIncrement();
201     }
202
203     private class DOMStoreTransactionChainImpl implements DOMStoreTransactionChain, TransactionReadyPrototype {
204
205         @GuardedBy("this")
206         private SnapshotBackedWriteTransaction latestOutstandingTx;
207
208         private boolean chainFailed = false;
209
210         private void checkFailed() {
211             Preconditions.checkState(!chainFailed, "Transaction chain is failed.");
212         }
213
214         @Override
215         public synchronized DOMStoreReadTransaction newReadOnlyTransaction() {
216             final DataTreeSnapshot snapshot;
217             checkFailed();
218             if (latestOutstandingTx != null) {
219                 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
220                 snapshot = latestOutstandingTx.getMutatedView();
221             } else {
222                 snapshot = dataTree.takeSnapshot();
223             }
224             return new SnapshotBackedReadTransaction(nextIdentifier(), snapshot);
225         }
226
227         @Override
228         public synchronized DOMStoreReadWriteTransaction newReadWriteTransaction() {
229             final DataTreeSnapshot snapshot;
230             checkFailed();
231             if (latestOutstandingTx != null) {
232                 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
233                 snapshot = latestOutstandingTx.getMutatedView();
234             } else {
235                 snapshot = dataTree.takeSnapshot();
236             }
237             final SnapshotBackedReadWriteTransaction ret = new SnapshotBackedReadWriteTransaction(nextIdentifier(),
238                     snapshot, this);
239             latestOutstandingTx = ret;
240             return ret;
241         }
242
243         @Override
244         public synchronized DOMStoreWriteTransaction newWriteOnlyTransaction() {
245             final DataTreeSnapshot snapshot;
246             checkFailed();
247             if (latestOutstandingTx != null) {
248                 checkState(latestOutstandingTx.isReady(), "Previous transaction in chain must be ready.");
249                 snapshot = latestOutstandingTx.getMutatedView();
250             } else {
251                 snapshot = dataTree.takeSnapshot();
252             }
253             final SnapshotBackedWriteTransaction ret = new SnapshotBackedWriteTransaction(nextIdentifier(), snapshot,
254                     this);
255             latestOutstandingTx = ret;
256             return ret;
257         }
258
259         @Override
260         public DOMStoreThreePhaseCommitCohort ready(final SnapshotBackedWriteTransaction tx) {
261             DOMStoreThreePhaseCommitCohort storeCohort = InMemoryDOMDataStore.this.ready(tx);
262             return new ChainedTransactionCommitImpl(tx, storeCohort, this);
263         }
264
265         @Override
266         public void close() {
267
268             // FIXME: this call doesn't look right here - listeningExecutor is shared and owned
269             // by the outer class.
270             //listeningExecutor.shutdownNow();
271         }
272
273         protected synchronized void onTransactionFailed(final SnapshotBackedWriteTransaction transaction,
274                 final Throwable t) {
275             chainFailed = true;
276
277         }
278
279         public synchronized void onTransactionCommited(final SnapshotBackedWriteTransaction transaction) {
280             // If commited transaction is latestOutstandingTx we clear
281             // latestOutstandingTx
282             // field in order to base new transactions on Datastore Data Tree
283             // directly.
284             if (transaction.equals(latestOutstandingTx)) {
285                 latestOutstandingTx = null;
286             }
287         }
288
289     }
290
291     private static class ChainedTransactionCommitImpl implements DOMStoreThreePhaseCommitCohort {
292
293         private final SnapshotBackedWriteTransaction transaction;
294         private final DOMStoreThreePhaseCommitCohort delegate;
295
296         private final DOMStoreTransactionChainImpl txChain;
297
298         protected ChainedTransactionCommitImpl(final SnapshotBackedWriteTransaction transaction,
299                 final DOMStoreThreePhaseCommitCohort delegate, final DOMStoreTransactionChainImpl txChain) {
300             super();
301             this.transaction = transaction;
302             this.delegate = delegate;
303             this.txChain = txChain;
304         }
305
306         @Override
307         public ListenableFuture<Boolean> canCommit() {
308             return delegate.canCommit();
309         }
310
311         @Override
312         public ListenableFuture<Void> preCommit() {
313             return delegate.preCommit();
314         }
315
316         @Override
317         public ListenableFuture<Void> abort() {
318             return delegate.abort();
319         }
320
321         @Override
322         public ListenableFuture<Void> commit() {
323             ListenableFuture<Void> commitFuture = delegate.commit();
324             Futures.addCallback(commitFuture, new FutureCallback<Void>() {
325                 @Override
326                 public void onFailure(final Throwable t) {
327                     txChain.onTransactionFailed(transaction, t);
328                 }
329
330                 @Override
331                 public void onSuccess(final Void result) {
332                     txChain.onTransactionCommited(transaction);
333                 }
334
335             });
336             return commitFuture;
337         }
338
339     }
340
341     private class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
342
343         private final SnapshotBackedWriteTransaction transaction;
344         private final DataTreeModification modification;
345
346         private ResolveDataChangeEventsTask listenerResolver;
347         private DataTreeCandidate candidate;
348
349         public ThreePhaseCommitImpl(final SnapshotBackedWriteTransaction writeTransaction) {
350             this.transaction = writeTransaction;
351             this.modification = transaction.getMutatedView();
352         }
353
354         @Override
355         public ListenableFuture<Boolean> canCommit() {
356             return listeningExecutor.submit(new Callable<Boolean>() {
357                 @Override
358                 public Boolean call() throws TransactionCommitFailedException {
359                     try {
360                         dataTree.validate(modification);
361                         LOG.debug("Store Transaction: {} can be committed", transaction.getIdentifier());
362                         return true;
363                     } catch (ConflictingModificationAppliedException e) {
364                         LOG.warn("Store Tx: {} Conflicting modification for {}.", transaction.getIdentifier(),
365                                 e.getPath());
366                         throw new OptimisticLockFailedException("Optimistic lock failed.",e);
367                     } catch (DataValidationFailedException e) {
368                         LOG.warn("Store Tx: {} Data Precondition failed for {}.", transaction.getIdentifier(),
369                                 e.getPath(), e);
370                         throw new TransactionCommitFailedException("Data did not pass validation.",e);
371                     }
372                 }
373             });
374         }
375
376         @Override
377         public ListenableFuture<Void> preCommit() {
378             return listeningExecutor.submit(new Callable<Void>() {
379                 @Override
380                 public Void call() {
381                     candidate = dataTree.prepare(modification);
382                     listenerResolver = ResolveDataChangeEventsTask.create(candidate, listenerTree,
383                             dataChangeListenerNotificationManager);
384                     return null;
385                 }
386             });
387         }
388
389         @Override
390         public ListenableFuture<Void> abort() {
391             candidate = null;
392             return Futures.immediateFuture(null);
393         }
394
395         @Override
396         public ListenableFuture<Void> commit() {
397             checkState(candidate != null, "Proposed subtree must be computed");
398
399             /*
400              * The commit has to occur atomically with regard to listener
401              * registrations.
402              */
403             synchronized (this) {
404                 dataTree.commit(candidate);
405
406                 for (ChangeListenerNotifyTask task : listenerResolver.call()) {
407                     LOG.trace("Scheduling invocation of listeners: {}", task);
408                     task.run();
409                 }
410             }
411
412             return Futures.immediateFuture(null);
413         }
414     }
415 }

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