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.controller.cluster.sharding;
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 java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
17 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
18 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
21 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Proxy {@link DOMDataTreeShardWriteTransaction} that creates a proxy cursor that translates all calls into
28 * {@link ClientTransaction} calls.
30 class ShardProxyTransaction implements DOMDataTreeShardWriteTransaction {
32 private static final Logger LOG = LoggerFactory.getLogger(ShardProxyTransaction.class);
33 private static final ListenableFuture<Void> NULL_FUTURE = Futures.immediateFuture(null);
34 private static final ListenableFuture<Boolean> VALIDATE_FUTURE = Futures.immediateFuture(true);
36 private final DOMDataTreeIdentifier shardRoot;
37 private final Collection<DOMDataTreeIdentifier> prefixes;
38 private final DataStoreClient client;
39 private final ClientLocalHistory history;
40 private ClientTransaction currentTx;
41 private DOMStoreThreePhaseCommitCohort cohort;
44 ShardProxyTransaction(final DOMDataTreeIdentifier shardRoot, final Collection<DOMDataTreeIdentifier> prefixes,
45 final DataStoreClient client) {
46 this.shardRoot = Preconditions.checkNotNull(shardRoot);
47 this.prefixes = Preconditions.checkNotNull(prefixes);
48 this.client = Preconditions.checkNotNull(client);
49 history = client.createLocalHistory();
50 currentTx = history.createTransaction();
55 public DOMDataTreeWriteCursor createCursor(@Nonnull final DOMDataTreeIdentifier prefix) {
56 checkAvailable(prefix);
58 return currentTx.openCursor();
61 private void checkAvailable(final DOMDataTreeIdentifier prefix) {
62 for (final DOMDataTreeIdentifier p : prefixes) {
63 if (p.contains(prefix)) {
67 throw new IllegalArgumentException("Prefix[" + prefix + "] not available for this transaction. "
68 + "Available prefixes: " + prefixes);
73 LOG.debug("Readying transaction for shard {}", shardRoot);
75 Preconditions.checkState(cohort == null, "Transaction was readied already");
76 cohort = currentTx.ready();
86 if (currentTx != null) {
93 public ListenableFuture<Void> submit() {
94 LOG.debug("Submitting transaction for shard {}", shardRoot);
96 Preconditions.checkNotNull(cohort, "Transaction not readied yet");
101 public ListenableFuture<Boolean> validate() {
102 LOG.debug("Validating transaction for shard {}", shardRoot);
104 Preconditions.checkNotNull(cohort, "Transaction not readied yet");
105 return VALIDATE_FUTURE;
109 public ListenableFuture<Void> prepare() {
110 LOG.debug("Preparing transaction for shard {}", shardRoot);
112 Preconditions.checkNotNull(cohort, "Transaction not readied yet");
117 public ListenableFuture<Void> commit() {
118 LOG.debug("Committing transaction for shard {}", shardRoot);
120 Preconditions.checkNotNull(cohort, "Transaction not readied yet");