Fix remaining CS warnings in sal-distributed-datastore
[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  * <p>
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
35  * leader.
36  *
37  * <p>
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.
40  *
41  * @author Robert Varga
42  */
43 @NotThreadSafe
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);
48     };
49
50     private final TransactionIdentifier identifier;
51     private DataTreeModification modification;
52
53     LocalProxyTransaction(final DistributedDataStoreClientBehavior client,
54         final TransactionIdentifier identifier, final DataTreeSnapshot snapshot) {
55         super(client);
56         this.identifier = Preconditions.checkNotNull(identifier);
57         this.modification = snapshot.newModification();
58     }
59
60     @Override
61     public TransactionIdentifier getIdentifier() {
62         return identifier;
63     }
64
65     @Override
66     void doDelete(final YangInstanceIdentifier path) {
67         modification.delete(path);
68     }
69
70     @Override
71     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
72         modification.merge(path, data);
73     }
74
75     @Override
76     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
77         modification.write(path, data);
78     }
79
80     @Override
81     CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
82         return Futures.immediateCheckedFuture(modification.readNode(path).isPresent());
83     }
84
85     @Override
86     CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
87         return Futures.immediateCheckedFuture(modification.readNode(path));
88     }
89
90     @Override
91     void doAbort() {
92         sendRequest(new AbortLocalTransactionRequest(identifier, localActor()), ABORT_COMPLETER);
93         modification = new FailedDataTreeModification(() -> new IllegalStateException("Tracker has been aborted"));
94     }
95
96     @Override
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"));
101         return ret;
102     }
103
104     @Override
105     void doSeal() {
106         modification.ready();
107     }
108 }