5f20089c5cbe2475acc410eb9e8aefee2cb1a542
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMDataTreeShardThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
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;
23
24 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
25
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);
29
30     private final DataTree dataTree;
31     private final DataTreeModification modification;
32     private DataTreeCandidate candidate;
33     private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
34
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);
41     }
42
43     @Override
44     public ListenableFuture<Boolean> canCommit() {
45         try {
46             dataTree.validate(modification);
47             LOG.debug("DataTreeModification {} validated", modification);
48
49             return CAN_COMMIT_FUTURE;
50         } catch (DataValidationFailedException e) {
51             LOG.warn("Data validation failed for {}", modification);
52             LOG.trace("dataTree : {}", dataTree);
53
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);
58         }
59     }
60
61     @Override
62     public ListenableFuture<Void> preCommit() {
63         try {
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);
70         }
71     }
72
73     @Override
74     public ListenableFuture<Void> abort() {
75         candidate = null;
76         return SUCCESSFUL_FUTURE;
77     }
78
79     @Override
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;
87     }
88 }