0fadec30cebc8e6001cba7c13dc753f4311171d7
[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.onap.ccsdk.cds.controllerblueprints.core.asJsonString
30 import org.springframework.core.io.Resource
31 import org.springframework.data.domain.Page
32 import org.springframework.data.domain.PageRequest
33 import org.springframework.data.domain.Sort
34 import org.springframework.http.HttpStatus
35 import org.springframework.http.MediaType
36 import org.springframework.http.ResponseEntity
37 import org.springframework.http.codec.multipart.FilePart
38 import org.springframework.security.access.prepost.PreAuthorize
39 import org.springframework.web.bind.annotation.DeleteMapping
40 import org.springframework.web.bind.annotation.GetMapping
41 import org.springframework.web.bind.annotation.PathVariable
42 import org.springframework.web.bind.annotation.PostMapping
43 import org.springframework.web.bind.annotation.RequestBody
44 import org.springframework.web.bind.annotation.RequestMapping
45 import org.springframework.web.bind.annotation.RequestParam
46 import org.springframework.web.bind.annotation.RequestPart
47 import org.springframework.web.bind.annotation.ResponseBody
48 import org.springframework.web.bind.annotation.RestController
49
50 /**
51  * BlueprintModelController Purpose: Handle controllerBlueprint API request
52  *
53  * @author Vinal Patel
54  * @version 1.0
55  */
56 @RestController
57 @RequestMapping("/api/v1/blueprint-model")
58 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
59
60     @PostMapping(
61         path = arrayOf("/bootstrap"), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE),
62         consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
63     )
64     @ResponseBody
65     @Throws(BluePrintException::class)
66     @PreAuthorize("hasRole('USER')")
67     suspend fun bootstrap(@RequestBody bootstrapRequest: BootstrapRequest): Unit = mdcWebCoroutineScope {
68         bluePrintModelHandler.bootstrapBlueprint(bootstrapRequest)
69     }
70
71     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
72     @ResponseBody
73     @Throws(BluePrintException::class)
74     @PreAuthorize("hasRole('USER')")
75     suspend fun saveBlueprint(@RequestPart("file") filePart: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
76         bluePrintModelHandler.saveBlueprintModel(filePart)
77     }
78
79     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
80     @ResponseBody
81     @PreAuthorize("hasRole('USER')")
82     fun allBlueprintModel(): List<BlueprintModelSearch> {
83         return this.bluePrintModelHandler.allBlueprintModel()
84     }
85
86     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
87     @ResponseBody
88     @PreAuthorize("hasRole('USER')")
89     fun allBlueprintModel(
90         @RequestParam(defaultValue = "20") limit: Int,
91         @RequestParam(defaultValue = "0") offset: Int,
92         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption,
93         @RequestParam(defaultValue = "ASC") sortType: String
94     ): Page<BlueprintModelSearch> {
95         val pageRequest = PageRequest.of(
96             offset, limit,
97             Sort.Direction.fromString(sortType), sort.columnName
98         )
99         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
100     }
101
102     @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
103     @ResponseBody
104     @PreAuthorize("hasRole('USER')")
105     suspend fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> =
106         mdcWebCoroutineScope {
107             bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
108         }
109
110     @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
111     @ResponseBody
112     @PreAuthorize("hasRole('USER')")
113     fun allBlueprintModelMetaDataPaged(
114         @NotNull @PathVariable(value = "keyword") keyWord: String,
115         @RequestParam(defaultValue = "20") limit: Int,
116         @RequestParam(defaultValue = "0") offset: Int,
117         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption,
118         @RequestParam(defaultValue = "ASC") sortType: String
119     ): Page<BlueprintModelSearch> {
120         val pageRequest = PageRequest.of(
121             offset, limit,
122             Sort.Direction.fromString(sortType), sort.columnName
123         )
124         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWordPaged(keyWord, pageRequest)
125     }
126
127     @DeleteMapping("/{id}")
128     @Throws(BluePrintException::class)
129     @PreAuthorize("hasRole('USER')")
130     suspend fun deleteBlueprint(@PathVariable(value = "id") id: String) = mdcWebCoroutineScope {
131         bluePrintModelHandler.deleteBlueprintModel(id)
132     }
133
134     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
135     @ResponseBody
136     @Throws(BluePrintException::class)
137     @PreAuthorize("hasRole('USER')")
138     suspend fun getBlueprintByNameAndVersion(
139         @PathVariable(value = "name") name: String,
140         @PathVariable(value = "version") version: String
141     ): ResponseEntity<BlueprintModelSearch> = mdcWebCoroutineScope {
142         val bluePrintModel: BlueprintModelSearch? =
143             bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
144         if (bluePrintModel != null)
145             ResponseEntity(bluePrintModel, HttpStatus.OK)
146         else
147             ResponseEntity(HttpStatus.NO_CONTENT)
148     }
149
150     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
151     @ResponseBody
152     @Throws(BluePrintException::class)
153     @PreAuthorize("hasRole('USER')")
154     suspend fun downloadBlueprintByNameAndVersion(
155         @PathVariable(value = "name") name: String,
156         @PathVariable(value = "version") version: String
157     ): ResponseEntity<Resource> = mdcWebCoroutineScope {
158         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
159     }
160
161     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
162     @ResponseBody
163     @Throws(BluePrintException::class)
164     @PreAuthorize("hasRole('USER')")
165     suspend fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch = mdcWebCoroutineScope {
166         bluePrintModelHandler.getBlueprintModelSearch(id)
167     }
168
169     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
170     @ResponseBody
171     @Throws(BluePrintException::class)
172     @PreAuthorize("hasRole('USER')")
173     suspend fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> =
174         mdcWebCoroutineScope {
175             bluePrintModelHandler.downloadBlueprintModelFile(id)
176         }
177
178     @PostMapping(
179         "/enrich", produces = [MediaType.APPLICATION_JSON_VALUE],
180         consumes = [
181             MediaType
182                 .MULTIPART_FORM_DATA_VALUE
183         ]
184     )
185     @ResponseBody
186     @Throws(BluePrintException::class)
187     @PreAuthorize("hasRole('USER')")
188     suspend fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity<Resource> = mdcWebCoroutineScope {
189         bluePrintModelHandler.enrichBlueprint(file)
190     }
191
192     @PostMapping(
193         "/enrichandpublish", produces = [MediaType.APPLICATION_JSON_VALUE],
194         consumes = [
195             MediaType
196                 .MULTIPART_FORM_DATA_VALUE
197         ]
198     )
199     @ResponseBody
200     @Throws(BluePrintException::class)
201     @PreAuthorize("hasRole('USER')")
202     suspend fun enrichAndPubishlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
203         bluePrintModelHandler.enrichAndPublishBlueprint(file)
204     }
205
206     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
207     @ResponseBody
208     @Throws(BluePrintException::class)
209     @PreAuthorize("hasRole('USER')")
210     suspend fun publishBlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
211         bluePrintModelHandler.publishBlueprint(file)
212     }
213
214     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
215     @ResponseBody
216     @PreAuthorize("hasRole('USER')")
217     suspend fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> =
218         mdcWebCoroutineScope {
219             bluePrintModelHandler.searchBlueprintModels(tags)
220         }
221
222     @DeleteMapping("/name/{name}/version/{version}")
223     @ApiOperation(
224         value = "Delete a CBA",
225         notes = "Delete the CBA package identified by its name and version.",
226         nickname = "BlueprintModelController_deleteBlueprint_1_DELETE.org.onap.ccsdk.cds.blueprintsprocessor.designer.api",
227         produces = MediaType.APPLICATION_JSON_VALUE
228     )
229     @PreAuthorize("hasRole('USER')")
230     suspend fun deleteBlueprint(
231         @ApiParam(value = "Name of the CBA.", required = true)
232         @PathVariable(value = "name") name: String,
233         @ApiParam(value = "Version of the CBA.", required = true)
234         @PathVariable(value = "version") version: String
235     ) = mdcWebCoroutineScope {
236         bluePrintModelHandler.deleteBlueprintModel(name, version)
237     }
238
239     @PostMapping(
240         path = arrayOf("/workflow-spec"),
241         produces = arrayOf(
242             MediaType
243                 .APPLICATION_JSON_VALUE
244         ),
245         consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
246     )
247     @ResponseBody
248     @Throws(BluePrintException::class)
249     @PreAuthorize("hasRole('USER')")
250     suspend fun workflowSpec(@RequestBody workFlowSpecReq: WorkFlowSpecRequest):
251         ResponseEntity<String> = mdcWebCoroutineScope {
252             var json = bluePrintModelHandler.prepareWorkFlowSpec(workFlowSpecReq)
253                 .asJsonString()
254             ResponseEntity(json, HttpStatus.OK)
255         }
256
257     @GetMapping(
258         path = arrayOf(
259             "/workflows/blueprint-name/{name}/version/{version" +
260                 "}"
261         ),
262         produces = arrayOf(MediaType.APPLICATION_JSON_VALUE)
263     )
264     @ResponseBody
265     @Throws(BluePrintException::class)
266     @PreAuthorize("hasRole('USER')")
267     suspend fun getWorkflowList(
268         @ApiParam(value = "Name of the CBA.", required = true)
269         @PathVariable(value = "name") name: String,
270         @ApiParam(value = "Version of the CBA.", required = true)
271         @PathVariable(value = "version") version: String
272     ): ResponseEntity<String> = mdcWebCoroutineScope {
273         var json = bluePrintModelHandler.getWorkflowNames(name, version)
274             .asJsonString()
275         ResponseEntity(json, HttpStatus.OK)
276     }
277 }