Formatting Code base with ktlint
[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.monoMdc
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.MediaType
34 import org.springframework.http.ResponseEntity
35 import org.springframework.http.codec.multipart.FilePart
36 import org.springframework.security.access.prepost.PreAuthorize
37 import org.springframework.web.bind.annotation.DeleteMapping
38 import org.springframework.web.bind.annotation.GetMapping
39 import org.springframework.web.bind.annotation.PathVariable
40 import org.springframework.web.bind.annotation.PostMapping
41 import org.springframework.web.bind.annotation.RequestBody
42 import org.springframework.web.bind.annotation.RequestMapping
43 import org.springframework.web.bind.annotation.RequestParam
44 import org.springframework.web.bind.annotation.RequestPart
45 import org.springframework.web.bind.annotation.ResponseBody
46 import org.springframework.web.bind.annotation.RestController
47 import reactor.core.publisher.Mono
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     fun bootstrap(@RequestBody bootstrapRequest: BootstrapRequest): Mono<Unit> = monoMdc {
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     fun saveBlueprint(@RequestPart("file") filePart: FilePart): Mono<BlueprintModelSearch> = monoMdc {
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     fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> {
101         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
102     }
103
104     @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
105     @ResponseBody
106     @PreAuthorize("hasRole('USER')")
107     fun allBlueprintModelMetaDataPaged(
108         @NotNull @PathVariable(value = "keyword") keyWord: String,
109         @RequestParam(defaultValue = "20") limit: Int,
110         @RequestParam(defaultValue = "0") offset: Int,
111         @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption
112     ): Page<BlueprintModelSearch> {
113         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
114         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWordPaged(keyWord, pageRequest)
115     }
116
117     @DeleteMapping("/{id}")
118     @Throws(BluePrintException::class)
119     @PreAuthorize("hasRole('USER')")
120     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
121         this.bluePrintModelHandler.deleteBlueprintModel(id)
122     }
123
124     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
125     @ResponseBody
126     @Throws(BluePrintException::class)
127     @PreAuthorize("hasRole('USER')")
128     fun getBlueprintByNameAndVersion(
129         @PathVariable(value = "name") name: String,
130         @PathVariable(value = "version") version: String
131     ):
132             Mono<BlueprintModelSearch> = monoMdc {
133         bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
134     }
135
136     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
137     @ResponseBody
138     @Throws(BluePrintException::class)
139     @PreAuthorize("hasRole('USER')")
140     fun downloadBlueprintByNameAndVersion(
141         @PathVariable(value = "name") name: String,
142         @PathVariable(value = "version") version: String
143     ):
144             Mono<ResponseEntity<Resource>> = monoMdc {
145         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
146     }
147
148     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
149     @ResponseBody
150     @Throws(BluePrintException::class)
151     @PreAuthorize("hasRole('USER')")
152     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
153         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
154     }
155
156     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
157     @ResponseBody
158     @Throws(BluePrintException::class)
159     @PreAuthorize("hasRole('USER')")
160     fun downloadBluePrint(@PathVariable(value = "id") id: String): Mono<ResponseEntity<Resource>> = monoMdc {
161         bluePrintModelHandler.downloadBlueprintModelFile(id)
162     }
163
164     @PostMapping(
165         "/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
166             .MULTIPART_FORM_DATA_VALUE]
167     )
168     @ResponseBody
169     @Throws(BluePrintException::class)
170     @PreAuthorize("hasRole('USER')")
171     fun enrichBlueprint(@RequestPart("file") file: FilePart): Mono<ResponseEntity<Resource>> = monoMdc {
172         bluePrintModelHandler.enrichBlueprint(file)
173     }
174
175     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
176     @ResponseBody
177     @Throws(BluePrintException::class)
178     @PreAuthorize("hasRole('USER')")
179     fun publishBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> = monoMdc {
180         bluePrintModelHandler.publishBlueprint(file)
181     }
182
183     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
184     @ResponseBody
185     @PreAuthorize("hasRole('USER')")
186     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
187         return this.bluePrintModelHandler.searchBlueprintModels(tags)
188     }
189
190     @DeleteMapping("/name/{name}/version/{version}")
191     @ApiOperation(
192         value = "Delete a CBA",
193         notes = "Delete the CBA package identified by its name and version.",
194         produces = MediaType.APPLICATION_JSON_VALUE
195     )
196     @PreAuthorize("hasRole('USER')")
197     fun deleteBlueprint(
198         @ApiParam(value = "Name of the CBA.", required = true)
199         @PathVariable(value = "name") name: String,
200         @ApiParam(value = "Version of the CBA.", required = true)
201         @PathVariable(value = "version") version: String
202     ) = monoMdc {
203         bluePrintModelHandler.deleteBlueprintModel(name, version)
204     }
205 }