Bug 4202: submit shard transactions
[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 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;
22
23 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
24
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);
28
29     private final DataTree dataTree;
30     private final DataTreeModification modification;
31     private DataTreeCandidate candidate;
32
33     InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
34                                                    final DataTreeModification modification) {
35         Preconditions.checkNotNull(dataTree);
36         this.dataTree = dataTree;
37         this.modification = modification;
38     }
39
40     @Override
41     public ListenableFuture<Boolean> canCommit() {
42         try {
43             dataTree.validate(modification);
44             LOG.debug("DataTreeModification {} validated");
45
46             return CAN_COMMIT_FUTURE;
47         } catch (DataValidationFailedException e) {
48             LOG.warn("Data validation failed for {}", modification);
49             LOG.trace("dataTree : {}", dataTree);
50
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);
55         }
56     }
57
58     @Override
59     public ListenableFuture<Void> preCommit() {
60         try {
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);
66         }
67     }
68
69     @Override
70     public ListenableFuture<Void> abort() {
71         candidate = null;
72         return SUCCESSFUL_FUTURE;
73     }
74
75     @Override
76     public ListenableFuture<Void> commit() {
77         Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
78         dataTree.commit(candidate);
79         return SUCCESSFUL_FUTURE;
80     }
81 }