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;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.common.base.Objects.ToStringHelper;
21 import com.google.common.base.Preconditions;
22 import com.google.common.base.Throwables;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 * Implementation of Write transaction which is backed by
28 * {@link DataTreeSnapshot} and executed according to
29 * {@link org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction.TransactionReadyPrototype}.
32 class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction {
34 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
35 private DataTreeModification mutableTree;
36 private boolean ready = false;
37 private TransactionReadyPrototype readyImpl;
40 * Creates new write-only transaction.
43 * transaction Identifier
45 * Snapshot which will be modified.
47 * Implementation of ready method.
49 public SnapshotBackedWriteTransaction(final Object identifier, final DataTreeSnapshot snapshot,
50 final TransactionReadyPrototype readyImpl) {
52 mutableTree = snapshot.newModification();
53 this.readyImpl = Preconditions.checkNotNull(readyImpl, "readyImpl must not be null.");
54 LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot);
59 LOG.debug("Store transaction: {} : Closed", getIdentifier());
60 this.mutableTree = null;
61 this.readyImpl = null;
65 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
68 LOG.debug("Tx: {} Write: {}:{}", getIdentifier(), path, data);
69 mutableTree.write(path, data);
70 // FIXME: Add checked exception
71 } catch (Exception e) {
72 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, e);
73 // Rethrow original ones if they are subclasses of RuntimeException
75 Throwables.propagateIfPossible(e);
76 // FIXME: Introduce proper checked exception
77 throw new IllegalArgumentException("Illegal input data.", e);
82 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
85 LOG.debug("Tx: {} Merge: {}:{}", getIdentifier(), path, data);
86 mutableTree.merge(path, data);
87 // FIXME: Add checked exception
88 } catch (Exception e) {
89 LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, e);
90 // Rethrow original ones if they are subclasses of RuntimeException
92 Throwables.propagateIfPossible(e);
93 // FIXME: Introduce proper checked exception
94 throw new IllegalArgumentException("Illegal input data.", e);
99 public void delete(final YangInstanceIdentifier path) {
102 LOG.debug("Tx: {} Delete: {}", getIdentifier(), path);
103 mutableTree.delete(path);
104 // FIXME: Add checked exception
105 } catch (Exception e) {
106 LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, mutableTree, e);
107 // Rethrow original ones if they are subclasses of RuntimeException
109 Throwables.propagateIfPossible(e);
110 // FIXME: Introduce proper checked exception
111 throw new IllegalArgumentException("Illegal path to delete.", e);
115 protected final boolean isReady() {
119 protected final void checkNotReady() {
120 checkState(!ready, "Transaction %s is ready. No further modifications allowed.", getIdentifier());
124 public synchronized DOMStoreThreePhaseCommitCohort ready() {
125 checkState(!ready, "Transaction %s is already ready.", getIdentifier());
127 LOG.debug("Store transaction: {} : Ready", getIdentifier());
129 return readyImpl.ready(this);
132 protected DataTreeModification getMutatedView() {
137 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
138 return toStringHelper.add("ready", isReady());
142 * Prototype implementation of
143 * {@link #ready(org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction)}
145 * This class is intended to be implemented by Transaction factories
146 * responsible for allocation of {@link org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction} and
147 * providing underlying logic for applying implementation.
150 public static interface TransactionReadyPrototype {
153 * Returns a commit coordinator associated with supplied transactions.
155 * This call must not fail.
158 * Transaction on which ready was invoked.
159 * @return DOMStoreThreePhaseCommitCohort associated with transaction
161 DOMStoreThreePhaseCommitCohort ready(SnapshotBackedWriteTransaction tx);