b3c8dfca33f6c6553872bd243aa19673f598eebf
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / ShardProxyTransaction.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.controller.cluster.sharding;
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 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;
25
26 /**
27  * Proxy {@link DOMDataTreeShardWriteTransaction} that creates a proxy cursor that translates all calls into
28  * {@link ClientTransaction} calls.
29  */
30 class ShardProxyTransaction implements DOMDataTreeShardWriteTransaction {
31
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);
35
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;
42
43
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();
51     }
52
53     @Nonnull
54     @Override
55     public DOMDataTreeWriteCursor createCursor(@Nonnull final DOMDataTreeIdentifier prefix) {
56         checkAvailable(prefix);
57
58         return currentTx.openCursor();
59     }
60
61     private void checkAvailable(final DOMDataTreeIdentifier prefix) {
62         for (final DOMDataTreeIdentifier p : prefixes) {
63             if (p.contains(prefix)) {
64                 return;
65             }
66         }
67         throw new IllegalArgumentException("Prefix[" + prefix + "] not available for this transaction. "
68                 + "Available prefixes: " + prefixes);
69     }
70
71     @Override
72     public void ready() {
73         LOG.debug("Readying transaction for shard {}", shardRoot);
74
75         Preconditions.checkState(cohort == null, "Transaction was readied already");
76         cohort = currentTx.ready();
77         currentTx = null;
78     }
79
80     @Override
81     public void close() {
82         if (cohort != null) {
83             cohort.abort();
84             cohort = null;
85         }
86         if (currentTx != null) {
87             currentTx.abort();
88             currentTx = null;
89         }
90     }
91
92     @Override
93     public ListenableFuture<Void> submit() {
94         LOG.debug("Submitting transaction for shard {}", shardRoot);
95
96         Preconditions.checkNotNull(cohort, "Transaction not readied yet");
97         return NULL_FUTURE;
98     }
99
100     @Override
101     public ListenableFuture<Boolean> validate() {
102         LOG.debug("Validating transaction for shard {}", shardRoot);
103
104         Preconditions.checkNotNull(cohort, "Transaction not readied yet");
105         return VALIDATE_FUTURE;
106     }
107
108     @Override
109     public ListenableFuture<Void> prepare() {
110         LOG.debug("Preparing transaction for shard {}", shardRoot);
111
112         Preconditions.checkNotNull(cohort, "Transaction not readied yet");
113         return NULL_FUTURE;
114     }
115
116     @Override
117     public ListenableFuture<Void> commit() {
118         LOG.debug("Committing transaction for shard {}", shardRoot);
119
120         Preconditions.checkNotNull(cohort, "Transaction not readied yet");
121         return NULL_FUTURE;
122     }
123 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.