Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / controller / BlueprintModelController.kt
1 /*
2  * Copyright © 2019 Bell Canada 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.controllerblueprints.service.controller
19
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
23 import org.onap.ccsdk.cds.controllerblueprints.service.handler.BluePrintModelHandler
24 import org.springframework.core.io.Resource
25 import org.springframework.http.MediaType
26 import org.springframework.http.ResponseEntity
27 import org.springframework.http.codec.multipart.FilePart
28 import org.springframework.web.bind.annotation.*
29 import reactor.core.publisher.Mono
30
31 /**
32  * BlueprintModelController Purpose: Handle controllerBlueprint API request
33  *
34  * @author Vinal Patel
35  * @version 1.0
36  */
37 @RestController
38 @RequestMapping("/api/v1/blueprint-model")
39 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
40
41     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
42     @ResponseBody
43     @Throws(BluePrintException::class)
44     fun saveBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> {
45         return bluePrintModelHandler.saveBlueprintModel(file)
46     }
47
48     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
49     @ResponseBody
50     fun allBlueprintModel(): List<BlueprintModelSearch> {
51         return this.bluePrintModelHandler.allBlueprintModel()
52     }
53
54     @DeleteMapping("/{id}")
55     @Throws(BluePrintException::class)
56     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
57         this.bluePrintModelHandler.deleteBlueprintModel(id)
58     }
59
60     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
61     @ResponseBody
62     @Throws(BluePrintException::class)
63     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
64                                      @PathVariable(value = "version") version: String): BlueprintModelSearch {
65         return this.bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
66     }
67
68     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
69     @ResponseBody
70     @Throws(BluePrintException::class)
71     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
72                                           @PathVariable(value = "version") version: String): ResponseEntity<Resource> {
73         return this.bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
74     }
75
76     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
77     @ResponseBody
78     @Throws(BluePrintException::class)
79     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
80         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
81     }
82
83     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
84     @ResponseBody
85     @Throws(BluePrintException::class)
86     fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> {
87         return this.bluePrintModelHandler.downloadBlueprintModelFile(id)
88     }
89
90     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
91             .MULTIPART_FORM_DATA_VALUE])
92     @ResponseBody
93     @Throws(BluePrintException::class)
94     fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity<Resource> = runBlocking {
95         bluePrintModelHandler.enrichBlueprint(file)
96     }
97
98     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
99     @ResponseBody
100     @Throws(BluePrintException::class)
101     fun publishBlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = runBlocking {
102         bluePrintModelHandler.publishBlueprint(file)
103     }
104
105     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
106     @ResponseBody
107     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
108         return this.bluePrintModelHandler.searchBlueprintModels(tags)
109     }
110 }