2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.mdsal.connector;
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;
26 //TODO make a global TransactionProvider for all Netconf sessions instead of each session having one.
27 public class TransactionProvider implements AutoCloseable{
29 private static final Logger LOG = LoggerFactory.getLogger(TransactionProvider.class);
31 private final DOMDataBroker dataBroker;
33 private DOMDataReadWriteTransaction candidateTransaction = null;
34 private DOMDataReadWriteTransaction runningTransaction = null;
35 private final List<DOMDataReadWriteTransaction> allOpenReadWriteTransactions = new ArrayList<>();
37 private final String netconfSessionIdForReporting;
39 private static final String NO_TRANSACTION_FOUND_FOR_SESSION = "No candidateTransaction found for session ";
42 public TransactionProvider(DOMDataBroker dataBroker, String netconfSessionIdForReporting) {
43 this.dataBroker = dataBroker;
44 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
48 public synchronized void close() throws Exception {
49 for (DOMDataReadWriteTransaction rwt : allOpenReadWriteTransactions) {
53 allOpenReadWriteTransactions.clear();
56 public synchronized Optional<DOMDataReadWriteTransaction> getCandidateTransaction() {
57 if (candidateTransaction == null) {
58 return Optional.absent();
61 return Optional.of(candidateTransaction);
64 public synchronized DOMDataReadWriteTransaction getOrCreateTransaction() {
65 if (getCandidateTransaction().isPresent()) {
66 return getCandidateTransaction().get();
69 candidateTransaction = dataBroker.newReadWriteTransaction();
70 allOpenReadWriteTransactions.add(candidateTransaction);
71 return candidateTransaction;
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);
80 CheckedFuture<Void, TransactionCommitFailedException> future = candidateTransaction.submit();
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);
88 allOpenReadWriteTransactions.remove(candidateTransaction);
89 candidateTransaction = null;
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;
103 public synchronized DOMDataReadWriteTransaction createRunningTransaction() {
104 runningTransaction = dataBroker.newReadWriteTransaction();
105 allOpenReadWriteTransactions.add(runningTransaction);
106 return runningTransaction;
109 public synchronized boolean commitRunningTransaction(DOMDataReadWriteTransaction tx) throws NetconfDocumentedException {
110 allOpenReadWriteTransactions.remove(tx);
112 CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
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);
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);
128 allOpenReadWriteTransactions.remove(tx);