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