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
9 package org.opendaylight.controller.cluster.databroker;
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;
17 import org.opendaylight.mdsal.common.api.AsyncTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionFactory;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements
25 AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
27 private final EnumMap<LogicalDatastoreType, T> backingTxs;
28 private final Object identifier;
29 private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
32 * Creates new composite Transactions.
34 * @param identifier Identifier of transaction.
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);
45 * Returns subtransaction associated with supplied key.
47 * @param key the data store type key
48 * @return the subtransaction
49 * @throws NullPointerException
51 * @throws IllegalArgumentException
52 * if no subtransaction is associated with key.
54 protected final T getSubtransaction(final LogicalDatastoreType key) {
55 Preconditions.checkNotNull(key, "key must not be null.");
57 T ret = backingTxs.get(key);
59 ret = createTransaction(key);
60 backingTxs.put(key, ret);
62 Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
66 protected abstract T createTransaction(LogicalDatastoreType key);
69 * Returns immutable Iterable of all subtransactions.
72 protected Collection<T> getSubtransactions() {
73 return backingTxs.values();
77 public Object getIdentifier() {
81 @SuppressWarnings("checkstyle:IllegalCatch")
82 protected void closeSubtransactions() {
84 * We share one exception for all failures, which are added
85 * as supressedExceptions to it.
87 IllegalStateException failure = null;
88 for (T subtransaction : backingTxs.values()) {
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);
96 // We update it with additional exceptions, which occurred during error.
97 failure.addSuppressed(e);
101 // If we have failure, we throw it at after all attempts to close.
102 if (failure != null) {
107 protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) {
108 return storeTxFactories.get(type);
112 public final String toString() {
113 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
116 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
117 return toStringHelper.add("identifier", identifier);