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 org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
10 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
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<InstanceIdentifier, NormalizedNode<?, ?>> {
32 private final ImmutableMap<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 ImmutableMap<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.");
61 Preconditions.checkArgument(backingTxs.containsKey(key), "No subtransaction associated with %s", key);
62 return backingTxs.get(key);
66 * Returns immutable Iterable of all subtransactions.
69 protected Iterable<T> getSubtransactions() {
70 return backingTxs.values();
74 public Object getIdentifier() {
78 protected void closeSubtransactions() {
80 * We share one exception for all failures, which are added
81 * as supressedExceptions to it.
84 IllegalStateException failure = null;
85 for (T subtransaction : backingTxs.values()) {
87 subtransaction.close();
88 } catch (Exception e) {
89 // If we did not allocated failure we allocate it
91 failure = new IllegalStateException("Uncaught exception occured during closing transaction.", e);
93 // We update it with addotional exceptions, which occured during error.
94 failure.addSuppressed(e);
98 // If we have failure, we throw it at after all attempts to close.