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