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
9 package org.opendaylight.controller.md.sal.dom.store.impl;
11 import static com.google.common.base.Preconditions.checkState;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
30 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
31 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
32 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
33 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
34 private final SnapshotBackedWriteTransaction<String> transaction;
35 private final DataTreeModification modification;
36 private final InMemoryDOMDataStore store;
37 private DataTreeCandidate candidate;
38 private final Exception operationError;
40 InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
41 final SnapshotBackedWriteTransaction<String> writeTransaction, final DataTreeModification modification,
42 final Exception operationError) {
43 this.transaction = Preconditions.checkNotNull(writeTransaction);
44 this.modification = Preconditions.checkNotNull(modification);
45 this.store = Preconditions.checkNotNull(store);
46 this.operationError = operationError;
49 private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
50 final Throwable ctx = transaction.getDebugContext();
52 LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 public final ListenableFuture<Boolean> canCommit() {
59 if (operationError != null) {
60 return Futures.immediateFailedFuture(operationError);
64 store.validate(modification);
65 LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
66 return CAN_COMMIT_FUTURE;
67 } catch (ConflictingModificationAppliedException e) {
68 LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
70 warnDebugContext(getTransaction());
71 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
72 } catch (DataValidationFailedException e) {
73 LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
75 warnDebugContext(getTransaction());
77 // For debugging purposes, allow dumping of the modification. Coupled with the above
78 // precondition log, it should allow us to understand what went on.
79 LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);
81 return Futures.immediateFailedFuture(new TransactionCommitFailedException(
82 "Data did not pass validation.", e));
83 } catch (RuntimeException e) {
84 LOG.warn("Unexpected failure in validation phase", e);
85 return Futures.immediateFailedFuture(e);
90 @SuppressWarnings("checkstyle:IllegalCatch")
91 public final ListenableFuture<Void> preCommit() {
93 candidate = store.prepare(modification);
94 return SUCCESSFUL_FUTURE;
95 } catch (RuntimeException e) {
96 LOG.warn("Unexpected failure in pre-commit phase", e);
97 return Futures.immediateFailedFuture(e);
102 public final ListenableFuture<Void> abort() {
104 return SUCCESSFUL_FUTURE;
107 protected final SnapshotBackedWriteTransaction<String> getTransaction() {
112 public ListenableFuture<Void> commit() {
113 checkState(candidate != null, "Proposed subtree must be computed");
116 * The commit has to occur atomically with regard to listener
119 store.commit(candidate);
120 return SUCCESSFUL_FUTURE;