Adjust to yangtools-2.0.0 changes
[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.AsyncFunction;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
28 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardWriteTransaction;
29 import org.opendaylight.mdsal.dom.spi.shard.ForeignShardModificationContext;
30 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
31 import org.opendaylight.mdsal.dom.store.inmemory.ForeignShardThreePhaseCommitCohort;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Proxy {@link DOMDataTreeShardWriteTransaction} that creates a proxy cursor that translates all calls into
38  * {@link ClientTransaction} calls.
39  */
40 class ShardProxyTransaction implements DOMDataTreeShardWriteTransaction {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ShardProxyTransaction.class);
43
44     private final DOMDataTreeIdentifier shardRoot;
45     private final Collection<DOMDataTreeIdentifier> prefixes;
46     private final DistributedShardModification modification;
47     private ClientTransaction currentTx;
48     private final List<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>();
49
50     private DOMDataTreeWriteCursor cursor = null;
51
52     ShardProxyTransaction(final DOMDataTreeIdentifier shardRoot,
53                           final Collection<DOMDataTreeIdentifier> prefixes,
54                           final DistributedShardModification modification) {
55         this.shardRoot = Preconditions.checkNotNull(shardRoot);
56         this.prefixes = Preconditions.checkNotNull(prefixes);
57         this.modification = Preconditions.checkNotNull(modification);
58     }
59
60     private DOMDataTreeWriteCursor getCursor() {
61         if (cursor == null) {
62             cursor = new DistributedShardModificationCursor(modification, this);
63         }
64         return cursor;
65     }
66
67     @Nonnull
68     @Override
69     public DOMDataTreeWriteCursor createCursor(@Nonnull final DOMDataTreeIdentifier prefix) {
70         checkAvailable(prefix);
71         final YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
72         final DOMDataTreeWriteCursor ret = getCursor();
73         ret.enter(relativePath.getPathArguments());
74         return ret;
75     }
76
77     void cursorClosed() {
78         cursor = null;
79         modification.cursorClosed();
80     }
81
82     private void checkAvailable(final DOMDataTreeIdentifier prefix) {
83         for (final DOMDataTreeIdentifier p : prefixes) {
84             if (p.contains(prefix)) {
85                 return;
86             }
87         }
88         throw new IllegalArgumentException("Prefix[" + prefix + "] not available for this transaction. "
89                 + "Available prefixes: " + prefixes);
90     }
91
92     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
93         final Optional<YangInstanceIdentifier> relative =
94                 path.relativeTo(modification.getPrefix().getRootIdentifier());
95         Preconditions.checkArgument(relative.isPresent());
96         return relative.get();
97     }
98
99     @Override
100     public void ready() {
101         LOG.debug("Readying transaction for shard {}", shardRoot);
102
103         Preconditions.checkNotNull(modification, "Attempting to ready an empty transaction.");
104
105         cohorts.add(modification.seal());
106         for (Entry<DOMDataTreeIdentifier, ForeignShardModificationContext> entry
107                 : modification.getChildShards().entrySet()) {
108             cohorts.add(new ForeignShardThreePhaseCommitCohort(entry.getKey(), entry.getValue()));
109         }
110     }
111
112     @Override
113     public void close() {
114         cohorts.forEach(DOMStoreThreePhaseCommitCohort::abort);
115         cohorts.clear();
116
117         if (currentTx != null) {
118             currentTx.abort();
119             currentTx = null;
120         }
121     }
122
123     @Override
124     public ListenableFuture<Void> submit() {
125         LOG.debug("Submitting transaction for shard {}", shardRoot);
126
127         checkTransactionReadied();
128
129         final AsyncFunction<Boolean, Void> validateFunction = input -> prepare();
130         final AsyncFunction<Void, Void> prepareFunction = input -> commit();
131
132         // transform validate into prepare
133         final ListenableFuture<Void> prepareFuture = Futures.transformAsync(validate(), validateFunction,
134             MoreExecutors.directExecutor());
135         // transform prepare into commit and return as submit result
136         return Futures.transformAsync(prepareFuture, prepareFunction, MoreExecutors.directExecutor());
137     }
138
139     private void checkTransactionReadied() {
140         Preconditions.checkState(!cohorts.isEmpty(), "Transaction not readied yet");
141     }
142
143     @Override
144     public ListenableFuture<Boolean> validate() {
145         LOG.debug("Validating transaction for shard {}", shardRoot);
146
147         checkTransactionReadied();
148         final List<ListenableFuture<Boolean>> futures =
149                 cohorts.stream().map(DOMStoreThreePhaseCommitCohort::canCommit).collect(Collectors.toList());
150         final SettableFuture<Boolean> ret = SettableFuture.create();
151
152         Futures.addCallback(Futures.allAsList(futures), new FutureCallback<List<Boolean>>() {
153             @Override
154             public void onSuccess(final List<Boolean> result) {
155                 ret.set(true);
156             }
157
158             @Override
159             public void onFailure(final Throwable throwable) {
160                 ret.setException(throwable);
161             }
162         }, MoreExecutors.directExecutor());
163
164         return ret;
165     }
166
167     @Override
168     public ListenableFuture<Void> prepare() {
169         LOG.debug("Preparing transaction for shard {}", shardRoot);
170
171         checkTransactionReadied();
172         final List<ListenableFuture<Void>> futures =
173                 cohorts.stream().map(DOMStoreThreePhaseCommitCohort::preCommit).collect(Collectors.toList());
174         final SettableFuture<Void> ret = SettableFuture.create();
175
176         Futures.addCallback(Futures.allAsList(futures), new FutureCallback<List<Void>>() {
177             @Override
178             public void onSuccess(final List<Void> result) {
179                 ret.set(null);
180             }
181
182             @Override
183             public void onFailure(final Throwable throwable) {
184                 ret.setException(throwable);
185             }
186         }, MoreExecutors.directExecutor());
187
188         return ret;
189     }
190
191     @Override
192     public ListenableFuture<Void> commit() {
193         LOG.debug("Committing transaction for shard {}", shardRoot);
194
195         checkTransactionReadied();
196         final List<ListenableFuture<Void>> futures =
197                 cohorts.stream().map(DOMStoreThreePhaseCommitCohort::commit).collect(Collectors.toList());
198         final SettableFuture<Void> ret = SettableFuture.create();
199
200         Futures.addCallback(Futures.allAsList(futures), new FutureCallback<List<Void>>() {
201             @Override
202             public void onSuccess(final List<Void> result) {
203                 ret.set(null);
204             }
205
206             @Override
207             public void onFailure(final Throwable throwable) {
208                 ret.setException(throwable);
209             }
210         }, MoreExecutors.directExecutor());
211
212         return ret;
213     }
214 }

©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.