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.
33 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
34 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
38 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
39 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
41 * @author Robert Varga
44 final class LocalProxyTransaction extends AbstractProxyTransaction {
45 private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
46 private static final Consumer<Response<?, ?>> ABORT_COMPLETER = response -> {
47 LOG.debug("Abort completed with {}", response);
50 private final TransactionIdentifier identifier;
51 private DataTreeModification modification;
53 LocalProxyTransaction(final DistributedDataStoreClientBehavior client,
54 final TransactionIdentifier identifier, final DataTreeSnapshot snapshot) {
56 this.identifier = Preconditions.checkNotNull(identifier);
57 this.modification = snapshot.newModification();
61 public TransactionIdentifier getIdentifier() {
66 void doDelete(final YangInstanceIdentifier path) {
67 modification.delete(path);
71 void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
72 modification.merge(path, data);
76 void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
77 modification.write(path, data);
81 CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
82 return Futures.immediateCheckedFuture(modification.readNode(path).isPresent());
86 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
87 return Futures.immediateCheckedFuture(modification.readNode(path));
92 sendRequest(new AbortLocalTransactionRequest(identifier, localActor()), ABORT_COMPLETER);
93 modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been aborted"));
97 CommitLocalTransactionRequest doCommit(final boolean coordinated) {
98 final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(identifier, localActor(),
99 modification, coordinated);
100 modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been submitted"));
106 modification.ready();