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