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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.