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