+
+ private void recreateTxChain() {
+ txChainFactory.close();
+ createTxChain(dataBroker);
+ synchronized (txLock) {
+ wTx = null;
+ }
+ }
+
+ private WriteTransaction getTransactionSafely() {
+ if (wTx == null && !TransactionChainManagerStatus.SHUTTING_DOWN.equals(transactionChainManagerStatus)) {
+ synchronized (txLock) {
+ if (wTx == null) {
+ wTx = txChainFactory.newWriteOnlyTransaction();
+ }
+ }
+ }
+ return wTx;
+ }
+
+ @VisibleForTesting
+ void enableSubmit() {
+ submitIsEnabled = true;
+ }
+
+ @Override
+ public void close() {
+ LOG.debug("Removing node {} from operational DS.", nodeII);
+ synchronized (txLock) {
+ final WriteTransaction writeTx = getTransactionSafely();
+ this.transactionChainManagerStatus = TransactionChainManagerStatus.SHUTTING_DOWN;
+ writeTx.delete(LogicalDatastoreType.OPERATIONAL, nodeII);
+ LOG.debug("Delete node {} from operational DS put to write transaction.", nodeII);
+ CheckedFuture<Void, TransactionCommitFailedException> submitsFuture = writeTx.submit();
+ LOG.debug("Delete node {} from operational DS write transaction submitted.", nodeII);
+ Futures.addCallback(submitsFuture, new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(final Void aVoid) {
+ LOG.debug("Removing node {} from operational DS successful .", nodeII);
+ notifyReadyForNewTransactionChainAndCloseFactory();
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.info("Attempt to close transaction chain factory failed.", throwable);
+ notifyReadyForNewTransactionChainAndCloseFactory();
+ }
+ });
+ wTx = null;
+ }
+ }
+
+ private void notifyReadyForNewTransactionChainAndCloseFactory() {
+ synchronized (this) {
+ try {
+ LOG.debug("Closing registration in manager.");
+ managerRegistration.close();
+ } catch (Exception e) {
+ LOG.warn("Failed to close transaction chain manager's registration.", e);
+ }
+ managerRegistration = null;
+ if (null != readyForNewTransactionChainHandler) {
+ readyForNewTransactionChainHandler.onReadyForNewTransactionChain();
+ }
+ }
+ txChainFactory.close();
+ LOG.debug("Transaction chain factory closed.");
+ }
+
+ public enum TransactionChainManagerStatus {
+ WORKING, SHUTTING_DOWN;
+ }
+