Clean-up mdsal-netconf-connector
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / 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.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.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
19 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class TransactionProvider implements AutoCloseable{
27
28     private static final Logger LOG = LoggerFactory.getLogger(TransactionProvider.class);
29
30     private final DOMDataBroker dataBroker;
31
32     private DOMDataReadWriteTransaction candidateTransaction = null;
33     private DOMDataReadWriteTransaction runningTransaction = null;
34     private final List<DOMDataReadWriteTransaction> allOpenReadWriteTransactions = new ArrayList<>();
35
36     private final String netconfSessionIdForReporting;
37
38     private static final String  NO_TRANSACTION_FOUND_FOR_SESSION = "No candidateTransaction found for session ";
39
40
41     public TransactionProvider(DOMDataBroker dataBroker, String netconfSessionIdForReporting) {
42         this.dataBroker = dataBroker;
43         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
44     }
45
46     @Override
47     public synchronized void close() throws Exception {
48         for (DOMDataReadWriteTransaction rwt : allOpenReadWriteTransactions) {
49             rwt.cancel();
50         }
51
52         allOpenReadWriteTransactions.clear();
53     }
54
55     public synchronized Optional<DOMDataReadWriteTransaction> getCandidateTransaction() {
56         if (candidateTransaction == null) {
57             return Optional.absent();
58         }
59
60         return Optional.of(candidateTransaction);
61     }
62
63     public synchronized DOMDataReadWriteTransaction getOrCreateTransaction() {
64         if (getCandidateTransaction().isPresent()) {
65             return getCandidateTransaction().get();
66         }
67
68         candidateTransaction = dataBroker.newReadWriteTransaction();
69         allOpenReadWriteTransactions.add(candidateTransaction);
70         return candidateTransaction;
71     }
72
73     public synchronized boolean commitTransaction() throws DocumentedException {
74         if (!getCandidateTransaction().isPresent()) {
75             //making empty commit without prior opened transaction, just return true
76             LOG.debug("Making commit without open candidate transaction for session {}", netconfSessionIdForReporting);
77             return true;
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             final String cause = e.getCause() != null ? (" Cause: " + e.getCause().getMessage()) : "";
86             throw new DocumentedException("Transaction commit failed on " + e.getMessage() + " " + netconfSessionIdForReporting +
87                     cause,
88                     ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error);
89         } finally {
90             allOpenReadWriteTransactions.remove(candidateTransaction);
91             candidateTransaction = null;
92         }
93
94         return true;
95     }
96
97     public synchronized void abortTransaction() {
98         LOG.debug("Aborting current candidateTransaction");
99         Optional<DOMDataReadWriteTransaction> otx = getCandidateTransaction();
100         if (!otx.isPresent()) {
101             LOG.warn("discard-changes triggerd on an empty transaction for session: {}", netconfSessionIdForReporting );
102             return;
103         }
104         candidateTransaction.cancel();
105         allOpenReadWriteTransactions.remove(candidateTransaction);
106         candidateTransaction = null;
107     }
108
109     public synchronized DOMDataReadWriteTransaction createRunningTransaction() {
110         runningTransaction = dataBroker.newReadWriteTransaction();
111         allOpenReadWriteTransactions.add(runningTransaction);
112         return runningTransaction;
113     }
114
115     public synchronized void abortRunningTransaction(DOMDataReadWriteTransaction tx) {
116         LOG.debug("Aborting current running Transaction");
117         Preconditions.checkState(runningTransaction != null, NO_TRANSACTION_FOUND_FOR_SESSION + netconfSessionIdForReporting);
118         tx.cancel();
119         allOpenReadWriteTransactions.remove(tx);
120     }
121
122 }