X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager-facade-xml%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Ffacade%2Fxml%2Ftransactions%2FTransactionProvider.java;fp=opendaylight%2Fconfig%2Fconfig-manager-facade-xml%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Ffacade%2Fxml%2Ftransactions%2FTransactionProvider.java;h=0000000000000000000000000000000000000000;hp=e255757dc188cbaa87bffe0971965b6ee2a1176b;hb=ac6f2699cd0c1e340cc32e8f0d0ca94c8e9c0cc0;hpb=f43b01b81319959b1907e3e04537f5169e7f33d8 diff --git a/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/transactions/TransactionProvider.java b/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/transactions/TransactionProvider.java deleted file mode 100644 index e255757dc1..0000000000 --- a/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/transactions/TransactionProvider.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.controller.config.facade.xml.transactions; - -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import javax.management.InstanceNotFoundException; -import javax.management.ObjectName; - -import org.opendaylight.controller.config.api.ConflictingVersionException; -import org.opendaylight.controller.config.api.ValidationException; -import org.opendaylight.controller.config.api.jmx.CommitStatus; -import org.opendaylight.controller.config.util.ConfigRegistryClient; -import org.opendaylight.controller.config.util.ConfigTransactionClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TransactionProvider implements AutoCloseable { - private static final Logger LOG = LoggerFactory.getLogger(TransactionProvider.class); - - private final ConfigRegistryClient configRegistryClient; - - private final String sessionIdForReporting; - private ObjectName candidateTx; - private ObjectName readTx; - private final List allOpenedTransactions = new ArrayList<>(); - private static final String NO_TRANSACTION_FOUND_FOR_SESSION = "No transaction found for session "; - - public TransactionProvider(final ConfigRegistryClient configRegistryClient, final String sessionIdForReporting) { - this.configRegistryClient = configRegistryClient; - this.sessionIdForReporting = sessionIdForReporting; - } - - @Override - @SuppressWarnings("IllegalCatch") - public synchronized void close() { - for (ObjectName tx : allOpenedTransactions) { - try { - if (isStillOpenTransaction(tx)) { - configRegistryClient.getConfigTransactionClient(tx).abortConfig(); - } - } catch (final RuntimeException e) { - LOG.debug("Ignoring exception while closing transaction {}", tx, e); - } - } - allOpenedTransactions.clear(); - } - - public synchronized Optional getTransaction() { - - if (candidateTx == null) { - return Optional.absent(); - } - - // Transaction was already closed somehow - if (!isStillOpenTransaction(candidateTx)) { - LOG.warn("Fixing illegal state: transaction {} was closed in {}", candidateTx, sessionIdForReporting); - candidateTx = null; - return Optional.absent(); - } - return Optional.of(candidateTx); - } - - public synchronized Optional getReadTransaction() { - - if (readTx == null) { - return Optional.absent(); - } - - // Transaction was already closed somehow - if (!isStillOpenTransaction(readTx)) { - LOG.warn("Fixing illegal state: transaction {} was closed in {}", readTx, sessionIdForReporting); - readTx = null; - return Optional.absent(); - } - return Optional.of(readTx); - } - - private boolean isStillOpenTransaction(final ObjectName transaction) { - return configRegistryClient.getOpenConfigs().contains(transaction); - } - - public synchronized ObjectName getOrCreateTransaction() { - Optional ta = getTransaction(); - - if (ta.isPresent()) { - return ta.get(); - } - candidateTx = configRegistryClient.beginConfig(); - allOpenedTransactions.add(candidateTx); - return candidateTx; - } - - public synchronized ObjectName getOrCreateReadTransaction() { - Optional ta = getReadTransaction(); - - if (ta.isPresent()) { - return ta.get(); - } - readTx = configRegistryClient.beginConfig(); - allOpenedTransactions.add(readTx); - return readTx; - } - - /** - * Used for editConfig test option. - */ - public synchronized ObjectName getTestTransaction() { - ObjectName testTx = configRegistryClient.beginConfig(); - allOpenedTransactions.add(testTx); - return testTx; - } - - /** - * Commit and notification send must be atomic. - */ - public CommitStatus commitTransaction() throws ValidationException, ConflictingVersionException { - return commitTransaction(configRegistryClient); - } - - /** - * Commit and notification send must be atomic. - */ - public synchronized CommitStatus commitTransaction(final ConfigRegistryClient client) - throws ValidationException, ConflictingVersionException { - if (!getTransaction().isPresent()) { - // making empty commit without prior opened transaction, just return commit - // status with empty lists - LOG.debug("Making commit without open candidate transaction for session {}", sessionIdForReporting); - return new CommitStatus(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - } - final Optional maybeTaON = getTransaction(); - ObjectName taON = maybeTaON.get(); - try { - CommitStatus status = client.commitConfig(taON); - // clean up - allOpenedTransactions.remove(candidateTx); - candidateTx = null; - return status; - } catch (final ValidationException validationException) { - // no clean up: user can reconfigure and recover this transaction - LOG.warn("Transaction {} failed on {}", taON, validationException.toString()); - throw validationException; - } catch (final ConflictingVersionException e) { - LOG.debug("Exception while commit of {}, aborting transaction", taON, e); - // clean up - abortTransaction(); - throw e; - } - } - - public synchronized void abortTransaction() { - LOG.debug("Aborting current transaction"); - Optional taON = getTransaction(); - Preconditions.checkState(taON.isPresent(), NO_TRANSACTION_FOUND_FOR_SESSION + sessionIdForReporting); - - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(taON.get()); - transactionClient.abortConfig(); - allOpenedTransactions.remove(candidateTx); - candidateTx = null; - } - - public synchronized void closeReadTransaction() { - LOG.debug("Closing read transaction"); - Optional taON = getReadTransaction(); - Preconditions.checkState(taON.isPresent(), NO_TRANSACTION_FOUND_FOR_SESSION + sessionIdForReporting); - - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(taON.get()); - transactionClient.abortConfig(); - allOpenedTransactions.remove(readTx); - readTx = null; - } - - public synchronized void abortTestTransaction(final ObjectName testTx) { - LOG.debug("Aborting transaction {}", testTx); - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(testTx); - allOpenedTransactions.remove(testTx); - transactionClient.abortConfig(); - } - - public void validateTransaction() throws ValidationException { - Optional taON = getTransaction(); - Preconditions.checkState(taON.isPresent(), NO_TRANSACTION_FOUND_FOR_SESSION + sessionIdForReporting); - - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(taON.get()); - transactionClient.validateConfig(); - } - - public void validateTestTransaction(final ObjectName taON) throws ValidationException { - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(taON); - transactionClient.validateConfig(); - } - - public void wipeTestTransaction(final ObjectName taON) { - wipeInternal(taON, true); - } - - /** - * Wiping means removing all module instances keeping the transaction open + - * service references. - */ - synchronized void wipeInternal(final ObjectName taON, final boolean isTest) { - ConfigTransactionClient transactionClient = configRegistryClient.getConfigTransactionClient(taON); - - Set lookupConfigBeans = transactionClient.lookupConfigBeans(); - int index = lookupConfigBeans.size(); - for (ObjectName instance : lookupConfigBeans) { - try { - transactionClient.destroyModule(instance); - } catch (final InstanceNotFoundException e) { - if (isTest) { - LOG.debug("Unable to clean configuration in transactiom {}", taON, e); - } else { - LOG.warn("Unable to clean configuration in transactiom {}", taON, e); - } - - throw new IllegalStateException("Unable to clean configuration in transactiom " + taON, e); - } - } - LOG.debug("Transaction {} wiped clean of {} config beans", taON, index); - - transactionClient.removeAllServiceReferences(); - LOG.debug("Transaction {} wiped clean of all service references", taON); - } - - public void wipeTransaction() { - Optional taON = getTransaction(); - Preconditions.checkState(taON.isPresent(), NO_TRANSACTION_FOUND_FOR_SESSION + sessionIdForReporting); - wipeInternal(taON.get(), false); - } -}