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;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
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.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
19 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
28 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
29 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
30 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
31 private final SnapshotBackedWriteTransaction<String> transaction;
32 private final DataTreeModification modification;
33 private final InMemoryDOMDataStore store;
34 private DataTreeCandidate candidate;
36 public InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store, final SnapshotBackedWriteTransaction<String> writeTransaction, final DataTreeModification modification) {
37 this.transaction = Preconditions.checkNotNull(writeTransaction);
38 this.modification = Preconditions.checkNotNull(modification);
39 this.store = Preconditions.checkNotNull(store);
42 private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
43 final Throwable ctx = transaction.getDebugContext();
45 LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
50 public final ListenableFuture<Boolean> canCommit() {
52 store.validate(modification);
53 LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
54 return CAN_COMMIT_FUTURE;
55 } catch (ConflictingModificationAppliedException e) {
56 LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
58 warnDebugContext(getTransaction());
59 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
60 } catch (DataValidationFailedException e) {
61 LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
63 warnDebugContext(getTransaction());
65 // For debugging purposes, allow dumping of the modification. Coupled with the above
66 // precondition log, it should allow us to understand what went on.
67 LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);
69 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
70 } catch (Exception e) {
71 LOG.warn("Unexpected failure in validation phase", e);
72 return Futures.immediateFailedFuture(e);
77 public final ListenableFuture<Void> preCommit() {
79 candidate = store.prepare(modification);
80 return SUCCESSFUL_FUTURE;
81 } catch (Exception e) {
82 LOG.warn("Unexpected failure in pre-commit phase", e);
83 return Futures.immediateFailedFuture(e);
88 public final ListenableFuture<Void> abort() {
90 return SUCCESSFUL_FUTURE;
93 protected final SnapshotBackedWriteTransaction<String> getTransaction() {
98 public ListenableFuture<Void> commit() {
99 checkState(candidate != null, "Proposed subtree must be computed");
102 * The commit has to occur atomically with regard to listener
105 store.commit(candidate);
106 return SUCCESSFUL_FUTURE;