2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.broker.impl;
10 import static com.google.common.base.Preconditions.checkState;
12 import javax.annotation.concurrent.GuardedBy;
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;
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;
31 * Read-Write Transaction, which is composed of several
32 * {@link DOMStoreWriteTransaction} transactions. Subtransaction is selected by
33 * {@link LogicalDatastoreType} type parameter in:
36 * <li>{@link #put(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
37 * <li>{@link #delete(LogicalDatastoreType, InstanceIdentifier)}
38 * <li>{@link #merge(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
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
47 * Subtype of {@link DOMStoreWriteTransaction} which is used as
50 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
51 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
54 * Implementation of real commit.
56 * Transaction can not be commited if commitImpl is null,
57 * so this seting this property to null is also used to
59 * already commited / canceled transaction {@link #checkNotCanceled()
64 private volatile DOMDataCommitImplementation commitImpl;
68 * Future task of transaction commit.
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()}.
77 private volatile ListenableFuture<RpcResult<TransactionStatus>> commitFuture;
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.");
86 public void put(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
88 getSubtransaction(store).write(path, data);
92 public void delete(final LogicalDatastoreType store, final InstanceIdentifier path) {
94 getSubtransaction(store).delete(path);
98 public void merge(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
100 getSubtransaction(store).merge(path, data);
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) {
113 cancelationResult = true;
116 return cancelationResult;
121 public synchronized ListenableFuture<RpcResult<TransactionStatus>> commit() {
124 ImmutableList.Builder<DOMStoreThreePhaseCommitCohort> cohortsBuilder = ImmutableList.builder();
125 for (DOMStoreWriteTransaction subTx : getSubtransactions()) {
126 cohortsBuilder.add(subTx.ready());
128 ImmutableList<DOMStoreThreePhaseCommitCohort> cohorts = cohortsBuilder.build();
129 commitFuture = commitImpl.commit(this, cohorts);
132 *We remove reference to Commit Implementation in order
133 *to prevent memory leak
139 private void checkNotReady() {
144 private void checkNotCanceled() {
145 Preconditions.checkState(commitImpl != null, "Transaction was canceled.");
148 private void checkNotCommited() {
149 checkState(commitFuture == null, "Transaction was already submited.");