Merge topic 'li/cleanup/javassist'
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBrokerTransaction.java
1 /*
2  * Copyright (c) 2015 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.cluster.databroker;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.EnumMap;
14 import java.util.Map;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21
22 public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransaction> implements
23         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
24
25     private Map<K, T> backingTxs;
26     private final Object identifier;
27     private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
28
29     /**
30      *
31      * Creates new composite Transactions.
32      *
33      * @param identifier
34      *            Identifier of transaction.
35      */
36     protected AbstractDOMBrokerTransaction(final Object identifier, Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
37         this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
38         this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories, "Store Transaction Factories should not be null");
39         this.backingTxs = new EnumMap(LogicalDatastoreType.class);
40     }
41
42     /**
43      * Returns subtransaction associated with supplied key.
44      *
45      * @param key
46      * @return
47      * @throws NullPointerException
48      *             if key is null
49      * @throws IllegalArgumentException
50      *             if no subtransaction is associated with key.
51      */
52     protected final T getSubtransaction(final K key) {
53         Preconditions.checkNotNull(key, "key must not be null.");
54
55         T ret = backingTxs.get(key);
56         if(ret == null){
57             ret = createTransaction(key);
58             backingTxs.put(key, ret);
59         }
60         Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
61         return ret;
62     }
63
64     protected abstract T createTransaction(final K key);
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     protected void closeSubtransactions() {
80         /*
81          * We share one exception for all failures, which are added
82          * as supressedExceptions to it.
83          */
84         IllegalStateException failure = null;
85         for (T subtransaction : backingTxs.values()) {
86             try {
87                 subtransaction.close();
88             } catch (Exception e) {
89                 // If we did not allocated failure we allocate it
90                 if (failure == null) {
91                     failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
92                 } else {
93                     // We update it with additional exceptions, which occurred during error.
94                     failure.addSuppressed(e);
95                 }
96             }
97         }
98         // If we have failure, we throw it at after all attempts to close.
99         if (failure != null) {
100             throw failure;
101         }
102     }
103
104     protected DOMStoreTransactionFactory getTxFactory(K type){
105         return storeTxFactories.get(type);
106     }
107 }