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