c20c3650d2fc217a0a09109084b260b6d6031275
[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  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.controller.md.sal.dom.broker.impl;
8
9 import com.google.common.base.Preconditions;
10 import com.google.common.collect.ImmutableMap;
11
12 import java.util.Collection;
13
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  * 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         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
33
34     private final ImmutableMap<K, T> backingTxs;
35     private final Object identifier;
36
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 ImmutableMap<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
55      * @return
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         Preconditions.checkArgument(backingTxs.containsKey(key), "No subtransaction associated with %s", key);
64         return backingTxs.get(key);
65     }
66
67     /**
68      * Returns immutable Iterable of all subtransactions.
69      *
70      */
71     protected Collection<T> getSubtransactions() {
72         return backingTxs.values();
73     }
74
75     @Override
76     public Object getIdentifier() {
77         return identifier;
78     }
79
80     protected void closeSubtransactions() {
81         /*
82          *  We share one exception for all failures, which are added
83          *  as supressedExceptions to it.
84          *
85          */
86         IllegalStateException failure = null;
87         for (T subtransaction : backingTxs.values()) {
88             try {
89                 subtransaction.close();
90             } catch (Exception e) {
91                 // If we did not allocated failure we allocate it
92                 if(failure == null) {
93                     failure = new IllegalStateException("Uncaught exception occured during closing transaction.", e);
94                 } else {
95                     // We update it with addotional exceptions, which occured during error.
96                     failure.addSuppressed(e);
97                 }
98             }
99         }
100         // If we have failure, we throw it at after all attempts to close.
101         if(failure != null) {
102             throw failure;
103         }
104     }
105 }