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
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.function.Consumer;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
17 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.concepts.Response;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
30 * the client instance.
32 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
33 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
36 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
37 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
39 * @author Robert Varga
42 final class LocalProxyTransaction extends AbstractProxyTransaction {
43 private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
44 private static final Consumer<Response<?, ?>> ABORT_COMPLETER = response -> {
45 LOG.debug("Abort completed with {}", response);
48 private final TransactionIdentifier identifier;
49 private DataTreeModification modification;
51 LocalProxyTransaction(final DistributedDataStoreClientBehavior client,
52 final TransactionIdentifier identifier, final DataTreeSnapshot snapshot) {
54 this.identifier = Preconditions.checkNotNull(identifier);
55 this.modification = snapshot.newModification();
59 public TransactionIdentifier getIdentifier() {
64 void doDelete(final YangInstanceIdentifier path) {
65 modification.delete(path);
69 void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
70 modification.merge(path, data);
74 void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
75 modification.write(path, data);
79 CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
80 return Futures.immediateCheckedFuture(modification.readNode(path).isPresent());
84 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
85 return Futures.immediateCheckedFuture(modification.readNode(path));
90 client().sendRequest(new AbortLocalTransactionRequest(identifier, client().self()), ABORT_COMPLETER);
91 modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been aborted"));
95 CommitLocalTransactionRequest doCommit(final boolean coordinated) {
96 final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(identifier, client().self(),
97 modification, coordinated);
98 modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been submitted"));
104 modification.ready();