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
9 package org.opendaylight.mdsal.dom.broker;
11 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
13 import org.opendaylight.mdsal.common.api.AsyncTransaction;
14 import com.google.common.base.Preconditions;
15 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 * 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
28 * Subtransaction distinguisher
32 abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTransaction> implements
33 AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
35 private final Map<K, T> backingTxs;
36 private final Object identifier;
40 * Creates new composite Transactions.
43 * Identifier of transaction.
45 * Key,value map of backing transactions.
47 protected AbstractDOMForwardedCompositeTransaction(final Object identifier, final Map<K, T> backingTxs) {
48 this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
49 this.backingTxs = Preconditions.checkNotNull(backingTxs, "Backing transactions should not be null");
53 * Returns subtransaction associated with supplied key.
57 * @throws NullPointerException
59 * @throws IllegalArgumentException
60 * if no subtransaction is associated with key.
62 protected final T getSubtransaction(final K key) {
63 Preconditions.checkNotNull(key, "key must not be null.");
65 final T ret = backingTxs.get(key);
66 Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
71 * Returns immutable Iterable of all subtransactions.
74 protected Collection<T> getSubtransactions() {
75 return backingTxs.values();
79 public Object getIdentifier() {
83 protected void closeSubtransactions() {
85 * We share one exception for all failures, which are added
86 * as supressedExceptions to it.
88 IllegalStateException failure = null;
89 for (T subtransaction : backingTxs.values()) {
91 subtransaction.close();
92 } catch (Exception e) {
93 // If we did not allocated failure we allocate it
94 if (failure == null) {
95 failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
97 // We update it with additional exceptions, which occurred during error.
98 failure.addSuppressed(e);
102 // If we have failure, we throw it at after all attempts to close.
103 if (failure != null) {