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;
33 InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
34 final DataTreeModification modification) {
35 Preconditions.checkNotNull(dataTree);
36 this.dataTree = dataTree;
37 this.modification = modification;
41 public ListenableFuture<Boolean> canCommit() {
43 dataTree.validate(modification);
44 LOG.debug("DataTreeModification {} validated");
46 return CAN_COMMIT_FUTURE;
47 } catch (DataValidationFailedException e) {
48 LOG.warn("Data validation failed for {}", modification);
49 LOG.trace("dataTree : {}", dataTree);
51 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
52 } catch (Exception e) {
53 LOG.warn("Unexpected failure in validation phase", e);
54 return Futures.immediateFailedFuture(e);
59 public ListenableFuture<Void> preCommit() {
61 candidate = dataTree.prepare(modification);
62 return SUCCESSFUL_FUTURE;
63 } catch (Exception e) {
64 LOG.warn("Unexpected failure in preparation phase", e);
65 return Futures.immediateFailedFuture(e);
70 public ListenableFuture<Void> abort() {
72 return SUCCESSFUL_FUTURE;
76 public ListenableFuture<Void> commit() {
77 Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
78 dataTree.commit(candidate);
79 return SUCCESSFUL_FUTURE;