Fix missing capability cli models.
[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.
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.springframework.beans.factory.annotation.Autowired
32 import org.springframework.http.MediaType
33 import org.springframework.http.ResponseEntity
34 import org.springframework.http.codec.multipart.FilePart
35 import org.springframework.security.access.prepost.PreAuthorize
36 import org.springframework.web.bind.annotation.*
37
38 @RestController
39 @RequestMapping("/api/v1/execution-service")
40 @Api(value = "/api/v1/execution-service",
41         description = "Interaction with CBA.")
42 open class ExecutionServiceController {
43
44     @Autowired
45     lateinit var executionServiceHandler: ExecutionServiceHandler
46
47     @RequestMapping(path = ["/health-check"],
48             method = [RequestMethod.GET],
49             produces = [MediaType.APPLICATION_JSON_VALUE])
50     @ResponseBody
51     @ApiOperation(value = "Health Check", hidden = true)
52     fun executionServiceControllerHealthCheck(): JsonNode = runBlocking {
53         "Success".asJsonPrimitive()
54     }
55
56     @PostMapping(path = ["/upload"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
57     @ResponseBody
58     @PreAuthorize("hasRole('USER')")
59     @ApiOperation(value = "Upload a CBA",
60             notes = "Upload the CBA package. This will also run validation on the CBA.",
61             produces = MediaType.APPLICATION_JSON_VALUE)
62     fun upload(@ApiParam(value = "The ZIP file containing the overall CBA package.", required = true)
63                @RequestPart("file") filePart: FilePart): JsonNode = runBlocking {
64         val uploadId = executionServiceHandler.upload(filePart)
65         """{"upload-id" : "$uploadId"}""".asJsonType()
66     }
67
68     @DeleteMapping("/name/{name}/version/{version}")
69     @ApiOperation(value = "Delete a CBA",
70             notes = "Delete the CBA package identified by its name and version.",
71             produces = MediaType.APPLICATION_JSON_VALUE)
72     @PreAuthorize("hasRole('USER')")
73     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
74                         @PathVariable(value = "name") name: String,
75                         @ApiParam(value = "Version of the CBA.", required = true)
76                         @PathVariable(value = "version") version: String) = runBlocking {
77         executionServiceHandler.remove(name, version)
78     }
79
80     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
81     @ApiOperation(value = "Execute a CBA workflow (action)",
82             notes = "Execute the appropriate CBA's action based on the ExecutionServiceInput object passed as input.",
83             produces = MediaType.APPLICATION_JSON_VALUE,
84             response = ExecutionServiceOutput::class)
85     @ResponseBody
86     @PreAuthorize("hasRole('USER')")
87     fun process(@ApiParam(value = "ExecutionServiceInput payload.", required = true)
88                 @RequestBody executionServiceInput: ExecutionServiceInput): ResponseEntity<ExecutionServiceOutput> =
89             runBlocking {
90                 if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
91                     throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
92                 }
93                 val processResult = executionServiceHandler.doProcess(executionServiceInput)
94                 ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
95             }
96 }