d17b71ec09e670c47ddeabc0e335765d548f92fb
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMDataBrokerTransactionChainImpl.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 com.google.common.base.Optional;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.Map;
13 import java.util.concurrent.atomic.AtomicLong;
14 import javax.annotation.concurrent.GuardedBy;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * NormalizedNode implementation of {@link org.opendaylight.controller.md.sal.common.api.data.TransactionChain} which is backed
27  * by several {@link DOMStoreTransactionChain} differentiated by provided
28  * {@link org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType} type.
29  *
30  */
31 public class DOMDataBrokerTransactionChainImpl extends AbstractDOMForwardedTransactionFactory<DOMStoreTransactionChain>
32         implements DOMTransactionChain, DOMDataCommitErrorListener {
33
34     private static final Logger LOG = LoggerFactory.getLogger(DOMDataBrokerTransactionChainImpl.class);
35     private final DOMDataCommitExecutor coordinator;
36     private final TransactionChainListener listener;
37     private final long chainId;
38     private final AtomicLong txNum = new AtomicLong();
39     @GuardedBy("this")
40     private boolean failed = false;
41
42     /**
43      *
44      * @param chainId
45      *            ID of transaction chain
46      * @param chains
47      *            Backing {@link DOMStoreTransactionChain}s.
48      * @param coordinator
49      *            Commit Coordinator which should be used to coordinate commits
50      *            of transaction
51      *            produced by this chain.
52      * @param listener
53      *            Listener, which listens on transaction chain events.
54      * @throws NullPointerException
55      *             If any of arguments is null.
56      */
57     public DOMDataBrokerTransactionChainImpl(final long chainId,
58             final Map<LogicalDatastoreType, DOMStoreTransactionChain> chains,
59             final DOMDataCommitExecutor coordinator, final TransactionChainListener listener) {
60         super(chains);
61         this.chainId = chainId;
62         this.coordinator = Preconditions.checkNotNull(coordinator);
63         this.listener = Preconditions.checkNotNull(listener);
64     }
65
66     @Override
67     protected Object newTransactionIdentifier() {
68         return "DOM-CHAIN-" + chainId + "-" + txNum.getAndIncrement();
69     }
70
71     @Override
72     public synchronized CheckedFuture<Void,TransactionCommitFailedException> submit(
73             final DOMDataWriteTransaction transaction, final Iterable<DOMStoreThreePhaseCommitCohort> cohorts) {
74         return coordinator.submit(transaction, cohorts, Optional.<DOMDataCommitErrorListener> of(this));
75     }
76
77     @Override
78     public synchronized void close() {
79         super.close();
80         for (DOMStoreTransactionChain subChain : getTxFactories().values()) {
81             subChain.close();
82         }
83
84         if (!failed) {
85             LOG.debug("Transaction chain {}¬†successfully finished.", this);
86             listener.onTransactionChainSuccessful(this);
87         }
88     }
89
90     @Override
91     public synchronized void onCommitFailed(final DOMDataWriteTransaction tx, final Throwable cause) {
92         failed = true;
93         LOG.debug("Transaction chain {}¬†failed.", this, cause);
94         listener.onTransactionChainFailed(this, tx, cause);
95     }
96 }