1f8c9cbcd372d7b0e20d4fa49f2b31035e87a15f
[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     private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
33
34     InMemoryDOMDataTreeShardThreePhaseCommitCohort(final DataTree dataTree,
35                                                    final DataTreeModification modification,
36                                                    final InMemoryDOMDataTreeShardChangePublisher changePublisher) {
37         this.dataTree = Preconditions.checkNotNull(dataTree);
38         this.modification = Preconditions.checkNotNull(modification);
39         this.changePublisher = Preconditions.checkNotNull(changePublisher);
40     }
41
42     @SuppressWarnings("checkstyle:IllegalCatch")
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(
55                     "Data did not pass validation.", e));
56         } catch (Exception e) {
57             LOG.warn("Unexpected failure in validation phase", e);
58             return Futures.immediateFailedFuture(e);
59         }
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @Override
64     public ListenableFuture<Void> preCommit() {
65         try {
66             candidate = dataTree.prepare(modification);
67             LOG.debug("DataTreeModification {} prepared", modification);
68             return SUCCESSFUL_FUTURE;
69         } catch (Exception e) {
70             LOG.warn("Unexpected failure in preparation phase", e);
71             return Futures.immediateFailedFuture(e);
72         }
73     }
74
75     @Override
76     public ListenableFuture<Void> abort() {
77         candidate = null;
78         return SUCCESSFUL_FUTURE;
79     }
80
81     @Override
82     public ListenableFuture<Void> commit() {
83         Preconditions.checkState(candidate != null, "Attempted to commit an aborted transaction");
84         LOG.debug("Commiting candidate {}", candidate);
85         dataTree.commit(candidate);
86         // publish this change for listeners
87         changePublisher.publishChange(candidate);
88         return SUCCESSFUL_FUTURE;
89     }
90 }