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.access.concepts.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
15 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
25 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
26 private static final ListenableFuture<Boolean> TRUE_FUTURE = Futures.immediateFuture(Boolean.TRUE);
27 private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
28 private final DataTreeModification transaction;
29 private final ShardDataTree dataTree;
30 private final TransactionIdentifier transactionId;
31 private DataTreeCandidateTip candidate;
33 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
34 final TransactionIdentifier transactionId) {
35 this.dataTree = Preconditions.checkNotNull(dataTree);
36 this.transaction = Preconditions.checkNotNull(transaction);
37 this.transactionId = Preconditions.checkNotNull(transactionId);
41 DataTreeCandidateTip getCandidate() {
46 public ListenableFuture<Boolean> canCommit() {
47 DataTreeModification modification = getDataTreeModification();
49 dataTree.getDataTree().validate(modification);
50 LOG.trace("Transaction {} validated", transaction);
53 catch (ConflictingModificationAppliedException e) {
54 LOG.warn("Store Tx {}: Conflicting modification for path {}.", transactionId, e.getPath());
55 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
56 } catch (DataValidationFailedException e) {
57 LOG.warn("Store Tx {}: Data validation failed for path {}.", transactionId, e.getPath(), e);
59 // For debugging purposes, allow dumping of the modification. Coupled with the above
60 // precondition log, it should allow us to understand what went on.
61 LOG.debug("Store Tx {}: modifications: {} tree: {}", transactionId, modification, dataTree.getDataTree());
63 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
64 } catch (Exception e) {
65 LOG.warn("Unexpected failure in validation phase", e);
66 return Futures.immediateFailedFuture(e);
71 public ListenableFuture<Void> preCommit() {
73 candidate = dataTree.getDataTree().prepare(getDataTreeModification());
75 * FIXME: this is the place where we should be interacting with persistence, specifically by invoking
76 * persist on the candidate (which gives us a Future).
78 LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
80 } catch (Exception e) {
81 if(LOG.isTraceEnabled()) {
82 LOG.trace("Transaction {} failed to prepare", transaction, e);
84 LOG.error("Transaction failed to prepare", e);
86 return Futures.immediateFailedFuture(e);
91 DataTreeModification getDataTreeModification() {
92 DataTreeModification dataTreeModification = transaction;
93 if(transaction instanceof PruningDataTreeModification){
94 dataTreeModification = ((PruningDataTreeModification) transaction).getResultingModification();
96 return dataTreeModification;
100 public ListenableFuture<Void> abort() {
106 public ListenableFuture<Void> commit() {
108 dataTree.getDataTree().commit(candidate);
109 } catch (Exception e) {
110 if(LOG.isTraceEnabled()) {
111 LOG.trace("Transaction {} failed to commit", transaction, e);
113 LOG.error("Transaction failed to commit", e);
115 return Futures.immediateFailedFuture(e);
118 LOG.trace("Transaction {} committed, proceeding to notify", transaction);
119 dataTree.notifyListeners(candidate);