BUG-5280: implement transaction dispatch
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / LocalProxyTransaction.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 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
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;
27
28 /**
29  * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
30  * the client instance.
31  *
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
34  * leader.
35  *
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.
38  *
39  * @author Robert Varga
40  */
41 @NotThreadSafe
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);
46     };
47
48     private final TransactionIdentifier identifier;
49     private DataTreeModification modification;
50
51     LocalProxyTransaction(final DistributedDataStoreClientBehavior client,
52         final TransactionIdentifier identifier, final DataTreeSnapshot snapshot) {
53         super(client);
54         this.identifier = Preconditions.checkNotNull(identifier);
55         this.modification = snapshot.newModification();
56     }
57
58     @Override
59     public TransactionIdentifier getIdentifier() {
60         return identifier;
61     }
62
63     @Override
64     void doDelete(final YangInstanceIdentifier path) {
65         modification.delete(path);
66     }
67
68     @Override
69     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
70         modification.merge(path, data);
71     }
72
73     @Override
74     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
75         modification.write(path, data);
76     }
77
78     @Override
79     CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
80         return Futures.immediateCheckedFuture(modification.readNode(path).isPresent());
81     }
82
83     @Override
84     CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
85         return Futures.immediateCheckedFuture(modification.readNode(path));
86     }
87
88     @Override
89     void doAbort() {
90         client().sendRequest(nextSequence(), new AbortLocalTransactionRequest(identifier, client().self()), ABORT_COMPLETER);
91         modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been aborted"));
92     }
93
94     @Override
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"));
99         return ret;
100     }
101
102     @Override
103     void doSeal() {
104         modification.ready();
105     }
106 }