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