Merge "BUG-2288: deprecate old binding Notification API elements"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / InMemoryDOMStoreThreePhaseCommitCohort.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import static com.google.common.base.Preconditions.checkState;
4 import com.google.common.base.Preconditions;
5 import com.google.common.util.concurrent.Futures;
6 import com.google.common.util.concurrent.ListenableFuture;
7 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
8 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
9 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
10 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
11 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
20     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
21     private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
22     private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
23     private final SnapshotBackedWriteTransaction<String> transaction;
24     private final DataTreeModification modification;
25     private final InMemoryDOMDataStore store;
26     private DataTreeCandidate candidate;
27
28     public InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store, final SnapshotBackedWriteTransaction<String> writeTransaction, final DataTreeModification modification) {
29         this.transaction = Preconditions.checkNotNull(writeTransaction);
30         this.modification = Preconditions.checkNotNull(modification);
31         this.store = Preconditions.checkNotNull(store);
32     }
33
34     private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
35         final Throwable ctx = transaction.getDebugContext();
36         if (ctx != null) {
37             LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
38         }
39     }
40
41     @Override
42     public final ListenableFuture<Boolean> canCommit() {
43         try {
44             store.validate(modification);
45             LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
46             return CAN_COMMIT_FUTURE;
47         } catch (ConflictingModificationAppliedException e) {
48             LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
49                     e.getPath());
50             warnDebugContext(getTransaction());
51             return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
52         } catch (DataValidationFailedException e) {
53             LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
54                     e.getPath(), e);
55             warnDebugContext(getTransaction());
56
57             // For debugging purposes, allow dumping of the modification. Coupled with the above
58             // precondition log, it should allow us to understand what went on.
59             LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);
60
61             return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
62         } catch (Exception e) {
63             LOG.warn("Unexpected failure in validation phase", e);
64             return Futures.immediateFailedFuture(e);
65         }
66     }
67
68     @Override
69     public final ListenableFuture<Void> preCommit() {
70         try {
71             candidate = store.prepare(modification);
72             return SUCCESSFUL_FUTURE;
73         } catch (Exception e) {
74             LOG.warn("Unexpected failure in pre-commit phase", e);
75             return Futures.immediateFailedFuture(e);
76         }
77     }
78
79     @Override
80     public final ListenableFuture<Void> abort() {
81         candidate = null;
82         return SUCCESSFUL_FUTURE;
83     }
84
85     protected final SnapshotBackedWriteTransaction<String> getTransaction() {
86         return transaction;
87     }
88
89     @Override
90     public ListenableFuture<Void> commit() {
91         checkState(candidate != null, "Proposed subtree must be computed");
92
93         /*
94          * The commit has to occur atomically with regard to listener
95          * registrations.
96          */
97         store.commit(candidate);
98         return SUCCESSFUL_FUTURE;
99     }
100 }