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.Preconditions;
12 import java.util.Collection;
13 import java.util.EnumMap;
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;
22 public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements
23 AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
25 private EnumMap<LogicalDatastoreType, T> backingTxs;
26 private final Object identifier;
27 private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
31 * Creates new composite Transactions.
34 * Identifier of transaction.
36 protected AbstractDOMBrokerTransaction(final Object identifier, Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
37 this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
38 this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories, "Store Transaction Factories should not be null");
39 this.backingTxs = new EnumMap<>(LogicalDatastoreType.class);
43 * Returns subtransaction associated with supplied key.
47 * @throws NullPointerException
49 * @throws IllegalArgumentException
50 * if no subtransaction is associated with key.
52 protected final T getSubtransaction(final LogicalDatastoreType key) {
53 Preconditions.checkNotNull(key, "key must not be null.");
55 T ret = backingTxs.get(key);
57 ret = createTransaction(key);
58 backingTxs.put(key, ret);
60 Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
64 protected abstract T createTransaction(final LogicalDatastoreType key);
67 * Returns immutable Iterable of all subtransactions.
70 protected Collection<T> getSubtransactions() {
71 return backingTxs.values();
75 public Object getIdentifier() {
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);