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