Expose rest API with non blocking call.
[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.rest.service.mdcWebCoroutineScope
28 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.determineHttpStatusCode
29 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.http.MediaType
33 import org.springframework.http.ResponseEntity
34 import org.springframework.security.access.prepost.PreAuthorize
35 import org.springframework.web.bind.annotation.RequestBody
36 import org.springframework.web.bind.annotation.RequestMapping
37 import org.springframework.web.bind.annotation.RequestMethod
38 import org.springframework.web.bind.annotation.ResponseBody
39 import org.springframework.web.bind.annotation.RestController
40 import java.util.concurrent.Phaser
41 import javax.annotation.PreDestroy
42
43 @RestController
44 @RequestMapping("/api/v1/execution-service")
45 @Api(
46     value = "/api/v1/execution-service",
47     description = "Interaction with CBA."
48 )
49 open class ExecutionServiceController {
50
51     val log = logger(ExecutionServiceController::class)
52
53     private val ph = Phaser(1)
54
55     @Autowired
56     lateinit var executionServiceHandler: ExecutionServiceHandler
57
58     @RequestMapping(
59         path = ["/health-check"],
60         method = [RequestMethod.GET],
61         produces = [MediaType.APPLICATION_JSON_VALUE]
62     )
63     @ResponseBody
64     @ApiOperation(value = "Health Check", hidden = true)
65     suspend fun executionServiceControllerHealthCheck(): JsonNode = mdcWebCoroutineScope {
66         "Success".asJsonPrimitive()
67     }
68
69     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
70     @ApiOperation(
71         value = "Execute a CBA workflow (action)",
72         notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
73         produces = MediaType.APPLICATION_JSON_VALUE,
74         response = ExecutionServiceOutput::class
75     )
76     @ResponseBody
77     @PreAuthorize("hasRole('USER')")
78     suspend fun process(
79         @ApiParam(value = "ExecutionServiceInput payload.", required = true)
80         @RequestBody executionServiceInput: ExecutionServiceInput
81     ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope {
82
83         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
84             throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
85         }
86         ph.register()
87         val processResult = executionServiceHandler.doProcess(executionServiceInput)
88         ph.arriveAndDeregister()
89         ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
90     }
91
92     @PreDestroy
93     fun preDestroy() {
94         val name = "ExecutionServiceController"
95         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
96         ph.arriveAndAwaitAdvance()
97         log.info("Done waiting in $name")
98     }
99 }