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.sal.core.spi.data;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Throwables;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Implementation of Write transaction which is backed by
28 * {@link DataTreeSnapshot} and executed according to
29 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype}.
31 * @param <T> Identifier type
32 * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction} instead.
36 public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransaction<T>
37 implements DOMStoreWriteTransaction {
38 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
39 @SuppressWarnings("rawtypes")
40 private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, TransactionReadyPrototype>
41 READY_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
42 TransactionReadyPrototype.class, "readyImpl");
43 @SuppressWarnings("rawtypes")
44 private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, DataTreeModification>
45 TREE_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
46 DataTreeModification.class, "mutableTree");
48 // non-null when not ready
49 private volatile TransactionReadyPrototype<T> readyImpl;
50 // non-null when not committed/closed
51 private volatile DataTreeModification mutableTree;
53 SnapshotBackedWriteTransaction(final T identifier, final boolean debug,
54 final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
55 super(identifier, debug);
56 this.readyImpl = Preconditions.checkNotNull(readyImpl, "readyImpl must not be null.");
57 mutableTree = snapshot.newModification();
58 LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot);
62 @SuppressWarnings("checkstyle:IllegalCatch")
63 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
66 final DataTreeModification tree = mutableTree;
67 LOG.debug("Tx: {} Write: {}:{}", getIdentifier(), path, data);
70 tree.write(path, data);
71 // FIXME: Add checked exception
72 } catch (RuntimeException e) {
73 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
74 // Rethrow original ones if they are subclasses of RuntimeException
76 Throwables.propagateIfPossible(e);
77 // FIXME: Introduce proper checked exception
78 throw new IllegalArgumentException("Illegal input data.", e);
83 @SuppressWarnings("checkstyle:IllegalCatch")
84 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
87 final DataTreeModification tree = mutableTree;
88 LOG.debug("Tx: {} Merge: {}:{}", getIdentifier(), path, data);
91 tree.merge(path, data);
92 // FIXME: Add checked exception
93 } catch (RuntimeException e) {
94 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
95 // Rethrow original ones if they are subclasses of RuntimeException
97 Throwables.propagateIfPossible(e);
98 // FIXME: Introduce proper checked exception
99 throw new IllegalArgumentException("Illegal input data.", e);
104 @SuppressWarnings("checkstyle:IllegalCatch")
105 public void delete(final YangInstanceIdentifier path) {
108 final DataTreeModification tree = mutableTree;
109 LOG.debug("Tx: {} Delete: {}", getIdentifier(), path);
113 // FIXME: Add checked exception
114 } catch (RuntimeException e) {
115 LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, tree, e);
116 // Rethrow original ones if they are subclasses of RuntimeException
118 Throwables.propagateIfPossible(e);
119 // FIXME: Introduce proper checked exception
120 throw new IllegalArgumentException("Illegal path to delete.", e);
125 * Exposed for {@link SnapshotBackedReadWriteTransaction}'s sake only. The contract does
126 * not allow data access after the transaction has been closed or readied.
128 * @param path Path to read
129 * @return null if the the transaction has been closed;
131 final Optional<NormalizedNode<?, ?>> readSnapshotNode(final YangInstanceIdentifier path) {
132 return readyImpl == null ? null : Optional.fromJavaUtil(mutableTree.readNode(path));
135 private void checkNotReady() {
136 checkState(readyImpl != null, "Transaction %s is no longer open. No further modifications allowed.",
141 @SuppressWarnings("checkstyle:IllegalCatch")
142 public DOMStoreThreePhaseCommitCohort ready() {
143 @SuppressWarnings("unchecked")
144 final TransactionReadyPrototype<T> wasReady = READY_UPDATER.getAndSet(this, null);
145 checkState(wasReady != null, "Transaction %s is no longer open", getIdentifier());
147 LOG.debug("Store transaction: {} : Ready", getIdentifier());
149 final DataTreeModification tree = mutableTree;
150 TREE_UPDATER.lazySet(this, null);
153 return wasReady.transactionReady(this, tree, null);
154 } catch (RuntimeException e) {
155 LOG.debug("Store transaction: {}: unexpected failure when readying", getIdentifier(), e);
156 return wasReady.transactionReady(this, tree, e);
161 public void close() {
162 @SuppressWarnings("unchecked")
163 final TransactionReadyPrototype<T> wasReady = READY_UPDATER.getAndSet(this, null);
164 if (wasReady != null) {
165 LOG.debug("Store transaction: {} : Closed", getIdentifier());
166 TREE_UPDATER.lazySet(this, null);
167 wasReady.transactionAborted(this);
169 LOG.debug("Store transaction: {} : Closed after submit", getIdentifier());
174 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
175 return toStringHelper.add("ready", readyImpl == null);
179 * This class is intended to be implemented by Transaction factories responsible for allocation of
180 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction} and
181 * providing underlying logic for applying implementation.
183 * @param <T> identifier type
186 public abstract static class TransactionReadyPrototype<T> {
188 * Called when a transaction is closed without being readied. This is not invoked for
189 * transactions which are ready.
191 * @param tx Transaction which got aborted.
193 protected abstract void transactionAborted(SnapshotBackedWriteTransaction<T> tx);
196 * Returns a commit coordinator associated with supplied transactions.
199 * This call must not fail.
202 * Transaction on which ready was invoked.
204 * Modified data tree which has been constructed.
206 * Any error that has already happened when readying.
207 * @return DOMStoreThreePhaseCommitCohort associated with transaction
209 protected abstract DOMStoreThreePhaseCommitCohort transactionReady(SnapshotBackedWriteTransaction<T> tx,
210 DataTreeModification tree,
211 @Nullable Exception readyError);