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