2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
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;
27 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction) {
28 this.dataTree = Preconditions.checkNotNull(dataTree);
29 this.transaction = Preconditions.checkNotNull(transaction);
33 DataTreeCandidateTip getCandidate() {
38 public ListenableFuture<Boolean> canCommit() {
40 dataTree.getDataTree().validate(dataTreeModification());
41 LOG.trace("Transaction {} validated", transaction);
43 } catch (Exception e) {
44 return Futures.immediateFailedFuture(e);
49 public ListenableFuture<Void> preCommit() {
51 candidate = dataTree.getDataTree().prepare(dataTreeModification());
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).
56 LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
58 } catch (Exception e) {
59 LOG.debug("Transaction {} failed to prepare", transaction, e);
60 return Futures.immediateFailedFuture(e);
64 private DataTreeModification dataTreeModification() {
65 DataTreeModification dataTreeModification = transaction;
66 if(transaction instanceof PruningDataTreeModification){
67 dataTreeModification = ((PruningDataTreeModification) transaction).getDelegate();
69 return dataTreeModification;
73 public ListenableFuture<Void> abort() {
79 public ListenableFuture<Void> commit() {
81 dataTree.getDataTree().commit(candidate);
82 } catch (Exception e) {
83 LOG.error("Transaction {} failed to commit", transaction, e);
84 return Futures.immediateFailedFuture(e);
87 LOG.trace("Transaction {} committed, proceeding to notify", transaction);
88 dataTree.notifyListeners(candidate);