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 static java.util.Objects.requireNonNull;
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.mdsal.common.api.TransactionCommitFailedException;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
27 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardThreePhaseCommitCohort.class);
28 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
29 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
31 private final DataTree dataTree;
32 private final DataTreeModification modification;
33 private DataTreeCandidate candidate;
34 private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
36 InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
37 final DataTreeModification modification,
38 final InMemoryDOMDataTreeShardChangePublisher changePublisher) {
39 this.dataTree = requireNonNull(dataTree);
40 this.modification = requireNonNull(modification);
41 this.changePublisher = requireNonNull(changePublisher);
44 @SuppressWarnings("checkstyle:IllegalCatch")
46 public ListenableFuture<Boolean> canCommit() {
48 dataTree.validate(modification);
49 LOG.debug("DataTreeModification {} validated", modification);
51 return CAN_COMMIT_FUTURE;
52 } catch (DataValidationFailedException e) {
53 LOG.warn("Data validation failed for {}", modification);
54 LOG.trace("dataTree : {}", dataTree);
56 return Futures.immediateFailedFuture(new TransactionCommitFailedException(
57 "Data did not pass validation.", e));
58 } catch (Exception e) {
59 LOG.warn("Unexpected failure in validation phase", e);
60 return Futures.immediateFailedFuture(e);
64 @SuppressWarnings("checkstyle:IllegalCatch")
66 public ListenableFuture<Void> preCommit() {
68 candidate = dataTree.prepare(modification);
69 LOG.debug("DataTreeModification {} prepared", modification);
70 return SUCCESSFUL_FUTURE;
71 } catch (Exception e) {
72 LOG.warn("Unexpected failure in preparation phase", e);
73 return Futures.immediateFailedFuture(e);
78 public ListenableFuture<Void> abort() {
80 return SUCCESSFUL_FUTURE;
84 public ListenableFuture<Void> commit() {
85 Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
86 LOG.debug("Commiting candidate {}", candidate);
87 dataTree.commit(candidate);
88 // publish this change for listeners
89 changePublisher.publishChange(candidate);
90 return SUCCESSFUL_FUTURE;