Adding some minor features
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / ExecutionServiceController.kt
index 7628da2..7c0672f 100644 (file)
@@ -24,19 +24,24 @@ import io.swagger.annotations.ApiParam
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.optionalClusterService
 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.mdcWebCoroutineScope
 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.determineHttpStatusCode
-import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.logger
+import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.http.MediaType
 import org.springframework.http.ResponseEntity
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
 import org.springframework.security.access.prepost.PreAuthorize
 import org.springframework.web.bind.annotation.RequestBody
 import org.springframework.web.bind.annotation.RequestMapping
 import org.springframework.web.bind.annotation.RequestMethod
+import org.springframework.web.bind.annotation.RequestParam
 import org.springframework.web.bind.annotation.ResponseBody
 import org.springframework.web.bind.annotation.RestController
 import java.util.concurrent.Phaser
@@ -45,8 +50,8 @@ import javax.annotation.PreDestroy
 @RestController
 @RequestMapping("/api/v1/execution-service")
 @Api(
-    value = "/api/v1/execution-service",
-    description = "Interaction with CBA."
+    value = "Execution Service Catalog",
+    description = "Interaction with CBA which are available in CDS"
 )
 open class ExecutionServiceController {
 
@@ -57,6 +62,9 @@ open class ExecutionServiceController {
     @Autowired
     lateinit var executionServiceHandler: ExecutionServiceHandler
 
+    @Autowired
+    lateinit var primaryEntityManager: LocalContainerEntityManagerFactoryBean
+
     @RequestMapping(
         path = ["/health-check"],
         method = [RequestMethod.GET],
@@ -64,8 +72,24 @@ open class ExecutionServiceController {
     )
     @ResponseBody
     @ApiOperation(value = "Health Check", hidden = true)
-    suspend fun executionServiceControllerHealthCheck(): JsonNode = mdcWebCoroutineScope {
-        "Success".asJsonPrimitive()
+    suspend fun executionServiceControllerHealthCheck(
+        @RequestParam(required = false, defaultValue = "false") checkDependencies: Boolean
+    ): ResponseEntity<JsonNode> = mdcWebCoroutineScope {
+        var body = mutableMapOf("success" to true)
+        var statusCode = 200
+        if (
+            (
+                BluePrintConstants.CLUSTER_ENABLED &&
+                    BluePrintDependencyService.optionalClusterService()?.clusterJoined() != true
+                ) ||
+            (
+                checkDependencies && !primaryEntityManager.dataSource.connection.isValid(1)
+                )
+        ) {
+            statusCode = 503
+            body.remove("success")
+        }
+        ResponseEntity.status(statusCode).body(body.asJsonType())
     }
 
     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
@@ -80,8 +104,7 @@ open class ExecutionServiceController {
     suspend fun process(
         @ApiParam(value = "ExecutionServiceInput payload.", required = true)
         @RequestBody executionServiceInput: ExecutionServiceInput
-    ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope {
-
+    ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope(executionServiceInput) {
         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
             throw httpProcessorException(
                 ErrorCatalogCodes.GENERIC_FAILURE,