BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / SnapshotBackedWriteTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12
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;
19
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;
25
26 /**
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}.
30  *
31  */
32 class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction {
33
34     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
35     private DataTreeModification mutableTree;
36     private boolean ready = false;
37     private TransactionReadyPrototype readyImpl;
38
39     /**
40      * Creates new write-only transaction.
41      *
42      * @param identifier
43      *            transaction Identifier
44      * @param snapshot
45      *            Snapshot which will be modified.
46      * @param readyImpl
47      *            Implementation of ready method.
48      */
49     public SnapshotBackedWriteTransaction(final Object identifier, final DataTreeSnapshot snapshot,
50             final TransactionReadyPrototype readyImpl) {
51         super(identifier);
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);
55     }
56
57     @Override
58     public void close() {
59         LOG.debug("Store transaction: {} : Closed", getIdentifier());
60         this.mutableTree = null;
61         this.readyImpl = null;
62     }
63
64     @Override
65     public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
66         checkNotReady();
67         try {
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
74             // or Error
75             Throwables.propagateIfPossible(e);
76             // FIXME: Introduce proper checked exception
77             throw new IllegalArgumentException("Illegal input data.", e);
78         }
79     }
80
81     @Override
82     public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
83         checkNotReady();
84         try {
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
91             // or Error
92             Throwables.propagateIfPossible(e);
93             // FIXME: Introduce proper checked exception
94             throw new IllegalArgumentException("Illegal input data.", e);
95         }
96     }
97
98     @Override
99     public void delete(final YangInstanceIdentifier path) {
100         checkNotReady();
101         try {
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
108             // or Error
109             Throwables.propagateIfPossible(e);
110             // FIXME: Introduce proper checked exception
111             throw new IllegalArgumentException("Illegal path to delete.", e);
112         }
113     }
114
115     protected final boolean isReady() {
116         return ready;
117     }
118
119     protected final void checkNotReady() {
120         checkState(!ready, "Transaction %s is ready. No further modifications allowed.", getIdentifier());
121     }
122
123     @Override
124     public synchronized DOMStoreThreePhaseCommitCohort ready() {
125         checkState(!ready, "Transaction %s is already ready.", getIdentifier());
126         ready = true;
127         LOG.debug("Store transaction: {} : Ready", getIdentifier());
128         mutableTree.ready();
129         return readyImpl.ready(this);
130     }
131
132     protected DataTreeModification getMutatedView() {
133         return mutableTree;
134     }
135
136     @Override
137     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
138         return toStringHelper.add("ready", isReady());
139     }
140
141     /**
142      * Prototype implementation of
143      * {@link #ready(org.opendaylight.controller.md.sal.dom.store.impl.SnapshotBackedWriteTransaction)}
144      *
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.
148      *
149      */
150     public static interface TransactionReadyPrototype {
151
152         /**
153          * Returns a commit coordinator associated with supplied transactions.
154          *
155          * This call must not fail.
156          *
157          * @param tx
158          *            Transaction on which ready was invoked.
159          * @return DOMStoreThreePhaseCommitCohort associated with transaction
160          */
161         DOMStoreThreePhaseCommitCohort ready(SnapshotBackedWriteTransaction tx);
162     }
163 }