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
34 public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransaction<T>
35 implements DOMStoreWriteTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
37 @SuppressWarnings("rawtypes")
38 private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, TransactionReadyPrototype>
39 READY_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
40 TransactionReadyPrototype.class, "readyImpl");
41 @SuppressWarnings("rawtypes")
42 private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, DataTreeModification>
43 TREE_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
44 DataTreeModification.class, "mutableTree");
46 // non-null when not ready
47 private volatile TransactionReadyPrototype<T> readyImpl;
48 // non-null when not committed/closed
49 private volatile DataTreeModification mutableTree;
51 SnapshotBackedWriteTransaction(final T identifier, final boolean debug,
52 final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
53 super(identifier, debug);
54 this.readyImpl = Preconditions.checkNotNull(readyImpl, "readyImpl must not be null.");
55 mutableTree = snapshot.newModification();
56 LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot);
60 @SuppressWarnings("checkstyle:IllegalCatch")
61 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
64 final DataTreeModification tree = mutableTree;
65 LOG.debug("Tx: {} Write: {}:{}", getIdentifier(), path, data);
68 tree.write(path, data);
69 // FIXME: Add checked exception
70 } catch (RuntimeException e) {
71 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
72 // Rethrow original ones if they are subclasses of RuntimeException
74 Throwables.propagateIfPossible(e);
75 // FIXME: Introduce proper checked exception
76 throw new IllegalArgumentException("Illegal input data.", e);
81 @SuppressWarnings("checkstyle:IllegalCatch")
82 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
85 final DataTreeModification tree = mutableTree;
86 LOG.debug("Tx: {} Merge: {}:{}", getIdentifier(), path, data);
89 tree.merge(path, data);
90 // FIXME: Add checked exception
91 } catch (RuntimeException e) {
92 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
93 // Rethrow original ones if they are subclasses of RuntimeException
95 Throwables.propagateIfPossible(e);
96 // FIXME: Introduce proper checked exception
97 throw new IllegalArgumentException("Illegal input data.", e);
102 @SuppressWarnings("checkstyle:IllegalCatch")
103 public void delete(final YangInstanceIdentifier path) {
106 final DataTreeModification tree = mutableTree;
107 LOG.debug("Tx: {} Delete: {}", getIdentifier(), path);
111 // FIXME: Add checked exception
112 } catch (RuntimeException e) {
113 LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, tree, e);
114 // Rethrow original ones if they are subclasses of RuntimeException
116 Throwables.propagateIfPossible(e);
117 // FIXME: Introduce proper checked exception
118 throw new IllegalArgumentException("Illegal path to delete.", e);
123 * Exposed for {@link SnapshotBackedReadWriteTransaction}'s sake only. The contract does
124 * not allow data access after the transaction has been closed or readied.
126 * @param path Path to read
127 * @return null if the the transaction has been closed;
129 final Optional<NormalizedNode<?, ?>> readSnapshotNode(final YangInstanceIdentifier path) {
130 return readyImpl == null ? null : Optional.fromJavaUtil(mutableTree.readNode(path));
133 private void checkNotReady() {
134 checkState(readyImpl != null, "Transaction %s is no longer open. No further modifications allowed.",
139 @SuppressWarnings("checkstyle:IllegalCatch")
140 public DOMStoreThreePhaseCommitCohort ready() {
141 @SuppressWarnings("unchecked")
142 final TransactionReadyPrototype<T> wasReady = READY_UPDATER.getAndSet(this, null);
143 checkState(wasReady != null, "Transaction %s is no longer open", getIdentifier());
145 LOG.debug("Store transaction: {} : Ready", getIdentifier());
147 final DataTreeModification tree = mutableTree;
148 TREE_UPDATER.lazySet(this, null);
151 return wasReady.transactionReady(this, tree, null);
152 } catch (RuntimeException e) {
153 LOG.debug("Store transaction: {}: unexpected failure when readying", getIdentifier(), e);
154 return wasReady.transactionReady(this, tree, e);
159 public void close() {
160 @SuppressWarnings("unchecked")
161 final TransactionReadyPrototype<T> wasReady = READY_UPDATER.getAndSet(this, null);
162 if (wasReady != null) {
163 LOG.debug("Store transaction: {} : Closed", getIdentifier());
164 TREE_UPDATER.lazySet(this, null);
165 wasReady.transactionAborted(this);
167 LOG.debug("Store transaction: {} : Closed after submit", getIdentifier());
172 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
173 return toStringHelper.add("ready", readyImpl == null);
177 * This class is intended to be implemented by Transaction factories responsible for allocation of
178 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction} and
179 * providing underlying logic for applying implementation.
181 * @param <T> identifier type
183 public abstract static class TransactionReadyPrototype<T> {
185 * Called when a transaction is closed without being readied. This is not invoked for
186 * transactions which are ready.
188 * @param tx Transaction which got aborted.
190 protected abstract void transactionAborted(SnapshotBackedWriteTransaction<T> tx);
193 * Returns a commit coordinator associated with supplied transactions.
196 * This call must not fail.
199 * Transaction on which ready was invoked.
201 * Modified data tree which has been constructed.
203 * Any error that has already happened when readying.
204 * @return DOMStoreThreePhaseCommitCohort associated with transaction
206 protected abstract DOMStoreThreePhaseCommitCohort transactionReady(SnapshotBackedWriteTransaction<T> tx,
207 DataTreeModification tree,
208 @Nullable Exception readyError);