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