16f0fa869049f8c5da96a534430e81ad568f2c09
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
18
19 import io.swagger.annotations.ApiOperation
20 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
21 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput
23 import org.springframework.beans.factory.annotation.Autowired
24 import org.springframework.http.MediaType
25 import org.springframework.http.codec.multipart.FilePart
26 import org.springframework.security.access.prepost.PreAuthorize
27 import org.springframework.web.bind.annotation.PostMapping
28 import org.springframework.web.bind.annotation.RequestBody
29 import org.springframework.web.bind.annotation.RequestMapping
30 import org.springframework.web.bind.annotation.RequestMethod
31 import org.springframework.web.bind.annotation.RequestPart
32 import org.springframework.web.bind.annotation.ResponseBody
33 import org.springframework.web.bind.annotation.RestController
34 import reactor.core.publisher.Mono
35
36 @RestController
37 @RequestMapping("/api/v1/execution-service")
38 open class ExecutionServiceController {
39
40     @Autowired
41     lateinit var executionServiceHandler: ExecutionServiceHandler
42
43     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
44     @ResponseBody
45     fun ping(): Mono<String> {
46         return Mono.just("Success")
47     }
48
49     @PostMapping(path = ["/upload"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
50     @ApiOperation(value = "Upload CBA", notes = "Takes a File and load it in the runtime database")
51     @ResponseBody
52     @PreAuthorize("hasRole('USER')")
53     fun upload(@RequestPart("file") parts: Mono<FilePart>): Mono<String> {
54         return parts
55             .filter { it is FilePart }
56             .ofType(FilePart::class.java)
57             .flatMap(executionServiceHandler::upload)
58     }
59
60     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
61     @ApiOperation(value = "Resolve Resource Mappings",
62         notes = "Takes the blueprint information and process as per the payload")
63     @ResponseBody
64     @PreAuthorize("hasRole('USER')")
65     fun process(@RequestBody executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
66         if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) {
67             throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.")
68         }
69         return executionServiceHandler.processSync(executionServiceInput)
70     }
71 }