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