Cleanup ParserFieldsParameter
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / handlers / TransactionChainHandler.java
index 4acea3aad78032369a5ff4fed20ac5f156f05ca9..28830dd3417e2ef9afb936ea7a9307b7bbe8b989 100644 (file)
@@ -5,68 +5,85 @@
  * 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.restconf.nb.rfc8040.handlers;
 
 import java.util.Objects;
-import javax.annotation.Nullable;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 /**
  * Implementation of {@link TransactionChainHandler}.
- *
  */
+@Singleton
 public class TransactionChainHandler implements Handler<DOMTransactionChain>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(TransactionChainHandler.class);
 
-    private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
+    private final DOMTransactionChainListener transactionChainListener = new DOMTransactionChainListener() {
         @Override
-        public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
-                final AsyncTransaction<?, ?> transaction, final Throwable cause) {
+        public void onTransactionChainFailed(final DOMTransactionChain chain, final DOMDataTreeTransaction transaction,
+                final Throwable cause) {
             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
-            reset();
+            transactionChainList.remove(chain);
             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
         }
 
         @Override
-        public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
+        public void onTransactionChainSuccessful(final DOMTransactionChain chain) {
             LOG.trace("TransactionChain({}) SUCCESSFUL", chain);
+            transactionChainList.remove(chain);
         }
     };
 
-    @Nullable
     private final DOMDataBroker dataBroker;
-    private volatile DOMTransactionChain transactionChain;
+    private final Queue<DOMTransactionChain> transactionChainList;
 
     /**
      * Prepare transaction chain service for Restconf services.
      */
+    @Inject
     public TransactionChainHandler(final DOMDataBroker dataBroker) {
         this.dataBroker = Objects.requireNonNull(dataBroker);
-        transactionChain = Objects.requireNonNull(dataBroker.createTransactionChain(transactionChainListener));
+        this.transactionChainList = new ConcurrentLinkedQueue<>();
     }
 
+    /**
+     * Create and return new instance of object {@link DOMTransactionChain}.
+     * After use, is important to close transactionChain by method {@link DOMTransactionChain#close()}.
+     * @return new instance of object {@link DOMTransactionChain}
+     */
     @Override
     public DOMTransactionChain get() {
-        return this.transactionChain;
-    }
-
-    public synchronized void reset() {
-        LOG.trace("Resetting TransactionChain({})", transactionChain);
-        transactionChain.close();
-        transactionChain = dataBroker.createTransactionChain(transactionChainListener);
+        final DOMTransactionChain transactionChain = dataBroker.createTransactionChain(transactionChainListener);
+        this.transactionChainList.add(transactionChain);
+        LOG.trace("Started TransactionChain({})", transactionChain);
+        return transactionChain;
     }
 
     @Override
+    @PreDestroy
     public synchronized void close() {
-        transactionChain.close();
+        for (DOMTransactionChain transactionChain: this.transactionChainList) {
+            transactionChain.close();
+            LOG.trace("Closed TransactionChain({})", transactionChain);
+        }
+    }
+
+    /**
+     * Verify if {@link DOMTransactionChain} exist in {@link TransactionChainHandler} queue.
+     * @param transactionChain object to check.
+     * @return true if object still exist in {@link TransactionChainHandler}.
+     */
+    boolean verifyIfExistTransactionChain(DOMTransactionChain transactionChain) {
+        return this.transactionChainList.contains(transactionChain);
     }
 }