Speed up tests on policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / ApiActivator.java
index 2173ef3..d50ff40 100644 (file)
@@ -1,6 +1,9 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ * ONAP Policy API
+ * ================================================================================
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.api.main.startstop;
 
-import org.onap.policy.api.main.rest.ApiRestServer;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.common.parameters.ParameterService;
-import org.onap.policy.api.main.PolicyApiException;
+import org.onap.policy.api.main.exception.PolicyApiException;
 import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.api.main.rest.ApiRestController;
+import org.onap.policy.api.main.rest.LegacyApiRestController;
+import org.onap.policy.api.main.rest.aaf.AafApiFilter;
+import org.onap.policy.common.endpoints.http.server.RestServer;
+import org.onap.policy.common.parameters.ParameterService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * This class wraps a distributor so that it can be activated as a complete service together with all its api
- * and forwarding handlers.
+ * This class wraps a distributor so that it can be activated as a complete service together with all its api and
+ * forwarding handlers.
  */
 public class ApiActivator {
-    // The logger for this class
-    private static final Logger LOGGER = FlexLogger.getLogger(ApiActivator.class);
 
-    // The parameters of this policy api activator
+    private static final Logger LOGGER = LoggerFactory.getLogger(ApiActivator.class);
+
     private final ApiParameterGroup apiParameterGroup;
 
     private static boolean alive = false;
 
-    private ApiRestServer restServer;
+    private RestServer restServer;
 
     /**
      * Instantiate the activator for policy api as a complete service.
@@ -56,7 +61,6 @@ public class ApiActivator {
      *
      * @throws PolicyApiException on errors in initializing the service
      */
-    @SuppressWarnings("unchecked")
     public void initialize() throws PolicyApiException {
         LOGGER.debug("Policy api starting as a service . . .");
         startApiRestServer();
@@ -72,10 +76,10 @@ public class ApiActivator {
      */
     private void startApiRestServer() throws PolicyApiException {
         apiParameterGroup.getRestServerParameters().setName(apiParameterGroup.getName());
-        restServer = new ApiRestServer(apiParameterGroup.getRestServerParameters());
+        restServer = new RestServer(apiParameterGroup.getRestServerParameters(), AafApiFilter.class,
+                LegacyApiRestController.class, ApiRestController.class);
         if (!restServer.start()) {
-            throw new PolicyApiException(
-                    "Failed to start api rest server. Check log for more details...");
+            throw new PolicyApiException("Failed to start api rest server. Check log for more details...");
         }
     }
 
@@ -87,13 +91,17 @@ public class ApiActivator {
     public void terminate() throws PolicyApiException {
         try {
             deregisterToParameterService(apiParameterGroup);
-            ApiActivator.setAlive(false);
+
+            if (ApiActivator.isAlive()) {
+                ApiActivator.setAlive(false);
+            }
 
             // Stop the api rest server
-            restServer.stop();
+            if (restServer.isAlive()) {
+                restServer.stop();
+            }
         } catch (final Exception exp) {
-            LOGGER.error("Policy api service termination failed", exp);
-            throw new PolicyApiException(exp.getMessage(), exp);
+            throw new PolicyApiException("Policy api service termination failed", exp);
         }
     }
 
@@ -112,7 +120,7 @@ public class ApiActivator {
      * @param apiParameterGroup the api parameter group
      */
     public void registerToParameterService(final ApiParameterGroup apiParameterGroup) {
-        ParameterService.register(apiParameterGroup);
+        ParameterService.register(apiParameterGroup, true);
     }
 
     /**
@@ -121,7 +129,9 @@ public class ApiActivator {
      * @param apiParameterGroup the api parameter group
      */
     public void deregisterToParameterService(final ApiParameterGroup apiParameterGroup) {
-        ParameterService.deregister(apiParameterGroup.getName());
+        if (ParameterService.contains(apiParameterGroup.getName())) {
+            ParameterService.deregister(apiParameterGroup.getName());
+        }
     }
 
     /**
@@ -141,4 +151,4 @@ public class ApiActivator {
     public static void setAlive(final boolean status) {
         alive = status;
     }
-}
+}
\ No newline at end of file