cc122962321767aaf788e6c7ed6fa4682c4dfcde
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
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 kotlinx.coroutines.runBlocking
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
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.asJsonType
31 import org.onap.ccsdk.cds.controllerblueprints.core.logger
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.http.MediaType
34 import org.springframework.http.ResponseEntity
35 import org.springframework.http.codec.multipart.FilePart
36 import org.springframework.security.access.prepost.PreAuthorize
37 import org.springframework.web.bind.annotation.*
38 import java.util.concurrent.Phaser
39 import javax.annotation.PreDestroy
40
41 @RestController
42 @RequestMapping("/api/v1/execution-service")
43 @Api(value = "/api/v1/execution-service",
44         description = "Interaction with CBA.")
45 open class ExecutionServiceController {
46
47     private val log = logger(ExecutionServiceController::class)
48
49     private val ph = Phaser(1)
50
51     @Autowired
52     lateinit var executionServiceHandler: ExecutionServiceHandler
53
54     @RequestMapping(path = ["/health-check"],
55             method = [RequestMethod.GET],
56             produces = [MediaType.APPLICATION_JSON_VALUE])
57     @ResponseBody
58     @ApiOperation(value = "Health Check", hidden = true)
59     fun executionServiceControllerHealthCheck(): JsonNode = runBlocking {
60         "Success".asJsonPrimitive()
61     }
62
63     @PostMapping(path = ["/upload"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
64     @ResponseBody
65     @PreAuthorize("hasRole('USER')")
66     @ApiOperation(value = "Upload a CBA",
67             notes = "Upload the CBA package. This will also run validation on the CBA.",
68             produces = MediaType.APPLICATION_JSON_VALUE)
69     fun upload(@ApiParam(value = "The ZIP file containing the overall CBA package.", required = true)
70                @RequestPart("file") filePart: FilePart): JsonNode = runBlocking {
71         val uploadId = executionServiceHandler.upload(filePart)
72         """{"upload-id" : "$uploadId"}""".asJsonType()
73     }
74
75     @DeleteMapping("/name/{name}/version/{version}")
76     @ApiOperation(value = "Delete a CBA",
77             notes = "Delete the CBA package identified by its name and version.",
78             produces = MediaType.APPLICATION_JSON_VALUE)
79     @PreAuthorize("hasRole('USER')")
80     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
81                         @PathVariable(value = "name") name: String,
82                         @ApiParam(value = "Version of the CBA.", required = true)
83                         @PathVariable(value = "version") version: String) = runBlocking {
84         executionServiceHandler.remove(name, version)
85     }
86
87     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
88     @ApiOperation(value = "Execute a CBA workflow (action)",
89             notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
90             produces = MediaType.APPLICATION_JSON_VALUE,
91             response = ExecutionServiceOutput::class)
92     @ResponseBody
93     @PreAuthorize("hasRole('USER')")
94     fun process(@ApiParam(value = "ExecutionServiceInput payload.", required = true)
95                 @RequestBody executionServiceInput: ExecutionServiceInput): ResponseEntity<ExecutionServiceOutput> =
96             runBlocking {
97                 if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
98                     throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
99                 }
100
101                 ph.register()
102                 val processResult = executionServiceHandler.doProcess(executionServiceInput)
103                 ph.arriveAndDeregister()
104                 ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
105             }
106
107     @PreDestroy
108     fun preDestroy() {
109         val name = "ExecutionServiceController"
110         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
111         ph.arriveAndAwaitAdvance()
112         log.info("Done waiting in $name")
113     }
114 }