Merge (Abstract)TransactionContext
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionContext.java
1 /*
2  * Copyright (c) 2015 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.datastore;
9
10 import akka.actor.ActorSelection;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.Optional;
13 import java.util.SortedSet;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
16 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.Future;
22
23 abstract class TransactionContext extends AbstractSimpleIdentifiable<TransactionIdentifier> {
24     private static final Logger LOG = LoggerFactory.getLogger(TransactionContext.class);
25
26     private final short transactionVersion;
27
28     private long modificationCount = 0;
29     private boolean handOffComplete;
30
31     TransactionContext(final TransactionIdentifier transactionIdentifier) {
32         this(transactionIdentifier, DataStoreVersions.CURRENT_VERSION);
33     }
34
35     TransactionContext(final TransactionIdentifier transactionIdentifier, final short transactionVersion) {
36         super(transactionIdentifier);
37         this.transactionVersion = transactionVersion;
38     }
39
40     final short getTransactionVersion() {
41         return transactionVersion;
42     }
43
44     final void incrementModificationCount() {
45         modificationCount++;
46     }
47
48     final void logModificationCount() {
49         LOG.debug("Total modifications on Tx {} = [ {} ]", getIdentifier(), modificationCount);
50     }
51
52     /**
53      * Invoked by {@link AbstractTransactionContextWrapper} when it has finished handing
54      * off operations to this context. From this point on, the context is responsible
55      * for throttling operations.
56      *
57      * <p>
58      * Implementations can rely on the wrapper calling this operation in a synchronized
59      * block, so they do not need to ensure visibility of this state transition themselves.
60      */
61     final void operationHandOffComplete() {
62         handOffComplete = true;
63     }
64
65     final boolean isOperationHandOffComplete() {
66         return handOffComplete;
67     }
68
69     /**
70      * A TransactionContext that uses operation limiting should return true else false.
71      *
72      * @return true if operation limiting is used, false otherwise
73      */
74     boolean usesOperationLimiting() {
75         return false;
76     }
77
78     abstract void executeDelete(YangInstanceIdentifier path, Boolean havePermit);
79
80     abstract void executeMerge(YangInstanceIdentifier path, NormalizedNode data, Boolean havePermit);
81
82     abstract void executeWrite(YangInstanceIdentifier path, NormalizedNode data, Boolean havePermit);
83
84     abstract <T> void executeRead(AbstractRead<T> readCmd, SettableFuture<T> proxyFuture, Boolean havePermit);
85
86     abstract Future<ActorSelection> readyTransaction(Boolean havePermit,
87             Optional<SortedSet<String>> participatingShardNames);
88
89     abstract Future<Object> directCommit(Boolean havePermit);
90
91     abstract void closeTransaction();
92 }