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