2 * Copyright (c) 2016 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.mdsal.dom.store.inmemory;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
15 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
25 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardThreePhaseCommitCohort.class);
26 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
27 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
29 private final DataTree dataTree;
30 private final DataTreeModification modification;
31 private DataTreeCandidate candidate;
32 private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
34 InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
35 final DataTreeModification modification,
36 final InMemoryDOMDataTreeShardChangePublisher changePublisher) {
37 this.dataTree = Preconditions.checkNotNull(dataTree);
38 this.modification = Preconditions.checkNotNull(modification);
39 this.changePublisher = Preconditions.checkNotNull(changePublisher);
42 @SuppressWarnings("checkstyle:IllegalCatch")
44 public ListenableFuture<Boolean> canCommit() {
46 dataTree.validate(modification);
47 LOG.debug("DataTreeModification {} validated", modification);
49 return CAN_COMMIT_FUTURE;
50 } catch (DataValidationFailedException e) {
51 LOG.warn("Data validation failed for {}", modification);
52 LOG.trace("dataTree : {}", dataTree);
54 return Futures.immediateFailedFuture(new TransactionCommitFailedException(
55 "Data did not pass validation.", e));
56 } catch (Exception e) {
57 LOG.warn("Unexpected failure in validation phase", e);
58 return Futures.immediateFailedFuture(e);
62 @SuppressWarnings("checkstyle:IllegalCatch")
64 public ListenableFuture<Void> preCommit() {
66 candidate = dataTree.prepare(modification);
67 LOG.debug("DataTreeModification {} prepared", modification);
68 return SUCCESSFUL_FUTURE;
69 } catch (Exception e) {
70 LOG.warn("Unexpected failure in preparation phase", e);
71 return Futures.immediateFailedFuture(e);
76 public ListenableFuture<Void> abort() {
78 return SUCCESSFUL_FUTURE;
82 public ListenableFuture<Void> commit() {
83 Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
84 LOG.debug("Commiting candidate {}", candidate);
85 dataTree.commit(candidate);
86 // publish this change for listeners
87 changePublisher.publishChange(candidate);
88 return SUCCESSFUL_FUTURE;