e8aab53883f87cf842752be04b94c0f171e2ef25
[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 static java.util.Objects.requireNonNull;
12
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;
24
25 class InMemoryDOMDataTreeShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
26
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);
30
31     private final DataTree dataTree;
32     private final DataTreeModification modification;
33     private DataTreeCandidate candidate;
34     private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
35
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);
42     }
43
44     @SuppressWarnings("checkstyle:IllegalCatch")
45     @Override
46     public ListenableFuture<Boolean> canCommit() {
47         try {
48             dataTree.validate(modification);
49             LOG.debug("DataTreeModification {} validated", modification);
50
51             return CAN_COMMIT_FUTURE;
52         } catch (DataValidationFailedException e) {
53             LOG.warn("Data validation failed for {}", modification);
54             LOG.trace("dataTree : {}", dataTree);
55
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);
61         }
62     }
63
64     @SuppressWarnings("checkstyle:IllegalCatch")
65     @Override
66     public ListenableFuture<Void> preCommit() {
67         try {
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);
74         }
75     }
76
77     @Override
78     public ListenableFuture<Void> abort() {
79         candidate = null;
80         return SUCCESSFUL_FUTURE;
81     }
82
83     @Override
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;
91     }
92 }