eff977348b04d4700bc58859c315b28514c98b1b
[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 io.swagger.annotations.ApiOperation
21 import kotlinx.coroutines.runBlocking
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
25 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.determineHttpStatusCode
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.http.HttpStatus
29 import org.springframework.http.MediaType
30 import org.springframework.http.ResponseEntity
31 import org.springframework.http.codec.multipart.FilePart
32 import org.springframework.security.access.prepost.PreAuthorize
33 import org.springframework.web.bind.annotation.*
34
35 @RestController
36 @RequestMapping("/api/v1/execution-service")
37 open class ExecutionServiceController {
38
39     @Autowired
40     lateinit var executionServiceHandler: ExecutionServiceHandler
41
42     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
43     @ResponseBody
44     fun ping(): String = runBlocking {
45         "Success"
46     }
47
48     @PostMapping(path = ["/upload"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
49     @ApiOperation(value = "Upload CBA", notes = "Takes a File and load it in the runtime database")
50     @ResponseBody
51     @PreAuthorize("hasRole('USER')")
52     fun upload(@RequestPart("file") filePart: FilePart): String = runBlocking {
53         executionServiceHandler.upload(filePart)
54     }
55
56     @DeleteMapping("/name/{name}/version/{version}")
57     @Throws(BluePrintException::class)
58     @PreAuthorize("hasRole('USER')")
59     fun deleteBlueprint(@PathVariable(value = "name") name: String,
60                         @PathVariable(value = "version") version: String) = runBlocking {
61         executionServiceHandler.remove(name, version)
62     }
63
64     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
65     @ApiOperation(value = "Resolve Resource Mappings",
66             notes = "Takes the blueprint information and process as per the payload")
67     @ResponseBody
68     @PreAuthorize("hasRole('USER')")
69     fun process(@RequestBody executionServiceInput: ExecutionServiceInput): ResponseEntity<ExecutionServiceOutput> = runBlocking {
70         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
71             throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
72         }
73         val processResult = executionServiceHandler.doProcess(executionServiceInput)
74         ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code))
75     }
76 }