CDS: use internal DataTree instance
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeCohort.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.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
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 ShardDataTreeCohort implements DOMStoreThreePhaseCommitCohort {
20     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeCohort.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     ShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction) {
28         this.dataTree = Preconditions.checkNotNull(dataTree);
29         this.transaction = Preconditions.checkNotNull(transaction);
30     }
31
32     @Override
33     public ListenableFuture<Boolean> canCommit() {
34         try {
35             dataTree.getDataTree().validate(transaction);
36             LOG.debug("Transaction {} validated", transaction);
37             return TRUE_FUTURE;
38         } catch (Exception e) {
39             return Futures.immediateFailedFuture(e);
40         }
41     }
42
43     @Override
44     public ListenableFuture<Void> preCommit() {
45         try {
46             candidate = dataTree.getDataTree().prepare(transaction);
47             /*
48              * FIXME: this is the place where we should be interacting with persistence, specifically by invoking
49              *        persist on the candidate (which gives us a Future).
50              */
51             LOG.debug("Transaction {} prepared candidate {}", transaction, candidate);
52             return VOID_FUTURE;
53         } catch (Exception e) {
54             LOG.debug("Transaction {} failed to prepare", transaction, e);
55             return Futures.immediateFailedFuture(e);
56         }
57     }
58
59     @Override
60     public ListenableFuture<Void> abort() {
61         // No-op, really
62         return VOID_FUTURE;
63     }
64
65     @Override
66     public ListenableFuture<Void> commit() {
67         try {
68             dataTree.getDataTree().commit(candidate);
69         } catch (Exception e) {
70             LOG.error("Transaction {} failed to commit", transaction, e);
71             return Futures.immediateFailedFuture(e);
72         }
73
74         LOG.debug("Transaction {} committed, proceeding to notify", transaction);
75         dataTree.notifyListeners(candidate);
76         return VOID_FUTURE;
77     }
78 }