Clean up binding-util tests
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ForeignShardThreePhaseCommitCohort.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.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.spi.shard.ForeignShardModificationContext;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class ForeignShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
22
23     private static final Logger LOG = LoggerFactory.getLogger(ForeignShardThreePhaseCommitCohort.class);
24
25     private static final ListenableFuture<Boolean> SUCCESS_VALIDATE = Futures.immediateFuture(Boolean.TRUE);
26     private static final ListenableFuture<Void> SUCCESS_FUTURE = Futures.immediateFuture(null);
27
28     private final DOMDataTreeIdentifier prefix;
29     private final ForeignShardModificationContext shard;
30
31     public ForeignShardThreePhaseCommitCohort(final DOMDataTreeIdentifier prefix,
32             final ForeignShardModificationContext shard) {
33         this.prefix = requireNonNull(prefix);
34         this.shard = requireNonNull(shard);
35     }
36
37     @Override
38     public ListenableFuture<Boolean> canCommit() {
39         LOG.debug("Validating transaction on foreign shard {}", prefix);
40         return shard.isModified() ? shard.validate() : SUCCESS_VALIDATE;
41     }
42
43     @Override
44     public ListenableFuture<Void> preCommit() {
45         LOG.debug("Preparing transaction on foreign shard {}", prefix);
46         return shard.isModified() ? shard.prepare() : SUCCESS_FUTURE;
47     }
48
49     @Override
50     public ListenableFuture<Void> abort() {
51         LOG.debug("Aborting transaction of foreign shard {}", prefix);
52         shard.closeForeignTransaction();
53         return SUCCESS_FUTURE;
54     }
55
56     @Override
57     public ListenableFuture<Void> commit() {
58         LOG.debug("Submitting transaction on foreign shard {}", prefix);
59         return shard.isModified() ? shard.submit() : SUCCESS_FUTURE;
60     }
61 }