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