571899ba14343f7b8f189571e18f708b397fc86b
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractTransactionContext.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 com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 abstract class AbstractTransactionContext implements TransactionContext {
17     private static final Logger LOG = LoggerFactory.getLogger(AbstractTransactionContext.class);
18     private final OperationLimiter limiter;
19     private long modificationCount = 0;
20     private boolean handoffComplete;
21
22     protected AbstractTransactionContext(final OperationLimiter limiter) {
23         this.limiter = Preconditions.checkNotNull(limiter);
24     }
25
26     /**
27      * Get the transaction identifier associated with this context.
28      *
29      * @return Transaction identifier.
30      */
31     @Nonnull protected final TransactionIdentifier getIdentifier() {
32         return limiter.getIdentifier();
33     }
34
35     /**
36      * Return the operation limiter associated with this context.
37      * @return Operation limiter.
38      */
39     @Nonnull protected final OperationLimiter getLimiter() {
40         return limiter;
41     }
42
43     /**
44      * Indicate whether all operations have been handed off by the {@link TransactionContextWrapper}.
45      *
46      * @return True if this context is responsible for throttling.
47      */
48     protected final boolean isOperationHandoffComplete() {
49         return handoffComplete;
50     }
51
52     /**
53      * Acquire operation from the limiter if the handoff has completed. If
54      * the handoff is still ongoing, this method does nothing.
55      */
56     protected final void acquireOperation() {
57         if (handoffComplete) {
58             limiter.acquire();
59         }
60     }
61
62     /**
63      * Acquire operation from the limiter if the handoff has NOT completed. If
64      * the handoff has completed, this method does nothing.
65      */
66     protected final void releaseOperation() {
67         if (!handoffComplete) {
68             limiter.release();
69         }
70     }
71
72     protected final void incrementModificationCount() {
73         modificationCount++;
74     }
75
76     protected final void logModificationCount() {
77         LOG.debug("Total modifications on Tx {} = [ {} ]", getIdentifier(), modificationCount);
78     }
79
80     @Override
81     public final void operationHandoffComplete() {
82         handoffComplete = true;
83     }
84 }