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 java.util.Collections;
15 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
26 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardThreePhaseCommitCohort.class);
27 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
28 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
30 private final DataTree dataTree;
31 private final DataTreeModification modification;
32 private DataTreeCandidate candidate;
33 private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
35 InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
36 final DataTreeModification modification,
37 final InMemoryDOMDataTreeShardChangePublisher changePublisher) {
38 this.dataTree = Preconditions.checkNotNull(dataTree);
39 this.modification = Preconditions.checkNotNull(modification);
40 this.changePublisher = Preconditions.checkNotNull(changePublisher);
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("Data did not pass validation.", e));
55 } catch (Exception e) {
56 LOG.warn("Unexpected failure in validation phase", e);
57 return Futures.immediateFailedFuture(e);
62 public ListenableFuture<Void> preCommit() {
64 candidate = dataTree.prepare(modification);
65 LOG.debug("DataTreeModification {} prepared", modification);
66 return SUCCESSFUL_FUTURE;
67 } catch (Exception e) {
68 LOG.warn("Unexpected failure in preparation phase", e);
69 return Futures.immediateFailedFuture(e);
74 public ListenableFuture<Void> abort() {
76 return SUCCESSFUL_FUTURE;
80 public ListenableFuture<Void> commit() {
81 Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
82 LOG.debug("Commiting candidate {}", candidate);
83 dataTree.commit(candidate);
84 // publish this change for listeners
85 changePublisher.publishChange(candidate);
86 return SUCCESSFUL_FUTURE;