Improve segmented journal actor metrics
[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 package org.opendaylight.controller.cluster.databroker;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import java.util.Collection;
16 import java.util.EnumMap;
17 import java.util.Map;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionFactory;
22
23 public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements DOMDataTreeTransaction {
24
25     private final EnumMap<LogicalDatastoreType, T> backingTxs;
26     private final Object identifier;
27     private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
28
29     /**
30      * Creates new composite Transactions.
31      *
32      * @param identifier Identifier of transaction.
33      */
34     protected AbstractDOMBrokerTransaction(final Object identifier,
35             Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
36         this.identifier = requireNonNull(identifier, "Identifier should not be null");
37         this.storeTxFactories = requireNonNull(storeTxFactories, "Store Transaction Factories should not be null");
38         this.backingTxs = new EnumMap<>(LogicalDatastoreType.class);
39     }
40
41     /**
42      * Returns subtransaction associated with supplied key.
43      *
44      * @param key the data store type key
45      * @return the subtransaction
46      * @throws NullPointerException
47      *             if key is null
48      * @throws IllegalArgumentException
49      *             if no subtransaction is associated with key.
50      */
51     protected final T getSubtransaction(final LogicalDatastoreType key) {
52         requireNonNull(key, "key must not be null.");
53
54         T ret = backingTxs.get(key);
55         if (ret == null) {
56             ret = createTransaction(key);
57             backingTxs.put(key, ret);
58         }
59         checkArgument(ret != null, "No subtransaction associated with %s", key);
60         return ret;
61     }
62
63     protected abstract T createTransaction(LogicalDatastoreType key);
64
65     /**
66      * Returns immutable Iterable of all subtransactions.
67      *
68      */
69     protected Collection<T> getSubtransactions() {
70         return backingTxs.values();
71     }
72
73     @Override
74     public Object getIdentifier() {
75         return identifier;
76     }
77
78     @SuppressWarnings("checkstyle:IllegalCatch")
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(LogicalDatastoreType type) {
105         return storeTxFactories.get(type);
106     }
107
108     @Override
109     public final String toString() {
110         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
111     }
112
113     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
114         return toStringHelper.add("identifier", identifier);
115     }
116 }