Merge "Introducing the Modification classses"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMForwardedWriteTransaction.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.broker.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import javax.annotation.concurrent.GuardedBy;
13
14 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.util.concurrent.ListenableFuture;
27
28 /**
29  *
30  *
31  * Read-Write Transaction, which is composed of several
32  * {@link DOMStoreWriteTransaction} transactions. Subtransaction is selected by
33  * {@link LogicalDatastoreType} type parameter in:
34  *
35  * <ul>
36  * <li>{@link #put(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
37  * <li>{@link #delete(LogicalDatastoreType, InstanceIdentifier)}
38  * <li>{@link #merge(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
39  * </ul>
40  * <p>
41  * {@link #commit()} will result in invocation of
42  * {@link DOMDataCommitImplementation#commit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction, Iterable)}
43  * invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort} for underlying
44  * transactions.
45  *
46  * @param <T>
47  *            Subtype of {@link DOMStoreWriteTransaction} which is used as
48  *            subtransaction.
49  */
50 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
51         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
52
53     /**
54      *  Implementation of real commit.
55      *
56      *  Transaction can not be commited if commitImpl is null,
57      *  so this seting this property to null is also used to
58      *  prevent write to
59      *  already commited / canceled transaction {@link #checkNotCanceled()
60      *
61      *
62      */
63     @GuardedBy("this")
64     private volatile DOMDataCommitImplementation commitImpl;
65
66     /**
67      *
68      * Future task of transaction commit.
69      *
70      * This value is initially null, and is once updated if transaction
71      * is commited {@link #commit()}.
72      * If this future exists, transaction MUST not be commited again
73      * and all modifications should fail. See {@link #checkNotCommited()}.
74      *
75      */
76     @GuardedBy("this")
77     private volatile ListenableFuture<RpcResult<TransactionStatus>> commitFuture;
78
79     protected DOMForwardedWriteTransaction(final Object identifier,
80             final ImmutableMap<LogicalDatastoreType, T> backingTxs, final DOMDataCommitImplementation commitImpl) {
81         super(identifier, backingTxs);
82         this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
83     }
84
85     @Override
86     public void put(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
87         checkNotReady();
88         getSubtransaction(store).write(path, data);
89     }
90
91     @Override
92     public void delete(final LogicalDatastoreType store, final InstanceIdentifier path) {
93         checkNotReady();
94         getSubtransaction(store).delete(path);
95     }
96
97     @Override
98     public void merge(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
99         checkNotReady();
100         getSubtransaction(store).merge(path, data);
101     }
102
103     @Override
104     public synchronized boolean cancel() {
105         // Transaction is already canceled, we are safe to return true
106         final boolean cancelationResult;
107         if (commitImpl == null && commitFuture != null) {
108             // Transaction is submitted, we try to cancel future.
109             cancelationResult = commitFuture.cancel(false);
110         } else if(commitImpl == null) {
111             return true;
112         } else {
113             cancelationResult = true;
114             commitImpl = null;
115         }
116         return cancelationResult;
117
118     }
119
120     @Override
121     public synchronized ListenableFuture<RpcResult<TransactionStatus>> commit() {
122         checkNotReady();
123
124         ImmutableList.Builder<DOMStoreThreePhaseCommitCohort> cohortsBuilder = ImmutableList.builder();
125         for (DOMStoreWriteTransaction subTx : getSubtransactions()) {
126             cohortsBuilder.add(subTx.ready());
127         }
128         ImmutableList<DOMStoreThreePhaseCommitCohort> cohorts = cohortsBuilder.build();
129         commitFuture = commitImpl.commit(this, cohorts);
130
131         /*
132          *We remove reference to Commit Implementation in order
133          *to prevent memory leak
134          */
135         commitImpl = null;
136         return commitFuture;
137     }
138
139     private void checkNotReady() {
140         checkNotCommited();
141         checkNotCanceled();
142     }
143
144     private void checkNotCanceled() {
145         Preconditions.checkState(commitImpl != null, "Transaction was canceled.");
146     }
147
148     private void checkNotCommited() {
149         checkState(commitFuture == null, "Transaction was already submited.");
150     }
151
152 }