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
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM, Bell Canada.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import io.swagger.annotations.Api
22 import io.swagger.annotations.ApiOperation
23 import io.swagger.annotations.ApiParam
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.optionalClusterService
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.mdcWebCoroutineScope
29 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.determineHttpStatusCode
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
32 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
33 import org.onap.ccsdk.cds.controllerblueprints.core.logger
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
35 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.http.MediaType
38 import org.springframework.http.ResponseEntity
39 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
40 import org.springframework.security.access.prepost.PreAuthorize
41 import org.springframework.web.bind.annotation.RequestBody
42 import org.springframework.web.bind.annotation.RequestMapping
43 import org.springframework.web.bind.annotation.RequestMethod
44 import org.springframework.web.bind.annotation.RequestParam
45 import org.springframework.web.bind.annotation.ResponseBody
46 import org.springframework.web.bind.annotation.RestController
47 import java.util.concurrent.Phaser
48 import javax.annotation.PreDestroy
49
50 @RestController
51 @RequestMapping("/api/v1/execution-service")
52 @Api(
53     value = "Execution Service Catalog",
54     description = "Interaction with CBA which are available in CDS"
55 )
56 open class ExecutionServiceController {
57
58     val log = logger(ExecutionServiceController::class)
59
60     private val ph = Phaser(1)
61
62     @Autowired
63     lateinit var executionServiceHandler: ExecutionServiceHandler
64
65     @Autowired
66     lateinit var primaryEntityManager: LocalContainerEntityManagerFactoryBean
67
68     @RequestMapping(
69         path = ["/health-check"],
70         method = [RequestMethod.GET],
71         produces = [MediaType.APPLICATION_JSON_VALUE]
72     )
73     @ResponseBody
74     @ApiOperation(value = "Health Check", hidden = true)
75     suspend fun executionServiceControllerHealthCheck(
76         @RequestParam(required = false, defaultValue = "false") checkDependencies: Boolean
77     ): ResponseEntity<JsonNode> = mdcWebCoroutineScope {
78         var body = mutableMapOf("success" to true)
79         var statusCode = 200
80         if (
81             (
82                 BluePrintConstants.CLUSTER_ENABLED &&
83                     BluePrintDependencyService.optionalClusterService()?.clusterJoined() != true
84                 ) ||
85             (
86                 checkDependencies && !primaryEntityManager.dataSource.connection.isValid(1)
87                 )
88         ) {
89             statusCode = 503
90             body.remove("success")
91         }
92         ResponseEntity.status(statusCode).body(body.asJsonType())
93     }
94
95     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
96     @ApiOperation(
97         value = "Execute a CBA workflow (action)",
98         notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
99         produces = MediaType.APPLICATION_JSON_VALUE,
100         response = ExecutionServiceOutput::class
101     )
102     @ResponseBody
103     @PreAuthorize("hasRole('USER')")
104     suspend fun process(
105         @ApiParam(value = "ExecutionServiceInput payload.", required = true)
106         @RequestBody executionServiceInput: ExecutionServiceInput
107     ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope(executionServiceInput) {
108         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
109             throw httpProcessorException(
110                 ErrorCatalogCodes.GENERIC_FAILURE,
111                 SelfServiceApiDomains.BLUEPRINT_PROCESSOR,
112                 "Can't process async request through the REST endpoint. Use gRPC for async processing."
113             )
114         }
115         ph.register()
116         val processResult = executionServiceHandler.doProcess(executionServiceInput)
117         ph.arriveAndDeregister()
118         ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
119     }
120
121     @PreDestroy
122     fun preDestroy() {
123         val name = "ExecutionServiceController"
124         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
125         ph.arriveAndAwaitAdvance()
126         log.info("Done waiting in $name")
127     }
128 }