2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.dom.store.inmemory;
11 import static java.util.Objects.requireNonNull;
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;
21 public class ForeignShardThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
23 private static final Logger LOG = LoggerFactory.getLogger(ForeignShardThreePhaseCommitCohort.class);
25 private static final ListenableFuture<Boolean> SUCCESS_VALIDATE = Futures.immediateFuture(Boolean.TRUE);
26 private static final ListenableFuture<Void> SUCCESS_FUTURE = Futures.immediateFuture(null);
28 private final DOMDataTreeIdentifier prefix;
29 private final ForeignShardModificationContext shard;
31 public ForeignShardThreePhaseCommitCohort(final DOMDataTreeIdentifier prefix,
32 final ForeignShardModificationContext shard) {
33 this.prefix = requireNonNull(prefix);
34 this.shard = requireNonNull(shard);
38 public ListenableFuture<Boolean> canCommit() {
39 LOG.debug("Validating transaction on foreign shard {}", prefix);
40 return shard.isModified() ? shard.validate() : SUCCESS_VALIDATE;
44 public ListenableFuture<Void> preCommit() {
45 LOG.debug("Preparing transaction on foreign shard {}", prefix);
46 return shard.isModified() ? shard.prepare() : SUCCESS_FUTURE;
50 public ListenableFuture<Void> abort() {
51 LOG.debug("Aborting transaction of foreign shard {}", prefix);
52 shard.closeForeignTransaction();
53 return SUCCESS_FUTURE;
57 public ListenableFuture<Void> commit() {
58 LOG.debug("Submitting transaction on foreign shard {}", prefix);
59 return shard.isModified() ? shard.submit() : SUCCESS_FUTURE;