f0e7abfb0ab371d98b6a91f1723db38da4d9d772
[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 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.controller.sal.core.spi.data.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 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
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     private final Exception operationError;
38
39     public InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
40                                                   final SnapshotBackedWriteTransaction<String> writeTransaction,
41                                                   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     public final ListenableFuture<Boolean> canCommit() {
58         if (operationError != null) {
59             return Futures.immediateFailedFuture(operationError);
60         }
61
62         try {
63             store.validate(modification);
64             LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
65             return CAN_COMMIT_FUTURE;
66         } catch (ConflictingModificationAppliedException e) {
67             LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
68                     e.getPath());
69             warnDebugContext(getTransaction());
70             return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
71         } catch (DataValidationFailedException e) {
72             LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
73                     e.getPath(), e);
74             warnDebugContext(getTransaction());
75
76             // For debugging purposes, allow dumping of the modification. Coupled with the above
77             // precondition log, it should allow us to understand what went on.
78             LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);
79
80             return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
81         } catch (Exception e) {
82             LOG.warn("Unexpected failure in validation phase", e);
83             return Futures.immediateFailedFuture(e);
84         }
85     }
86
87     @Override
88     public final ListenableFuture<Void> preCommit() {
89         try {
90             candidate = store.prepare(modification);
91             return SUCCESSFUL_FUTURE;
92         } catch (Exception e) {
93             LOG.warn("Unexpected failure in pre-commit phase", e);
94             return Futures.immediateFailedFuture(e);
95         }
96     }
97
98     @Override
99     public final ListenableFuture<Void> abort() {
100         candidate = null;
101         return SUCCESSFUL_FUTURE;
102     }
103
104     protected final SnapshotBackedWriteTransaction<String> getTransaction() {
105         return transaction;
106     }
107
108     @Override
109     public ListenableFuture<Void> commit() {
110         checkState(candidate != null, "Proposed subtree must be computed");
111
112         /*
113          * The commit has to occur atomically with regard to listener
114          * registrations.
115          */
116         store.commit(candidate);
117         return SUCCESSFUL_FUTURE;
118     }
119 }
120