4d13486c3828ba6d68b73ec03b421ce8e02b67d1
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / 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.blueprintsprocessor.designer.api
19
20 import io.swagger.annotations.ApiOperation
21 import io.swagger.annotations.ApiParam
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.BluePrintModelHandler
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.springframework.core.io.Resource
27 import org.springframework.http.MediaType
28 import org.springframework.http.ResponseEntity
29 import org.springframework.http.codec.multipart.FilePart
30 import org.springframework.security.access.prepost.PreAuthorize
31 import org.springframework.web.bind.annotation.*
32
33 /**
34  * BlueprintModelController Purpose: Handle controllerBlueprint API request
35  *
36  * @author Vinal Patel
37  * @version 1.0
38  */
39 @RestController
40 @RequestMapping("/api/v1/blueprint-model")
41 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
42
43     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
44     @ResponseBody
45     @Throws(BluePrintException::class)
46     @PreAuthorize("hasRole('USER')")
47     fun saveBlueprint(@RequestPart("file") filePart: FilePart): BlueprintModelSearch = runBlocking {
48         bluePrintModelHandler.saveBlueprintModel(filePart)
49     }
50
51     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
52     @ResponseBody
53     @PreAuthorize("hasRole('USER')")
54     fun allBlueprintModel(): List<BlueprintModelSearch> {
55         return this.bluePrintModelHandler.allBlueprintModel()
56     }
57
58     @DeleteMapping("/{id}")
59     @Throws(BluePrintException::class)
60     @PreAuthorize("hasRole('USER')")
61     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
62         this.bluePrintModelHandler.deleteBlueprintModel(id)
63     }
64
65     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
66     @ResponseBody
67     @Throws(BluePrintException::class)
68     @PreAuthorize("hasRole('USER')")
69     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
70                                      @PathVariable(value = "version") version: String): BlueprintModelSearch {
71         return this.bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
72     }
73
74     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
75     @ResponseBody
76     @Throws(BluePrintException::class)
77     @PreAuthorize("hasRole('USER')")
78     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
79                                           @PathVariable(value = "version") version: String): ResponseEntity<Resource> {
80         return this.bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
81     }
82
83     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
84     @ResponseBody
85     @Throws(BluePrintException::class)
86     @PreAuthorize("hasRole('USER')")
87     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
88         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
89     }
90
91     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
92     @ResponseBody
93     @Throws(BluePrintException::class)
94     @PreAuthorize("hasRole('USER')")
95     fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> {
96         return this.bluePrintModelHandler.downloadBlueprintModelFile(id)
97     }
98
99     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
100             .MULTIPART_FORM_DATA_VALUE])
101     @ResponseBody
102     @Throws(BluePrintException::class)
103     @PreAuthorize("hasRole('USER')")
104     fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity<Resource> = runBlocking {
105         bluePrintModelHandler.enrichBlueprint(file)
106     }
107
108     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
109     @ResponseBody
110     @Throws(BluePrintException::class)
111     @PreAuthorize("hasRole('USER')")
112     fun publishBlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = runBlocking {
113         bluePrintModelHandler.publishBlueprint(file)
114     }
115
116     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
117     @ResponseBody
118     @PreAuthorize("hasRole('USER')")
119     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
120         return this.bluePrintModelHandler.searchBlueprintModels(tags)
121     }
122
123     @DeleteMapping("/name/{name}/version/{version}")
124     @ApiOperation(value = "Delete a CBA",
125             notes = "Delete the CBA package identified by its name and version.",
126             produces = MediaType.APPLICATION_JSON_VALUE)
127     @PreAuthorize("hasRole('USER')")
128     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
129                         @PathVariable(value = "name") name: String,
130                         @ApiParam(value = "Version of the CBA.", required = true)
131                         @PathVariable(value = "version") version: String) = runBlocking {
132         bluePrintModelHandler.deleteBlueprintModel(name, version)
133     }
134 }