Merge "Refractor rest log tracing filter for reuse."
[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 io.swagger.annotations.Api
21 import io.swagger.annotations.ApiOperation
22 import io.swagger.annotations.ApiParam
23 import kotlinx.coroutines.Dispatchers
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.monoMdc
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.*
36 import reactor.core.publisher.Mono
37 import java.util.concurrent.Phaser
38 import javax.annotation.PreDestroy
39
40 @RestController
41 @RequestMapping("/api/v1/execution-service")
42 @Api(value = "/api/v1/execution-service",
43         description = "Interaction with CBA.")
44 open class ExecutionServiceController {
45     val log = logger(ExecutionServiceController::class)
46
47     private val ph = Phaser(1)
48
49     @Autowired
50     lateinit var executionServiceHandler: ExecutionServiceHandler
51
52     @RequestMapping(path = ["/health-check"],
53             method = [RequestMethod.GET],
54             produces = [MediaType.APPLICATION_JSON_VALUE])
55     @ResponseBody
56     @ApiOperation(value = "Health Check", hidden = true)
57     fun executionServiceControllerHealthCheck() = monoMdc(Dispatchers.IO) {
58         "Success".asJsonPrimitive()
59     }
60
61     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
62     @ApiOperation(value = "Execute a CBA workflow (action)",
63             notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
64             produces = MediaType.APPLICATION_JSON_VALUE,
65             response = ExecutionServiceOutput::class)
66     @ResponseBody
67     @PreAuthorize("hasRole('USER')")
68     fun process(@ApiParam(value = "ExecutionServiceInput payload.", required = true)
69                 @RequestBody executionServiceInput: ExecutionServiceInput)
70             : Mono<ResponseEntity<ExecutionServiceOutput>> = monoMdc(Dispatchers.IO) {
71
72         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
73             throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
74         }
75
76         ph.register()
77         val processResult = executionServiceHandler.doProcess(executionServiceInput)
78         ph.arriveAndDeregister()
79         ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
80     }
81
82     @PreDestroy
83     fun preDestroy() {
84         val name = "ExecutionServiceController"
85         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
86         ph.arriveAndAwaitAdvance()
87         log.info("Done waiting in $name")
88     }
89 }
90