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.store.impl;
10 import static com.google.common.base.Preconditions.checkState;
12 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeSnapshot;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.common.base.Objects.ToStringHelper;
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Throwables;
26 * Implementation of Write transaction which is backed by
27 * {@link DataTreeSnapshot} and executed according to
28 * {@link TransactionReadyPrototype}.
31 class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction {
33 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
34 private DataTreeModification mutableTree;
35 private boolean ready = false;
36 private TransactionReadyPrototype readyImpl;
39 * Creates new write-only transaction.
42 * transaction Identifier
44 * Snapshot which will be modified.
46 * Implementation of ready method.
48 public SnapshotBackedWriteTransaction(final Object identifier, final DataTreeSnapshot snapshot,
49 final TransactionReadyPrototype readyImpl) {
51 mutableTree = snapshot.newModification();
52 this.readyImpl = Preconditions.checkNotNull(readyImpl, "readyImpl must not be null.");
53 LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot);
58 LOG.debug("Store transaction: {} : Closed", getIdentifier());
59 this.mutableTree = null;
60 this.readyImpl = null;
64 public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
67 LOG.debug("Tx: {} Write: {}:{}", getIdentifier(), path, data);
68 mutableTree.write(path, data);
69 // FIXME: Add checked exception
70 } catch (Exception e) {
71 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, 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 public void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
84 LOG.debug("Tx: {} Merge: {}:{}", getIdentifier(), path, data);
85 mutableTree.merge(path, data);
86 // FIXME: Add checked exception
87 } catch (Exception e) {
88 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, e);
89 // Rethrow original ones if they are subclasses of RuntimeException
91 Throwables.propagateIfPossible(e);
92 // FIXME: Introduce proper checked exception
93 throw new IllegalArgumentException("Illegal input data.", e);
98 public void delete(final InstanceIdentifier path) {
101 LOG.debug("Tx: {} Delete: {}", getIdentifier(), path);
102 mutableTree.delete(path);
103 // FIXME: Add checked exception
104 } catch (Exception e) {
105 LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, mutableTree, e);
106 // Rethrow original ones if they are subclasses of RuntimeException
108 Throwables.propagateIfPossible(e);
109 // FIXME: Introduce proper checked exception
110 throw new IllegalArgumentException("Illegal path to delete.", e);
114 protected final boolean isReady() {
118 protected final void checkNotReady() {
119 checkState(!ready, "Transaction %s is ready. No further modifications allowed.", getIdentifier());
123 public synchronized DOMStoreThreePhaseCommitCohort ready() {
124 checkState(!ready, "Transaction %s is already ready.", getIdentifier());
126 LOG.debug("Store transaction: {} : Ready", getIdentifier());
128 return readyImpl.ready(this);
131 protected DataTreeModification getMutatedView() {
136 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
137 return toStringHelper.add("ready", isReady());
141 * Prototype implementation of
142 * {@link #ready(SnapshotBackedWriteTransaction)}
144 * This class is intended to be implemented by Transaction factories
145 * responsible for allocation of {@link SnapshotBackedWriteTransaction} and
146 * providing underlying logic for applying implementation.
149 public static interface TransactionReadyPrototype {
152 * Returns a commit coordinator associated with supplied transactions.
154 * This call must not fail.
157 * Transaction on which ready was invoked.
158 * @return DOMStoreThreePhaseCommitCohort associated with transaction
160 DOMStoreThreePhaseCommitCohort ready(SnapshotBackedWriteTransaction tx);