Fix close() in provider
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / RestConnectorProvider.java
index dedea9b0a6cd15d3edfdea6e08d9d8462da27f6d..cc14b7f332ad611bb0ea1bb96eefdeea4881ec3f 100644 (file)
@@ -5,6 +5,7 @@
  * 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;
 
 import com.google.common.base.Preconditions;
@@ -16,13 +17,12 @@ import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListen
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
 import org.opendaylight.controller.sal.core.api.Provider;
 import org.opendaylight.controller.sal.core.api.model.SchemaService;
 import org.opendaylight.netconf.sal.rest.api.RestConnector;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
-import org.opendaylight.restconf.common.wrapper.services.Draft15ServicesWrapperImpl;
+import org.opendaylight.restconf.common.wrapper.services.Draft16ServicesWrapperImpl;
 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
 import org.opendaylight.restconf.handlers.RpcServiceHandler;
@@ -34,19 +34,18 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Provider for restconf draft15.
+ * Provider for restconf draft16.
  *
  */
 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
 
-    private final TransactionChainListener transactionListener = new TransactionChainListener() {
+    public static final TransactionChainListener transactionListener = new TransactionChainListener() {
         @Override
         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
-            chain.close();
             resetTransactionChainForAdapaters(chain);
             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
         }
@@ -58,14 +57,14 @@ public class RestConnectorProvider implements Provider, RestConnector, AutoClose
     };
 
     private ListenerRegistration<SchemaContextListener> listenerRegistration;
-    private DOMDataBroker dataBroker;
-    private DOMTransactionChain transactionChain;
+    private static TransactionChainHandler transactionChainHandler;
+    private static DOMDataBroker dataBroker;
 
     @Override
     public void onSessionInitiated(final ProviderSession session) {
         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
 
-        final Draft15ServicesWrapperImpl wrapperServices = Draft15ServicesWrapperImpl.getInstance();
+        final Draft16ServicesWrapperImpl wrapperServices = Draft16ServicesWrapperImpl.getInstance();
 
         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
@@ -73,30 +72,32 @@ public class RestConnectorProvider implements Provider, RestConnector, AutoClose
         final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
                 session.getService(DOMMountPointService.class));
 
-        this.dataBroker = session.getService(DOMDataBroker.class);
-        final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(this.dataBroker);
+        RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
+        final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
 
-        this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
-        final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
+        RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
+                .createTransactionChain(RestConnectorProvider.transactionListener));
 
         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
 
-        wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
-                brokerHandler, rpcServiceHandler);
+        wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler,
+                RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler);
     }
 
     /**
-     * After {@link TransactionChain} failed, this is creating new transaction
-     * with listener.
+     * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
      *
      * @param chain
      *            - old {@link TransactionChain}
      */
-    private void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
-        LOG.trace("Resetting TransactionChain({}) to {}", chain, this.transactionChain);
-        this.transactionChain = Preconditions.checkNotNull(this.dataBroker)
-                .createTransactionChain(this.transactionListener);
+    public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
+        LOG.trace("Resetting TransactionChain({})", chain);
+        chain.close();
+        RestConnectorProvider.transactionChainHandler.update(
+                Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
+                        RestConnectorProvider.transactionListener)
+        );
     }
 
     @Override
@@ -106,11 +107,14 @@ public class RestConnectorProvider implements Provider, RestConnector, AutoClose
 
     @Override
     public void close() throws Exception {
+        // close registration
         if (this.listenerRegistration != null) {
             this.listenerRegistration.close();
         }
-        if (this.transactionChain != null) {
-            this.transactionChain.close();
+
+        // close transaction chain
+        if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
+            transactionChainHandler.get().close();
         }
     }
 }