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