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.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
20 * Composite DOM Transaction backed by {@link DOMStoreTransaction}.
23 * Abstract base for composite transaction, which provides access only to common
24 * functionality as retrieval of subtransaction, close method and retrieval of
27 * @param <T> Subtransaction type
29 abstract class AbstractDOMForwardedCompositeTransaction<T extends DOMStoreTransaction>
30 implements DOMDataTreeTransaction {
31 private final Map<LogicalDatastoreType, T> backingTxs;
32 private final Object identifier;
35 * Creates new composite Transactions.
38 * Identifier of transaction.
40 * Key,value map of backing transactions.
42 protected AbstractDOMForwardedCompositeTransaction(final Object identifier,
43 final Map<LogicalDatastoreType, T> backingTxs) {
44 this.identifier = requireNonNull(identifier, "Identifier should not be null");
45 this.backingTxs = requireNonNull(backingTxs, "Backing transactions should not be null");
49 * Returns subtransaction associated with supplied key.
51 * @param key is used to retrieve subtransaction object
52 * @return the subtransaction object
53 * @throws NullPointerException
55 * @throws IllegalArgumentException
56 * if no subtransaction is associated with key.
58 protected final T getSubtransaction(final LogicalDatastoreType key) {
59 requireNonNull(key, "key must not be null.");
61 final T ret = backingTxs.get(key);
62 checkArgument(ret != null, "No subtransaction associated with %s", key);
67 * Returns immutable Iterable of all subtransactions.
70 protected Collection<T> getSubtransactions() {
71 return backingTxs.values();
75 public Object getIdentifier() {
79 @SuppressWarnings("checkstyle:IllegalCatch")
80 protected void closeSubtransactions() {
82 * We share one exception for all failures, which are added
83 * as supressedExceptions to it.
85 IllegalStateException failure = null;
86 for (T subtransaction : backingTxs.values()) {
88 subtransaction.close();
89 } catch (Exception e) {
90 // If we did not allocate failure we allocate it
91 if (failure == null) {
92 failure = new IllegalStateException("Uncaught exception occurred during closing transaction", e);
94 // We update it with additional exceptions, which occurred during error.
95 failure.addSuppressed(e);
99 // If we have failure, we throw it at after all attempts to close.
100 if (failure != null) {