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.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
24 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
26 import org.opendaylight.controller.cluster.access.concepts.Response;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
29 import org.opendaylight.mdsal.common.api.ReadFailedException;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
40 * the client instance.
43 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
44 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
48 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
49 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
51 * @author Robert Varga
54 abstract class LocalProxyTransaction extends AbstractProxyTransaction {
55 private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
57 private final TransactionIdentifier identifier;
59 LocalProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier) {
61 this.identifier = Preconditions.checkNotNull(identifier);
65 public final TransactionIdentifier getIdentifier() {
69 abstract DataTreeSnapshot readOnlyView();
71 abstract void applyModifyTransactionRequest(ModifyTransactionRequest request,
72 @Nullable Consumer<Response<?, ?>> callback);
75 final CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
76 return Futures.immediateCheckedFuture(readOnlyView().readNode(path).isPresent());
80 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
81 return Futures.immediateCheckedFuture(readOnlyView().readNode(path));
85 final void doAbort() {
86 sendAbort(new AbortLocalTransactionRequest(identifier, localActor()), response -> {
87 LOG.debug("Transaction {} abort completed with {}", identifier, response);
92 void handleForwardedRemoteRequest(final TransactionRequest<?> request,
93 final @Nullable Consumer<Response<?, ?>> callback) {
94 if (request instanceof ModifyTransactionRequest) {
95 applyModifyTransactionRequest((ModifyTransactionRequest) request, callback);
96 } else if (request instanceof ReadTransactionRequest) {
97 final YangInstanceIdentifier path = ((ReadTransactionRequest) request).getPath();
98 final Optional<NormalizedNode<?, ?>> result = readOnlyView().readNode(path);
99 callback.accept(new ReadTransactionSuccess(request.getTarget(), request.getSequence(), result));
100 } else if (request instanceof ExistsTransactionRequest) {
101 final YangInstanceIdentifier path = ((ExistsTransactionRequest) request).getPath();
102 final boolean result = readOnlyView().readNode(path).isPresent();
103 callback.accept(new ExistsTransactionSuccess(request.getTarget(), request.getSequence(), result));
104 } else if (request instanceof TransactionPurgeRequest) {
107 throw new IllegalArgumentException("Unhandled request " + request);
112 final void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
113 final Consumer<Response<?, ?>> callback) {
114 if (request instanceof CommitLocalTransactionRequest) {
115 final CommitLocalTransactionRequest req = (CommitLocalTransactionRequest) request;
116 final DataTreeModification mod = req.getModification();
118 LOG.debug("Applying modification {} to successor {}", mod, successor);
119 mod.applyToCursor(new AbstractDataTreeModificationCursor() {
121 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
122 successor.write(current().node(child), data);
126 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
127 successor.merge(current().node(child), data);
131 public void delete(final PathArgument child) {
132 successor.delete(current().node(child));
136 successor.ensureSealed();
138 final ModifyTransactionRequest successorReq = successor.commitRequest(req.isCoordinated());
139 successor.sendRequest(successorReq, callback);
140 } else if (request instanceof AbortLocalTransactionRequest) {
141 LOG.debug("Forwarding abort {} to successor {}", request, successor);
143 } else if (request instanceof TransactionPurgeRequest) {
144 LOG.debug("Forwarding purge {} to successor {}", request, successor);
147 throw new IllegalArgumentException("Unhandled request" + request);
152 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
153 final Consumer<Response<?, ?>> callback) {
154 if (request instanceof AbortLocalTransactionRequest) {
155 successor.sendAbort(request, callback);
156 } else if (request instanceof TransactionPurgeRequest) {
159 throw new IllegalArgumentException("Unhandled request" + request);
162 LOG.debug("Forwarded request {} to successor {}", request, successor);
165 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
166 sendRequest(request, callback);