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.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
51 @RequestMapping("/api/v1/execution-service")
53 value = "Execution Service Catalog",
54 description = "Interaction with CBA which are available in CDS"
56 open class ExecutionServiceController {
58 val log = logger(ExecutionServiceController::class)
60 private val ph = Phaser(1)
63 lateinit var executionServiceHandler: ExecutionServiceHandler
66 lateinit var primaryEntityManager: LocalContainerEntityManagerFactoryBean
69 path = ["/health-check"],
70 method = [RequestMethod.GET],
71 produces = [MediaType.APPLICATION_JSON_VALUE]
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)
82 BluePrintConstants.CLUSTER_ENABLED &&
83 BluePrintDependencyService.optionalClusterService()?.clusterJoined() != true
86 checkDependencies && !primaryEntityManager.dataSource.connection.isValid(1)
90 body.remove("success")
92 ResponseEntity.status(statusCode).body(body.asJsonType())
95 @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
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
103 @PreAuthorize("hasRole('USER')")
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."
116 val processResult = executionServiceHandler.doProcess(executionServiceInput)
117 ph.arriveAndDeregister()
118 ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
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")