Bug 3372: Convert backend DataTree exceptions appropriately
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SimpleShardDataTreeCohort.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 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
24     private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
25     private static final ListenableFuture<Boolean> TRUE_FUTURE = Futures.immediateFuture(Boolean.TRUE);
26     private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
27     private final DataTreeModification transaction;
28     private final ShardDataTree dataTree;
29     private final String transactionId;
30     private DataTreeCandidateTip candidate;
31
32     SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
33             final String transactionId) {
34         this.dataTree = Preconditions.checkNotNull(dataTree);
35         this.transaction = Preconditions.checkNotNull(transaction);
36         this.transactionId = transactionId;
37     }
38
39     @Override
40     DataTreeCandidateTip getCandidate() {
41         return candidate;
42     }
43
44     @Override
45     public ListenableFuture<Boolean> canCommit() {
46         DataTreeModification modification = dataTreeModification();
47         try {
48             dataTree.getDataTree().validate(modification);
49             LOG.trace("Transaction {} validated", transaction);
50             return TRUE_FUTURE;
51         }
52         catch (ConflictingModificationAppliedException e) {
53             LOG.warn("Store Tx {}: Conflicting modification for path {}.", transactionId, e.getPath());
54             return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
55         } catch (DataValidationFailedException e) {
56             LOG.warn("Store Tx {}: Data validation failed for path {}.", transactionId, e.getPath(), e);
57
58             // For debugging purposes, allow dumping of the modification. Coupled with the above
59             // precondition log, it should allow us to understand what went on.
60             LOG.debug("Store Tx {}: modifications: {} tree: {}", transactionId, modification, dataTree.getDataTree());
61
62             return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
63         } catch (Exception e) {
64             LOG.warn("Unexpected failure in validation phase", e);
65             return Futures.immediateFailedFuture(e);
66         }
67     }
68
69     @Override
70     public ListenableFuture<Void> preCommit() {
71         try {
72             candidate = dataTree.getDataTree().prepare(dataTreeModification());
73             /*
74              * FIXME: this is the place where we should be interacting with persistence, specifically by invoking
75              *        persist on the candidate (which gives us a Future).
76              */
77             LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
78             return VOID_FUTURE;
79         } catch (Exception e) {
80             LOG.debug("Transaction {} failed to prepare", transaction, e);
81             return Futures.immediateFailedFuture(e);
82         }
83     }
84
85     private DataTreeModification dataTreeModification() {
86         DataTreeModification dataTreeModification = transaction;
87         if(transaction instanceof PruningDataTreeModification){
88             dataTreeModification = ((PruningDataTreeModification) transaction).getDelegate();
89         }
90         return dataTreeModification;
91     }
92
93     @Override
94     public ListenableFuture<Void> abort() {
95         // No-op, really
96         return VOID_FUTURE;
97     }
98
99     @Override
100     public ListenableFuture<Void> commit() {
101         try {
102             dataTree.getDataTree().commit(candidate);
103         } catch (Exception e) {
104             LOG.error("Transaction {} failed to commit", transaction, e);
105             return Futures.immediateFailedFuture(e);
106         }
107
108         LOG.trace("Transaction {} committed, proceeding to notify", transaction);
109         dataTree.notifyListeners(candidate);
110         return VOID_FUTURE;
111     }
112 }