bb824ce4df9544a757981a3307671a4fe37bd6b7
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2019 Orange.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
20
21 import io.swagger.annotations.ApiOperation
22 import io.swagger.annotations.ApiParam
23 import org.jetbrains.annotations.NotNull
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
25 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.BluePrintModelHandler
26 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BlueprintSortByOption
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.mdcWebCoroutineScope
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
29 import org.springframework.core.io.Resource
30 import org.springframework.data.domain.Page
31 import org.springframework.data.domain.PageRequest
32 import org.springframework.data.domain.Sort
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.MediaType
35 import org.springframework.http.ResponseEntity
36 import org.springframework.http.codec.multipart.FilePart
37 import org.springframework.security.access.prepost.PreAuthorize
38 import org.springframework.web.bind.annotation.DeleteMapping
39 import org.springframework.web.bind.annotation.GetMapping
40 import org.springframework.web.bind.annotation.PathVariable
41 import org.springframework.web.bind.annotation.PostMapping
42 import org.springframework.web.bind.annotation.RequestBody
43 import org.springframework.web.bind.annotation.RequestMapping
44 import org.springframework.web.bind.annotation.RequestParam
45 import org.springframework.web.bind.annotation.RequestPart
46 import org.springframework.web.bind.annotation.ResponseBody
47 import org.springframework.web.bind.annotation.RestController
48
49 /**
50  * BlueprintModelController Purpose: Handle controllerBlueprint API request
51  *
52  * @author Vinal Patel
53  * @version 1.0
54  */
55 @RestController
56 @RequestMapping("/api/v1/blueprint-model")
57 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
58
59     @PostMapping(
60         path = arrayOf("/bootstrap"), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE),
61         consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
62     )
63     @ResponseBody
64     @Throws(BluePrintException::class)
65     @PreAuthorize("hasRole('USER')")
66     suspend fun bootstrap(@RequestBody bootstrapRequest: BootstrapRequest): Unit = mdcWebCoroutineScope {
67         bluePrintModelHandler.bootstrapBlueprint(bootstrapRequest)
68     }
69
70     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
71     @ResponseBody
72     @Throws(BluePrintException::class)
73     @PreAuthorize("hasRole('USER')")
74     suspend fun saveBlueprint(@RequestPart("file") filePart: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
75         bluePrintModelHandler.saveBlueprintModel(filePart)
76     }
77
78     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
79     @ResponseBody
80     @PreAuthorize("hasRole('USER')")
81     fun allBlueprintModel(): List<BlueprintModelSearch> {
82         return this.bluePrintModelHandler.allBlueprintModel()
83     }
84
85     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
86     @ResponseBody
87     @PreAuthorize("hasRole('USER')")
88     fun allBlueprintModel(
89         @RequestParam(defaultValue = "20") limit: Int,
90         @RequestParam(defaultValue = "0") offset: Int,
91         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption
92     ): Page<BlueprintModelSearch> {
93         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
94         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
95     }
96
97     @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
98     @ResponseBody
99     @PreAuthorize("hasRole('USER')")
100     suspend fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> =
101         mdcWebCoroutineScope {
102             bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
103         }
104
105     @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
106     @ResponseBody
107     @PreAuthorize("hasRole('USER')")
108     fun allBlueprintModelMetaDataPaged(
109         @NotNull @PathVariable(value = "keyword") keyWord: String,
110         @RequestParam(defaultValue = "20") limit: Int,
111         @RequestParam(defaultValue = "0") offset: Int,
112         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption
113     ): Page<BlueprintModelSearch> {
114         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
115         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWordPaged(keyWord, pageRequest)
116     }
117
118     @DeleteMapping("/{id}")
119     @Throws(BluePrintException::class)
120     @PreAuthorize("hasRole('USER')")
121     suspend fun deleteBlueprint(@PathVariable(value = "id") id: String) = mdcWebCoroutineScope {
122         bluePrintModelHandler.deleteBlueprintModel(id)
123     }
124
125     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
126     @ResponseBody
127     @Throws(BluePrintException::class)
128     @PreAuthorize("hasRole('USER')")
129     suspend fun getBlueprintByNameAndVersion(
130         @PathVariable(value = "name") name: String,
131         @PathVariable(value = "version") version: String
132     ): ResponseEntity<BlueprintModelSearch> = mdcWebCoroutineScope {
133         val bluePrintModel: BlueprintModelSearch? =
134             bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
135         if (bluePrintModel != null)
136             ResponseEntity(bluePrintModel, HttpStatus.OK)
137         else
138             ResponseEntity(HttpStatus.NO_CONTENT)
139     }
140
141     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
142     @ResponseBody
143     @Throws(BluePrintException::class)
144     @PreAuthorize("hasRole('USER')")
145     suspend fun downloadBlueprintByNameAndVersion(
146         @PathVariable(value = "name") name: String,
147         @PathVariable(value = "version") version: String
148     ): ResponseEntity<Resource> = mdcWebCoroutineScope {
149         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
150     }
151
152     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
153     @ResponseBody
154     @Throws(BluePrintException::class)
155     @PreAuthorize("hasRole('USER')")
156     suspend fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch = mdcWebCoroutineScope {
157         bluePrintModelHandler.getBlueprintModelSearch(id)
158     }
159
160     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
161     @ResponseBody
162     @Throws(BluePrintException::class)
163     @PreAuthorize("hasRole('USER')")
164     suspend fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> =
165         mdcWebCoroutineScope {
166             bluePrintModelHandler.downloadBlueprintModelFile(id)
167         }
168
169     @PostMapping(
170         "/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
171             .MULTIPART_FORM_DATA_VALUE]
172     )
173     @ResponseBody
174     @Throws(BluePrintException::class)
175     @PreAuthorize("hasRole('USER')")
176     suspend fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity<Resource> = mdcWebCoroutineScope {
177         bluePrintModelHandler.enrichBlueprint(file)
178     }
179
180     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
181     @ResponseBody
182     @Throws(BluePrintException::class)
183     @PreAuthorize("hasRole('USER')")
184     suspend fun publishBlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
185         bluePrintModelHandler.publishBlueprint(file)
186     }
187
188     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
189     @ResponseBody
190     @PreAuthorize("hasRole('USER')")
191     suspend fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> =
192         mdcWebCoroutineScope {
193             bluePrintModelHandler.searchBlueprintModels(tags)
194         }
195
196     @DeleteMapping("/name/{name}/version/{version}")
197     @ApiOperation(
198         value = "Delete a CBA",
199         notes = "Delete the CBA package identified by its name and version.",
200         produces = MediaType.APPLICATION_JSON_VALUE
201     )
202     @PreAuthorize("hasRole('USER')")
203     suspend fun deleteBlueprint(
204         @ApiParam(value = "Name of the CBA.", required = true)
205         @PathVariable(value = "name") name: String,
206         @ApiParam(value = "Version of the CBA.", required = true)
207         @PathVariable(value = "version") version: String
208     ) = mdcWebCoroutineScope {
209         bluePrintModelHandler.deleteBlueprintModel(name, version)
210     }
211 }