2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorSelection;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import scala.concurrent.Future;
30 * Processes front-end transaction operations locally before being committed to the destination shard.
31 * Instances of this class are used when the destination shard is local to the caller.
33 * @author Thomas Pantelis
35 abstract class LocalTransactionContext extends AbstractTransactionContext {
36 private final DOMStoreTransaction txDelegate;
37 private final LocalTransactionReadySupport readySupport;
38 private Exception operationError;
40 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
41 final LocalTransactionReadySupport readySupport) {
43 this.txDelegate = requireNonNull(txDelegate);
44 this.readySupport = readySupport;
47 protected abstract DOMStoreWriteTransaction getWriteDelegate();
49 protected abstract DOMStoreReadTransaction getReadDelegate();
52 @SuppressWarnings("checkstyle:IllegalCatch")
53 public void executeDelete(final YangInstanceIdentifier path, final Boolean havePermit) {
54 incrementModificationCount();
55 if (operationError == null) {
57 getWriteDelegate().delete(path);
58 } catch (Exception e) {
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 public void executeMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data,
67 final Boolean havePermit) {
68 incrementModificationCount();
69 if (operationError == null) {
71 getWriteDelegate().merge(path, data);
72 } catch (Exception e) {
79 @SuppressWarnings("checkstyle:IllegalCatch")
80 public void executeWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data,
81 final Boolean havePermit) {
82 incrementModificationCount();
83 if (operationError == null) {
85 getWriteDelegate().write(path, data);
86 } catch (Exception e) {
93 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
94 final Boolean havePermit) {
95 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
97 public void onSuccess(final T result) {
98 proxyFuture.set(result);
102 public void onFailure(final Throwable failure) {
103 proxyFuture.setException(failure instanceof Exception
104 ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
106 }, MoreExecutors.directExecutor());
109 private LocalThreePhaseCommitCohort ready() {
110 logModificationCount();
111 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
115 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
116 final Optional<SortedSet<String>> participatingShardNames) {
117 final LocalThreePhaseCommitCohort cohort = ready();
118 return cohort.initiateCoordinatedCommit(participatingShardNames);
122 public Future<Object> directCommit(final Boolean havePermit) {
123 final LocalThreePhaseCommitCohort cohort = ready();
124 return cohort.initiateDirectCommit();
128 public void closeTransaction() {