Add UnsignedLongBitmap
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorSelection;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import java.util.function.Consumer;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import scala.concurrent.Future;
29
30 /**
31  * Processes front-end transaction operations locally before being committed to the destination shard.
32  * Instances of this class are used when the destination shard is local to the caller.
33  *
34  * @author Thomas Pantelis
35  */
36 abstract class LocalTransactionContext extends TransactionContext {
37     private final DOMStoreTransaction txDelegate;
38     private final LocalTransactionReadySupport readySupport;
39     private Exception operationError;
40
41     LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
42             final LocalTransactionReadySupport readySupport) {
43         super(identifier);
44         this.txDelegate = requireNonNull(txDelegate);
45         this.readySupport = readySupport;
46     }
47
48     abstract DOMStoreWriteTransaction getWriteDelegate();
49
50     abstract DOMStoreReadTransaction getReadDelegate();
51
52     @SuppressWarnings("checkstyle:IllegalCatch")
53     private void executeModification(final Consumer<DOMStoreWriteTransaction> consumer) {
54         incrementModificationCount();
55         if (operationError == null) {
56             try {
57                 consumer.accept(getWriteDelegate());
58             } catch (Exception e) {
59                 operationError = e;
60             }
61         }
62     }
63
64     @Override
65     void executeDelete(final YangInstanceIdentifier path, final Boolean havePermit) {
66         executeModification(transaction -> transaction.delete(path));
67     }
68
69     @Override
70     void executeMerge(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
71         executeModification(transaction -> transaction.merge(path, data));
72     }
73
74     @Override
75     void executeWrite(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
76         executeModification(transaction -> transaction.write(path, data));
77     }
78
79     @Override
80     <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
81             final Boolean havePermit) {
82         Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
83             @Override
84             public void onSuccess(final T result) {
85                 proxyFuture.set(result);
86             }
87
88             @Override
89             public void onFailure(final Throwable failure) {
90                 proxyFuture.setException(failure instanceof Exception
91                         ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
92             }
93         }, MoreExecutors.directExecutor());
94     }
95
96     @Override
97     Future<ActorSelection> readyTransaction(final Boolean havePermit,
98             final Optional<SortedSet<String>> participatingShardNames) {
99         final LocalThreePhaseCommitCohort cohort = ready();
100         return cohort.initiateCoordinatedCommit(participatingShardNames);
101     }
102
103     @Override
104     Future<Object> directCommit(final Boolean havePermit) {
105         final LocalThreePhaseCommitCohort cohort = ready();
106         return cohort.initiateDirectCommit();
107     }
108
109     @Override
110     void closeTransaction() {
111         txDelegate.close();
112     }
113
114     private LocalThreePhaseCommitCohort ready() {
115         logModificationCount();
116         return readySupport.onTransactionReady(getWriteDelegate(), operationError);
117     }
118 }