2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM, Bell Canada.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
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.httpProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.logger
32 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.http.MediaType
35 import org.springframework.http.ResponseEntity
36 import org.springframework.security.access.prepost.PreAuthorize
37 import org.springframework.web.bind.annotation.RequestBody
38 import org.springframework.web.bind.annotation.RequestMapping
39 import org.springframework.web.bind.annotation.RequestMethod
40 import org.springframework.web.bind.annotation.ResponseBody
41 import org.springframework.web.bind.annotation.RestController
42 import java.util.concurrent.Phaser
43 import javax.annotation.PreDestroy
46 @RequestMapping("/api/v1/execution-service")
48 value = "/api/v1/execution-service",
49 description = "Interaction with CBA."
51 open class ExecutionServiceController {
53 val log = logger(ExecutionServiceController::class)
55 private val ph = Phaser(1)
58 lateinit var executionServiceHandler: ExecutionServiceHandler
61 path = ["/health-check"],
62 method = [RequestMethod.GET],
63 produces = [MediaType.APPLICATION_JSON_VALUE]
66 @ApiOperation(value = "Health Check", hidden = true)
67 suspend fun executionServiceControllerHealthCheck(): JsonNode = mdcWebCoroutineScope {
68 "Success".asJsonPrimitive()
71 @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
73 value = "Execute a CBA workflow (action)",
74 notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
75 produces = MediaType.APPLICATION_JSON_VALUE,
76 response = ExecutionServiceOutput::class
79 @PreAuthorize("hasRole('USER')")
81 @ApiParam(value = "ExecutionServiceInput payload.", required = true)
82 @RequestBody executionServiceInput: ExecutionServiceInput
83 ): ResponseEntity<ExecutionServiceOutput> = mdcWebCoroutineScope {
85 if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
86 throw httpProcessorException(ErrorCatalogCodes.GENERIC_FAILURE,
87 SelfServiceApiDomains.BLUEPRINT_PROCESSOR,
88 "Can't process async request through the REST endpoint. Use gRPC for async processing.")
91 val processResult = executionServiceHandler.doProcess(executionServiceInput)
92 ph.arriveAndDeregister()
93 ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
98 val name = "ExecutionServiceController"
99 log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
100 ph.arriveAndAwaitAdvance()
101 log.info("Done waiting in $name")