2b6b39684a271976be496cd366eda3ef0797b7c9
[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.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
20     private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
21     private static final ListenableFuture<Boolean> TRUE_FUTURE = Futures.immediateFuture(Boolean.TRUE);
22     private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
23     private final DataTreeModification transaction;
24     private final ShardDataTree dataTree;
25     private DataTreeCandidateTip candidate;
26
27     SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction) {
28         this.dataTree = Preconditions.checkNotNull(dataTree);
29         this.transaction = Preconditions.checkNotNull(transaction);
30     }
31
32     @Override
33     DataTreeCandidateTip getCandidate() {
34         return candidate;
35     }
36
37     @Override
38     public ListenableFuture<Boolean> canCommit() {
39         try {
40             dataTree.getDataTree().validate(dataTreeModification());
41             LOG.trace("Transaction {} validated", transaction);
42             return TRUE_FUTURE;
43         } catch (Exception e) {
44             return Futures.immediateFailedFuture(e);
45         }
46     }
47
48     @Override
49     public ListenableFuture<Void> preCommit() {
50         try {
51             candidate = dataTree.getDataTree().prepare(dataTreeModification());
52             /*
53              * FIXME: this is the place where we should be interacting with persistence, specifically by invoking
54              *        persist on the candidate (which gives us a Future).
55              */
56             LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
57             return VOID_FUTURE;
58         } catch (Exception e) {
59             LOG.debug("Transaction {} failed to prepare", transaction, e);
60             return Futures.immediateFailedFuture(e);
61         }
62     }
63
64     private DataTreeModification dataTreeModification() {
65         DataTreeModification dataTreeModification = transaction;
66         if(transaction instanceof PruningDataTreeModification){
67             dataTreeModification = ((PruningDataTreeModification) transaction).getDelegate();
68         }
69         return dataTreeModification;
70     }
71
72     @Override
73     public ListenableFuture<Void> abort() {
74         // No-op, really
75         return VOID_FUTURE;
76     }
77
78     @Override
79     public ListenableFuture<Void> commit() {
80         try {
81             dataTree.getDataTree().commit(candidate);
82         } catch (Exception e) {
83             LOG.error("Transaction {} failed to commit", transaction, e);
84             return Futures.immediateFailedFuture(e);
85         }
86
87         LOG.trace("Transaction {} committed, proceeding to notify", transaction);
88         dataTree.notifyListeners(candidate);
89         return VOID_FUTURE;
90     }
91 }