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