2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.controller.md.sal.dom.broker.impl;
9 import com.google.common.base.Preconditions;
10 import java.util.Collection;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 * Composite DOM Transaction backed by {@link DOMStoreTransaction}.
20 * Abstract base for composite transaction, which provides access only to common
21 * functionality as retrieval of subtransaction, close method and retrieval of
25 * Subtransaction distinguisher
29 abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTransaction> implements
30 AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
32 private final Map<K, T> backingTxs;
33 private final Object identifier;
37 * Creates new composite Transactions.
40 * Identifier of transaction.
42 * Key,value map of backing transactions.
44 protected AbstractDOMForwardedCompositeTransaction(final Object identifier, final Map<K, T> backingTxs) {
45 this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
46 this.backingTxs = Preconditions.checkNotNull(backingTxs, "Backing transactions should not be null");
50 * Returns subtransaction associated with supplied key.
54 * @throws NullPointerException
56 * @throws IllegalArgumentException
57 * if no subtransaction is associated with key.
59 protected final T getSubtransaction(final K key) {
60 Preconditions.checkNotNull(key, "key must not be null.");
62 final T ret = backingTxs.get(key);
63 Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
68 * Returns immutable Iterable of all subtransactions.
71 protected Collection<T> getSubtransactions() {
72 return backingTxs.values();
76 public Object getIdentifier() {
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 allocated failure we allocate it
91 if (failure == null) {
92 failure = new IllegalStateException("Uncaught exception occured 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) {