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.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;
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;
26 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction) {
27 this.dataTree = Preconditions.checkNotNull(dataTree);
28 this.transaction = Preconditions.checkNotNull(transaction);
32 DataTreeCandidateTip getCandidate() {
37 public ListenableFuture<Boolean> canCommit() {
39 dataTree.getDataTree().validate(transaction);
40 LOG.debug("Transaction {} validated", transaction);
42 } catch (Exception e) {
43 return Futures.immediateFailedFuture(e);
48 public ListenableFuture<Void> preCommit() {
50 candidate = dataTree.getDataTree().prepare(transaction);
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).
55 LOG.debug("Transaction {} prepared candidate {}", transaction, candidate);
57 } catch (Exception e) {
58 LOG.debug("Transaction {} failed to prepare", transaction, e);
59 return Futures.immediateFailedFuture(e);
64 public ListenableFuture<Void> abort() {
70 public ListenableFuture<Void> commit() {
72 dataTree.getDataTree().commit(candidate);
73 } catch (Exception e) {
74 LOG.error("Transaction {} failed to commit", transaction, e);
75 return Futures.immediateFailedFuture(e);
78 LOG.debug("Transaction {} committed, proceeding to notify", transaction);
79 dataTree.notifyListeners(candidate);