Fix warnings/javadocs in sal-distributed-datastore
[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<T extends DOMStoreTransaction> implements
23         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
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 = Preconditions.checkNotNull(identifier, "Identifier should not be null");
37         this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories,
38                 "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 the data store type key
46      * @return the subtransaction
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 LogicalDatastoreType 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 LogicalDatastoreType 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     @SuppressWarnings("checkstyle:IllegalCatch")
80     protected void closeSubtransactions() {
81         /*
82          * We share one exception for all failures, which are added
83          * as supressedExceptions to it.
84          */
85         IllegalStateException failure = null;
86         for (T subtransaction : backingTxs.values()) {
87             try {
88                 subtransaction.close();
89             } catch (Exception e) {
90                 // If we did not allocated failure we allocate it
91                 if (failure == null) {
92                     failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
93                 } else {
94                     // We update it with additional exceptions, which occurred during error.
95                     failure.addSuppressed(e);
96                 }
97             }
98         }
99         // If we have failure, we throw it at after all attempts to close.
100         if (failure != null) {
101             throw failure;
102         }
103     }
104
105     protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) {
106         return storeTxFactories.get(type);
107     }
108 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.