Merge "Bug 2538: Remove redundant Augmentation checks and tests"
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / TransactionProvider.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.mdsal.connector;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
22 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 //TODO make a global TransactionProvider for all Netconf sessions instead of each session having one.
27 public class TransactionProvider implements AutoCloseable{
28
29     private static final Logger LOG = LoggerFactory.getLogger(TransactionProvider.class);
30
31     private final DOMDataBroker dataBroker;
32
33     private DOMDataReadWriteTransaction candidateTransaction = null;
34     private DOMDataReadWriteTransaction runningTransaction = null;
35     private final List<DOMDataReadWriteTransaction> allOpenReadWriteTransactions = new ArrayList<>();
36
37     private final String netconfSessionIdForReporting;
38
39     private static final String  NO_TRANSACTION_FOUND_FOR_SESSION = "No candidateTransaction found for session ";
40
41
42     public TransactionProvider(DOMDataBroker dataBroker, String netconfSessionIdForReporting) {
43         this.dataBroker = dataBroker;
44         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
45     }
46
47     @Override
48     public synchronized void close() throws Exception {
49         for (DOMDataReadWriteTransaction rwt : allOpenReadWriteTransactions) {
50             rwt.cancel();
51         }
52
53         allOpenReadWriteTransactions.clear();
54     }
55
56     public synchronized Optional<DOMDataReadWriteTransaction> getCandidateTransaction() {
57         if (candidateTransaction == null) {
58             return Optional.absent();
59         }
60
61         return Optional.of(candidateTransaction);
62     }
63
64     public synchronized DOMDataReadWriteTransaction getOrCreateTransaction() {
65         if (getCandidateTransaction().isPresent()) {
66             return getCandidateTransaction().get();
67         }
68
69         candidateTransaction = dataBroker.newReadWriteTransaction();
70         allOpenReadWriteTransactions.add(candidateTransaction);
71         return candidateTransaction;
72     }
73
74     public synchronized boolean commitTransaction() throws NetconfDocumentedException {
75         if (!getCandidateTransaction().isPresent()) {
76             throw new NetconfDocumentedException(NO_TRANSACTION_FOUND_FOR_SESSION + netconfSessionIdForReporting,
77                     ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error);
78         }
79
80         CheckedFuture<Void, TransactionCommitFailedException> future = candidateTransaction.submit();
81         try {
82             future.checkedGet();
83         } catch (TransactionCommitFailedException e) {
84             LOG.debug("Transaction {} failed on", candidateTransaction, e);
85             throw new NetconfDocumentedException("Transaction commit failed on " + e.getMessage() + " " + netconfSessionIdForReporting,
86                     ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error);
87         }
88         allOpenReadWriteTransactions.remove(candidateTransaction);
89         candidateTransaction = null;
90
91         return true;
92     }
93
94     public synchronized void abortTransaction() {
95         LOG.debug("Aborting current candidateTransaction");
96         Optional<DOMDataReadWriteTransaction> otx = getCandidateTransaction();
97         Preconditions.checkState(otx.isPresent(), NO_TRANSACTION_FOUND_FOR_SESSION + netconfSessionIdForReporting);
98         candidateTransaction.cancel();
99         allOpenReadWriteTransactions.remove(candidateTransaction);
100         candidateTransaction = null;
101     }
102
103     public synchronized DOMDataReadWriteTransaction createRunningTransaction() {
104         runningTransaction = dataBroker.newReadWriteTransaction();
105         allOpenReadWriteTransactions.add(runningTransaction);
106         return runningTransaction;
107     }
108
109     public synchronized boolean commitRunningTransaction(DOMDataReadWriteTransaction tx) throws NetconfDocumentedException {
110         allOpenReadWriteTransactions.remove(tx);
111
112         CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
113         try {
114             future.checkedGet();
115         } catch (TransactionCommitFailedException e) {
116             LOG.debug("Transaction {} failed on", tx, e);
117             throw new NetconfDocumentedException("Transaction commit failed on " + e.getMessage() + " " + netconfSessionIdForReporting,
118                     ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error);
119         }
120
121         return true;
122     }
123
124     public synchronized void abortRunningTransaction(DOMDataReadWriteTransaction tx) {
125         LOG.debug("Aborting current running Transaction");
126         Preconditions.checkState(runningTransaction != null, NO_TRANSACTION_FOUND_FOR_SESSION + netconfSessionIdForReporting);
127         tx.cancel();
128         allOpenReadWriteTransactions.remove(tx);
129     }
130
131 }