Update DOMStoreThreePhaseCommitCohort design
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / TestCommitCohort.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.mdsal.dom.broker;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.common.api.CommitInfo;
13 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
14 import org.opendaylight.yangtools.yang.common.Empty;
15
16 public enum TestCommitCohort implements DOMStoreThreePhaseCommitCohort {
17     ALLWAYS_SUCCESS(true, true, true, true),
18     CAN_COMMIT_FAILED(false, false, false, true),
19     PRE_COMMIT_FAILED(true, false, false, true),
20     COMMIT_FAILED(true, true, false, true);
21
22     private final ListenableFuture<Boolean> canCommit;
23     private final ListenableFuture<Empty> preCommit;
24     private final ListenableFuture<CommitInfo> commit;
25     private final ListenableFuture<Empty> abort;
26
27     TestCommitCohort(final boolean canCommit, final boolean preCommit,
28             final boolean commit, final boolean abort) {
29         this.canCommit = Futures.immediateFuture(canCommit);
30         this.preCommit = immediate(canCommit, new IllegalStateException());
31         this.commit = commit ? CommitInfo.emptyFluentFuture()
32             : Futures.immediateFailedFuture(new IllegalStateException());
33         this.abort = immediate(abort, new IllegalStateException());
34     }
35
36
37     @Override
38     public ListenableFuture<Boolean> canCommit() {
39         return canCommit;
40     }
41
42     @Override
43     public ListenableFuture<Empty> preCommit() {
44         return preCommit;
45     }
46
47     @Override
48     public ListenableFuture<Empty> abort() {
49         return abort;
50     }
51
52     @Override
53     public ListenableFuture<CommitInfo> commit() {
54         return commit;
55     }
56
57     private static ListenableFuture<Empty> immediate(final boolean isSuccess, final Exception except) {
58         return isSuccess ? Futures.immediateFuture(Empty.value()) : Futures.immediateFailedFuture(except);
59     }
60 }