Add JSONRestconfService impl for the restconf Draft18 impl
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / RestConnectorProvider.java
index 5ce439ca73ccbd97c4f462bc2c6463c32836d5db..2aab6df3a243926742c4ca9499bfb30b6c7d878a 100644 (file)
@@ -5,26 +5,24 @@
  * 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;
-import java.util.Collection;
-import java.util.Collections;
 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.DOMMountPointService;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
 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.Draft11ServicesWrapperImpl;
+import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
+import org.opendaylight.restconf.handlers.NotificationServiceHandler;
 import org.opendaylight.restconf.handlers.RpcServiceHandler;
 import org.opendaylight.restconf.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.handlers.TransactionChainHandler;
@@ -34,19 +32,18 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Provider for restconf draft11.
+ * Provider for restconf draft18.
  *
  */
-public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
+public class RestConnectorProvider implements RestConnector, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
 
-    private final TransactionChainListener transactionListener = new TransactionChainListener() {
+    public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = 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);
         }
@@ -57,60 +54,92 @@ public class RestConnectorProvider implements Provider, RestConnector, AutoClose
         }
     };
 
+    private static TransactionChainHandler transactionChainHandler;
+    private static DOMDataBroker dataBroker;
+    private static DOMMountPointServiceHandler mountPointServiceHandler;
+
+    private final SchemaService schemaService;
+    private final DOMRpcService rpcService;
+    private final DOMNotificationService notificationService;
+    private final DOMMountPointService mountPointService;
     private ListenerRegistration<SchemaContextListener> listenerRegistration;
-    private DOMDataBroker dataBroker;
-    private DOMTransactionChain transactionChain;
 
-    @Override
-    public void onSessionInitiated(final ProviderSession session) {
-        final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
+    private SchemaContextHandler schemaCtxHandler;
 
-        final Draft11ServicesWrapperImpl wrapperServices = Draft11ServicesWrapperImpl.getInstance();
+    public RestConnectorProvider(DOMDataBroker domDataBroker, SchemaService schemaService, DOMRpcService rpcService,
+            DOMNotificationService notificationService, DOMMountPointService mountPointService) {
+        this.schemaService = Preconditions.checkNotNull(schemaService);
+        this.rpcService = Preconditions.checkNotNull(rpcService);
+        this.notificationService = Preconditions.checkNotNull(notificationService);
+        this.mountPointService = Preconditions.checkNotNull(mountPointService);
 
-        final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
-        this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
+        RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
+    }
 
-        final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
-                session.getService(DOMMountPointService.class));
+    public void start() {
+        final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
 
-        this.dataBroker = session.getService(DOMDataBroker.class);
-        final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(this.dataBroker);
+        mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
 
-        this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
-        final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
+        final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
+
+        RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
+                .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
+
+        this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
+        this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
 
-        final DOMRpcService rpcService = session.getService(DOMRpcService.class);
         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
 
-        wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
-                brokerHandler, rpcServiceHandler);
+        final NotificationServiceHandler notificationServiceHandler =
+                new NotificationServiceHandler(notificationService);
+
+        wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
+                RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
+                notificationServiceHandler);
+    }
+
+    public DOMMountPointServiceHandler getMountPointServiceHandler() {
+        return mountPointServiceHandler;
     }
 
     /**
-     * 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}
+     *             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(dataBroker).createTransactionChain(
+                        RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
+        );
     }
 
-    @Override
-    public Collection<ProviderFunctionality> getProviderFunctionality() {
-        return Collections.emptySet();
+    /**
+     * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
+     * @return {@link DOMMountPointService}
+     */
+    public static DOMMountPointService getMountPointService() {
+        return mountPointServiceHandler.get();
     }
 
     @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();
         }
+
+        transactionChainHandler = null;
+        mountPointServiceHandler = null;
+        dataBroker = null;
     }
 }