Merge "Truncate message published on Kafka / Spike: Define solution for logs separation"
[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  * 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     ): Page<BlueprintModelSearch> {
94         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
95         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
96     }
97
98     @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
99     @ResponseBody
100     @PreAuthorize("hasRole('USER')")
101     suspend fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> =
102         mdcWebCoroutineScope {
103             bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
104         }
105
106     @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
107     @ResponseBody
108     @PreAuthorize("hasRole('USER')")
109     fun allBlueprintModelMetaDataPaged(
110         @NotNull @PathVariable(value = "keyword") keyWord: String,
111         @RequestParam(defaultValue = "20") limit: Int,
112         @RequestParam(defaultValue = "0") offset: Int,
113         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption
114     ): Page<BlueprintModelSearch> {
115         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
116         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWordPaged(keyWord, pageRequest)
117     }
118
119     @DeleteMapping("/{id}")
120     @Throws(BluePrintException::class)
121     @PreAuthorize("hasRole('USER')")
122     suspend fun deleteBlueprint(@PathVariable(value = "id") id: String) = mdcWebCoroutineScope {
123         bluePrintModelHandler.deleteBlueprintModel(id)
124     }
125
126     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
127     @ResponseBody
128     @Throws(BluePrintException::class)
129     @PreAuthorize("hasRole('USER')")
130     suspend fun getBlueprintByNameAndVersion(
131         @PathVariable(value = "name") name: String,
132         @PathVariable(value = "version") version: String
133     ): ResponseEntity<BlueprintModelSearch> = mdcWebCoroutineScope {
134         val bluePrintModel: BlueprintModelSearch? =
135             bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
136         if (bluePrintModel != null)
137             ResponseEntity(bluePrintModel, HttpStatus.OK)
138         else
139             ResponseEntity(HttpStatus.NO_CONTENT)
140     }
141
142     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
143     @ResponseBody
144     @Throws(BluePrintException::class)
145     @PreAuthorize("hasRole('USER')")
146     suspend fun downloadBlueprintByNameAndVersion(
147         @PathVariable(value = "name") name: String,
148         @PathVariable(value = "version") version: String
149     ): ResponseEntity<Resource> = mdcWebCoroutineScope {
150         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
151     }
152
153     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
154     @ResponseBody
155     @Throws(BluePrintException::class)
156     @PreAuthorize("hasRole('USER')")
157     suspend fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch = mdcWebCoroutineScope {
158         bluePrintModelHandler.getBlueprintModelSearch(id)
159     }
160
161     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
162     @ResponseBody
163     @Throws(BluePrintException::class)
164     @PreAuthorize("hasRole('USER')")
165     suspend fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> =
166         mdcWebCoroutineScope {
167             bluePrintModelHandler.downloadBlueprintModelFile(id)
168         }
169
170     @PostMapping(
171         "/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
172             .MULTIPART_FORM_DATA_VALUE]
173     )
174     @ResponseBody
175     @Throws(BluePrintException::class)
176     @PreAuthorize("hasRole('USER')")
177     suspend fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity<Resource> = mdcWebCoroutineScope {
178         bluePrintModelHandler.enrichBlueprint(file)
179     }
180
181     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
182     @ResponseBody
183     @Throws(BluePrintException::class)
184     @PreAuthorize("hasRole('USER')")
185     suspend fun publishBlueprint(@RequestPart("file") file: FilePart): BlueprintModelSearch = mdcWebCoroutineScope {
186         bluePrintModelHandler.publishBlueprint(file)
187     }
188
189     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
190     @ResponseBody
191     @PreAuthorize("hasRole('USER')")
192     suspend fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> =
193         mdcWebCoroutineScope {
194             bluePrintModelHandler.searchBlueprintModels(tags)
195         }
196
197     @DeleteMapping("/name/{name}/version/{version}")
198     @ApiOperation(
199         value = "Delete a CBA",
200         notes = "Delete the CBA package identified by its name and version.",
201         produces = MediaType.APPLICATION_JSON_VALUE
202     )
203     @PreAuthorize("hasRole('USER')")
204     suspend fun deleteBlueprint(
205         @ApiParam(value = "Name of the CBA.", required = true)
206         @PathVariable(value = "name") name: String,
207         @ApiParam(value = "Version of the CBA.", required = true)
208         @PathVariable(value = "version") version: String
209     ) = mdcWebCoroutineScope {
210         bluePrintModelHandler.deleteBlueprintModel(name, version)
211     }
212
213     @PostMapping(
214         path = arrayOf("/workflow-spec"), produces = arrayOf(
215             MediaType
216                 .APPLICATION_JSON_VALUE
217         ),
218         consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
219     )
220     @ResponseBody
221     @Throws(BluePrintException::class)
222     @PreAuthorize("hasRole('USER')")
223     suspend fun workflowSpec(@RequestBody workFlowSpecReq: WorkFlowSpecRequest):
224         ResponseEntity<String> = mdcWebCoroutineScope {
225         var json = bluePrintModelHandler.prepareWorkFlowSpec(workFlowSpecReq)
226             .asJsonString()
227         ResponseEntity(json, HttpStatus.OK)
228     }
229
230     @GetMapping(
231         path = arrayOf(
232             "/workflows/blueprint-name/{name}/version/{version" +
233                 "}"
234         ),
235         produces = arrayOf(MediaType.APPLICATION_JSON_VALUE)
236     )
237     @ResponseBody
238     @Throws(BluePrintException::class)
239     @PreAuthorize("hasRole('USER')")
240     suspend fun getWorkflowList(
241         @ApiParam(value = "Name of the CBA.", required = true)
242         @PathVariable(value = "name") name: String,
243         @ApiParam(value = "Version of the CBA.", required = true)
244         @PathVariable(value = "version") version: String
245     ): ResponseEntity<String> = mdcWebCoroutineScope {
246         var json = bluePrintModelHandler.getWorkflowNames(name, version)
247             .asJsonString()
248         ResponseEntity(json, HttpStatus.OK)
249     }
250 }