2 * Copyright (c) 2014 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.mdsal.dom.broker;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
19 * Composite DOM Transaction backed by {@link DOMStoreTransaction}.
22 * Abstract base for composite transaction, which provides access only to common
23 * functionality as retrieval of subtransaction, close method and retrieval of
27 * Subtransaction distinguisher
31 abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTransaction> implements
32 DOMDataTreeTransaction {
34 private final Map<K, T> backingTxs;
35 private final Object identifier;
38 * Creates new composite Transactions.
41 * Identifier of transaction.
43 * Key,value map of backing transactions.
45 protected AbstractDOMForwardedCompositeTransaction(final Object identifier, final Map<K, T> backingTxs) {
46 this.identifier = requireNonNull(identifier, "Identifier should not be null");
47 this.backingTxs = requireNonNull(backingTxs, "Backing transactions should not be null");
51 * Returns subtransaction associated with supplied key.
53 * @param key is used to retrieve subtransaction object
54 * @return the subtransaction object
55 * @throws NullPointerException
57 * @throws IllegalArgumentException
58 * if no subtransaction is associated with key.
60 protected final T getSubtransaction(final K key) {
61 requireNonNull(key, "key must not be null.");
63 final T ret = backingTxs.get(key);
64 checkArgument(ret != null, "No subtransaction associated with %s", 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) {