BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / AbstractDOMForwardedCompositeTransaction.java
1 /*
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
6  */
7 package org.opendaylight.controller.md.sal.dom.broker.impl;
8
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.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16
17 /**
18  * Composite DOM Transaction backed by {@link DOMStoreTransaction}.
19  *
20  * Abstract base for composite transaction, which provides access only to common
21  * functionality as retrieval of subtransaction, close method and retrieval of
22  * identifier.
23  *
24  * @param <K>
25  *            Subtransaction distinguisher
26  * @param <T>
27  *            Subtransaction type
28  */
29 abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTransaction> implements
30         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
31
32     private final ImmutableMap<K, T> backingTxs;
33     private final Object identifier;
34
35     /**
36      *
37      * Creates new composite Transactions.
38      *
39      * @param identifier
40      *            Identifier of transaction.
41      * @param backingTxs
42      *            Key,value map of backing transactions.
43      */
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");
47     }
48
49     /**
50      * Returns subtransaction associated with supplied key.
51      *
52      * @param key
53      * @return
54      * @throws NullPointerException
55      *             if key is null
56      * @throws IllegalArgumentException
57      *             if no subtransaction is associated with key.
58      */
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);
63     }
64
65     /**
66      * Returns immutable Iterable of all subtransactions.
67      *
68      */
69     protected Iterable<T> getSubtransactions() {
70         return backingTxs.values();
71     }
72
73     @Override
74     public Object getIdentifier() {
75         return identifier;
76     }
77
78     protected void closeSubtransactions() {
79         /*
80          *  We share one exception for all failures, which are added
81          *  as supressedExceptions to it.
82          *
83          */
84         IllegalStateException failure = null;
85         for (T subtransaction : backingTxs.values()) {
86             try {
87                 subtransaction.close();
88             } catch (Exception e) {
89                 // If we did not allocated failure we allocate it
90                 if(failure == null) {
91                     failure = new IllegalStateException("Uncaught exception occured during closing transaction.", e);
92                 } else {
93                     // We update it with addotional exceptions, which occured during error.
94                     failure.addSuppressed(e);
95                 }
96             }
97         }
98         // If we have failure, we throw it at after all attempts to close.
99         if(failure != null) {
100             throw failure;
101         }
102     }
103 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.