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