2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.databroker;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
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;
23 public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements DOMDataTreeTransaction {
25 private final EnumMap<LogicalDatastoreType, T> backingTxs;
26 private final Object identifier;
27 private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
30 * Creates new composite Transactions.
32 * @param identifier Identifier of transaction.
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);
42 * Returns subtransaction associated with supplied key.
44 * @param key the data store type key
45 * @return the subtransaction
46 * @throws NullPointerException
48 * @throws IllegalArgumentException
49 * if no subtransaction is associated with key.
51 protected final T getSubtransaction(final LogicalDatastoreType key) {
52 requireNonNull(key, "key must not be null.");
54 T ret = backingTxs.get(key);
56 ret = createTransaction(key);
57 backingTxs.put(key, ret);
59 checkArgument(ret != null, "No subtransaction associated with %s", key);
63 protected abstract T createTransaction(LogicalDatastoreType key);
66 * Returns immutable Iterable of all subtransactions.
69 protected Collection<T> getSubtransactions() {
70 return backingTxs.values();
74 public Object getIdentifier() {
78 @SuppressWarnings("checkstyle:IllegalCatch")
79 protected void closeSubtransactions() {
81 * We share one exception for all failures, which are added
82 * as supressedExceptions to it.
84 IllegalStateException failure = null;
85 for (T subtransaction : backingTxs.values()) {
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);
93 // We update it with additional exceptions, which occurred during error.
94 failure.addSuppressed(e);
98 // If we have failure, we throw it at after all attempts to close.
99 if (failure != null) {
104 protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) {
105 return storeTxFactories.get(type);
109 public final String toString() {
110 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
113 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
114 return toStringHelper.add("identifier", identifier);