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 org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
29 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
30 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
31 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
32 private final SnapshotBackedWriteTransaction<String> transaction;
33 private final DataTreeModification modification;
34 private final InMemoryDOMDataStore store;
35 private DataTreeCandidate candidate;
36 private final Exception operationError;
38 InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
39 final SnapshotBackedWriteTransaction<String> writeTransaction, final DataTreeModification modification,
40 final Exception operationError) {
41 this.transaction = Preconditions.checkNotNull(writeTransaction);
42 this.modification = Preconditions.checkNotNull(modification);
43 this.store = Preconditions.checkNotNull(store);
44 this.operationError = operationError;
47 private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
48 final Throwable ctx = transaction.getDebugContext();
50 LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
55 @SuppressWarnings("checkstyle:IllegalCatch")
56 public final ListenableFuture<Boolean> canCommit() {
57 if (operationError != null) {
58 return Futures.immediateFailedFuture(operationError);
62 store.validate(modification);
63 LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
64 return CAN_COMMIT_FUTURE;
65 } catch (ConflictingModificationAppliedException e) {
66 LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
68 warnDebugContext(getTransaction());
69 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
70 } catch (DataValidationFailedException e) {
71 LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
73 warnDebugContext(getTransaction());
75 // For debugging purposes, allow dumping of the modification. Coupled with the above
76 // precondition log, it should allow us to understand what went on.
77 LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);
79 return Futures.immediateFailedFuture(new TransactionCommitFailedException(
80 "Data did not pass validation.", e));
81 } catch (RuntimeException e) {
82 LOG.warn("Unexpected failure in validation phase", e);
83 return Futures.immediateFailedFuture(e);
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 public final ListenableFuture<Void> preCommit() {
91 candidate = store.prepare(modification);
92 return SUCCESSFUL_FUTURE;
93 } catch (RuntimeException e) {
94 LOG.warn("Unexpected failure in pre-commit phase", e);
95 return Futures.immediateFailedFuture(e);
100 public final ListenableFuture<Void> abort() {
102 return SUCCESSFUL_FUTURE;
105 protected final SnapshotBackedWriteTransaction<String> getTransaction() {
110 public ListenableFuture<Void> commit() {
111 checkState(candidate != null, "Proposed subtree must be computed");
114 * The commit has to occur atomically with regard to listener
117 store.commit(candidate);
118 return SUCCESSFUL_FUTURE;