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