Remove AsyncDataBroker and related classes
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMForwardedCompositeTransaction.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.mdsal.dom.broker;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Map;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
17
18 /**
19  * Composite DOM Transaction backed by {@link DOMStoreTransaction}.
20  *
21  *<p>
22  * Abstract base for composite transaction, which provides access only to common
23  * functionality as retrieval of subtransaction, close method and retrieval of
24  * identifier.
25  *
26  * @param <K>
27  *            Subtransaction distinguisher
28  * @param <T>
29  *            Subtransaction type
30  */
31 abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTransaction> implements
32         DOMDataTreeTransaction {
33
34     private final Map<K, T> backingTxs;
35     private final Object identifier;
36
37     /**
38      * Creates new composite Transactions.
39      *
40      * @param identifier
41      *            Identifier of transaction.
42      * @param backingTxs
43      *            Key,value map of backing transactions.
44      */
45     protected AbstractDOMForwardedCompositeTransaction(final Object identifier, final Map<K, T> backingTxs) {
46         this.identifier = requireNonNull(identifier, "Identifier should not be null");
47         this.backingTxs = requireNonNull(backingTxs, "Backing transactions should not be null");
48     }
49
50     /**
51      * Returns subtransaction associated with supplied key.
52      *
53      * @param key is used to retrieve subtransaction object
54      * @return the subtransaction object
55      * @throws NullPointerException
56      *             if key is null
57      * @throws IllegalArgumentException
58      *             if no subtransaction is associated with key.
59      */
60     protected final T getSubtransaction(final K key) {
61         requireNonNull(key, "key must not be null.");
62
63         final T ret = backingTxs.get(key);
64         checkArgument(ret != null, "No subtransaction associated with %s", key);
65         return ret;
66     }
67
68     /**
69      * Returns immutable Iterable of all subtransactions.
70      *
71      */
72     protected Collection<T> getSubtransactions() {
73         return backingTxs.values();
74     }
75
76     @Override
77     public Object getIdentifier() {
78         return identifier;
79     }
80
81     @SuppressWarnings("checkstyle:IllegalCatch")
82     protected void closeSubtransactions() {
83         /*
84          * We share one exception for all failures, which are added
85          * as supressedExceptions to it.
86          */
87         IllegalStateException failure = null;
88         for (T subtransaction : backingTxs.values()) {
89             try {
90                 subtransaction.close();
91             } catch (Exception e) {
92                 // If we did not allocated failure we allocate it
93                 if (failure == null) {
94                     failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
95                 } else {
96                     // We update it with additional exceptions, which occurred during error.
97                     failure.addSuppressed(e);
98                 }
99             }
100         }
101         // If we have failure, we throw it at after all attempts to close.
102         if (failure != null) {
103             throw failure;
104         }
105     }
106 }