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.controller.md.sal.dom.broker.impl;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 * 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 AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
34 private final Map<K, T> backingTxs;
35 private final Object identifier;
39 * Creates new composite Transactions.
42 * Identifier of transaction.
44 * Key,value map of backing transactions.
46 protected AbstractDOMForwardedCompositeTransaction(final Object identifier, final Map<K, T> backingTxs) {
47 this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
48 this.backingTxs = Preconditions.checkNotNull(backingTxs, "Backing transactions should not be null");
52 * Returns subtransaction associated with supplied key.
56 * @throws NullPointerException
58 * @throws IllegalArgumentException
59 * if no subtransaction is associated with key.
61 protected final T getSubtransaction(final K key) {
62 Preconditions.checkNotNull(key, "key must not be null.");
64 final T ret = backingTxs.get(key);
65 Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
70 * Returns immutable Iterable of all subtransactions.
73 protected Collection<T> getSubtransactions() {
74 return backingTxs.values();
78 public Object getIdentifier() {
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) {