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 akka.actor.ActorSelection;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import scala.concurrent.Future;
25 * Processes front-end transaction operations locally before being committed to the destination shard.
26 * Instances of this class are used when the destination shard is local to the caller.
28 * @author Thomas Pantelis
30 abstract class LocalTransactionContext extends AbstractTransactionContext {
32 private final DOMStoreTransaction txDelegate;
33 private final OperationCompleter completer;
35 LocalTransactionContext(TransactionIdentifier identifier, DOMStoreTransaction txDelegate, OperationCompleter completer) {
37 this.txDelegate = Preconditions.checkNotNull(txDelegate);
38 this.completer = Preconditions.checkNotNull(completer);
41 protected abstract DOMStoreWriteTransaction getWriteDelegate();
43 protected abstract DOMStoreReadTransaction getReadDelegate();
46 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
47 getWriteDelegate().write(path, data);
48 completer.onComplete(null, null);
52 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
53 getWriteDelegate().merge(path, data);
54 completer.onComplete(null, null);
58 public void deleteData(YangInstanceIdentifier path) {
59 getWriteDelegate().delete(path);
60 completer.onComplete(null, null);
64 public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
65 Futures.addCallback(getReadDelegate().read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
67 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
68 proxyFuture.set(result);
69 completer.onComplete(null, null);
73 public void onFailure(Throwable t) {
74 proxyFuture.setException(t);
75 completer.onComplete(null, null);
81 public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
82 Futures.addCallback(getReadDelegate().exists(path), new FutureCallback<Boolean>() {
84 public void onSuccess(Boolean result) {
85 proxyFuture.set(result);
86 completer.onComplete(null, null);
90 public void onFailure(Throwable t) {
91 proxyFuture.setException(t);
92 completer.onComplete(null, null);
97 private LocalThreePhaseCommitCohort ready() {
98 LocalThreePhaseCommitCohort ready = (LocalThreePhaseCommitCohort) getWriteDelegate().ready();
99 completer.onComplete(null, null);
104 public Future<ActorSelection> readyTransaction() {
105 return ready().initiateCoordinatedCommit();
109 public Future<Object> directCommit() {
110 return ready().initiateDirectCommit();
114 public boolean supportsDirectCommit() {
119 public void closeTransaction() {