Revert "Renaming Files having BluePrint to have Blueprint"
[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.security.access.prepost.PreAuthorize
40 import org.springframework.web.bind.annotation.RequestBody
41 import org.springframework.web.bind.annotation.RequestMapping
42 import org.springframework.web.bind.annotation.RequestMethod
43 import org.springframework.web.bind.annotation.ResponseBody
44 import org.springframework.web.bind.annotation.RestController
45 import java.util.concurrent.Phaser
46 import javax.annotation.PreDestroy
47
48 @RestController
49 @RequestMapping("/api/v1/execution-service")
50 @Api(
51     value = "/api/v1/execution-service",
52     description = "Interaction with CBA."
53 )
54 open class ExecutionServiceController {
55
56     val log = logger(ExecutionServiceController::class)
57
58     private val ph = Phaser(1)
59
60     @Autowired
61     lateinit var executionServiceHandler: ExecutionServiceHandler
62
63     @RequestMapping(
64         path = ["/health-check"],
65         method = [RequestMethod.GET],
66         produces = [MediaType.APPLICATION_JSON_VALUE]
67     )
68     @ResponseBody
69     @ApiOperation(value = "Health Check", hidden = true)
70     suspend fun executionServiceControllerHealthCheck(): ResponseEntity<JsonNode> = mdcWebCoroutineScope {
71         var body = mutableMapOf("success" to true)
72         var statusCode = 200
73         if (BluePrintConstants.CLUSTER_ENABLED &&
74             BluePrintDependencyService.optionalClusterService()?.clusterJoined() != true
75         ) {
76             statusCode = 503
77             body.remove("success")
78         }
79         ResponseEntity.status(statusCode).body(body.asJsonType())
80     }
81
82     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
83     @ApiOperation(
84         value = "Execute a CBA workflow (action)",
85         notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
86         produces = MediaType.APPLICATION_JSON_VALUE,
87         response = ExecutionServiceOutput::class
88     )
89     @ResponseBody
90     @PreAuthorize("hasRole('USER')")
91     suspend fun process(
92         @ApiParam(value = "ExecutionServiceInput payload.", required = true)
93         @RequestBody executionServiceInput: ExecutionServiceInput
94     ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope {
95
96         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
97             throw httpProcessorException(
98                 ErrorCatalogCodes.GENERIC_FAILURE,
99                 SelfServiceApiDomains.BLUEPRINT_PROCESSOR,
100                 "Can't process async request through the REST endpoint. Use gRPC for async processing."
101             )
102         }
103         ph.register()
104         val processResult = executionServiceHandler.doProcess(executionServiceInput)
105         ph.arriveAndDeregister()
106         ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
107     }
108
109     @PreDestroy
110     fun preDestroy() {
111         val name = "ExecutionServiceController"
112         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
113         ph.arriveAndAwaitAdvance()
114         log.info("Done waiting in $name")
115     }
116 }