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