Add AbstractDOMBrokerTransaction.toString()
[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.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import java.util.Collection;
15 import java.util.EnumMap;
16 import java.util.Map;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements
25         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
26
27     private final EnumMap<LogicalDatastoreType, T> backingTxs;
28     private final Object identifier;
29     private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
30
31     /**
32      * Creates new composite Transactions.
33      *
34      * @param identifier Identifier of transaction.
35      */
36     protected AbstractDOMBrokerTransaction(final Object identifier,
37             Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
38         this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
39         this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories,
40                 "Store Transaction Factories should not be null");
41         this.backingTxs = new EnumMap<>(LogicalDatastoreType.class);
42     }
43
44     /**
45      * Returns subtransaction associated with supplied key.
46      *
47      * @param key the data store type key
48      * @return the subtransaction
49      * @throws NullPointerException
50      *             if key is null
51      * @throws IllegalArgumentException
52      *             if no subtransaction is associated with key.
53      */
54     protected final T getSubtransaction(final LogicalDatastoreType key) {
55         Preconditions.checkNotNull(key, "key must not be null.");
56
57         T ret = backingTxs.get(key);
58         if (ret == null) {
59             ret = createTransaction(key);
60             backingTxs.put(key, ret);
61         }
62         Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
63         return ret;
64     }
65
66     protected abstract T createTransaction(LogicalDatastoreType key);
67
68     /**
69      * Returns immutable Iterable of all subtransactions.
70      *
71      */
72     protected Collection<T> getSubtransactions() {
73         return backingTxs.values();
74     }
75
76     @Override
77     public Object getIdentifier() {
78         return identifier;
79     }
80
81     @SuppressWarnings("checkstyle:IllegalCatch")
82     protected void closeSubtransactions() {
83         /*
84          * We share one exception for all failures, which are added
85          * as supressedExceptions to it.
86          */
87         IllegalStateException failure = null;
88         for (T subtransaction : backingTxs.values()) {
89             try {
90                 subtransaction.close();
91             } catch (Exception e) {
92                 // If we did not allocated failure we allocate it
93                 if (failure == null) {
94                     failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
95                 } else {
96                     // We update it with additional exceptions, which occurred during error.
97                     failure.addSuppressed(e);
98                 }
99             }
100         }
101         // If we have failure, we throw it at after all attempts to close.
102         if (failure != null) {
103             throw failure;
104         }
105     }
106
107     protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) {
108         return storeTxFactories.get(type);
109     }
110
111     @Override
112     public final String toString() {
113         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
114     }
115
116     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
117         return toStringHelper.add("identifier", identifier);
118     }
119 }

©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.