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