5ef50b09fbb2b344ff084477f3ed717d732102f7
[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.*
38 import reactor.core.publisher.Mono
39
40 /**
41  * BlueprintModelController Purpose: Handle controllerBlueprint API request
42  *
43  * @author Vinal Patel
44  * @version 1.0
45  */
46 @RestController
47 @RequestMapping("/api/v1/blueprint-model")
48 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
49
50     @PostMapping(path = arrayOf("/bootstrap"), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE),
51             consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE))
52     @ResponseBody
53     @Throws(BluePrintException::class)
54     @PreAuthorize("hasRole('USER')")
55     fun bootstrap(@RequestBody bootstrapRequest: BootstrapRequest): Mono<Unit> = monoMdc {
56         bluePrintModelHandler.bootstrapBlueprint(bootstrapRequest)
57     }
58
59     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
60     @ResponseBody
61     @Throws(BluePrintException::class)
62     @PreAuthorize("hasRole('USER')")
63     fun saveBlueprint(@RequestPart("file") filePart: FilePart): Mono<BlueprintModelSearch> = monoMdc {
64         bluePrintModelHandler.saveBlueprintModel(filePart)
65     }
66
67     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
68     @ResponseBody
69     @PreAuthorize("hasRole('USER')")
70     fun allBlueprintModel(): List<BlueprintModelSearch> {
71         return this.bluePrintModelHandler.allBlueprintModel()
72     }
73
74     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
75     @ResponseBody
76     @PreAuthorize("hasRole('USER')")
77     fun allBlueprintModel(@RequestParam(defaultValue = "20") limit: Int,
78                           @RequestParam(defaultValue = "0") offset: Int,
79                           @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption): Page<BlueprintModelSearch> {
80         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
81         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
82     }
83
84     @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
85     @ResponseBody
86     @PreAuthorize("hasRole('USER')")
87     fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> {
88         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
89     }
90
91
92     @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
93     @ResponseBody
94     @PreAuthorize("hasRole('USER')")
95     fun allBlueprintModelMetaDataPaged(@NotNull @PathVariable(value = "keyword") keyWord: String,
96                                        @RequestParam(defaultValue = "20") limit: Int,
97                                        @RequestParam(defaultValue = "0") offset: Int,
98                                        @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption
99     ): Page<BlueprintModelSearch> {
100         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
101         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWordPaged(keyWord, pageRequest)
102
103     }
104
105     @DeleteMapping("/{id}")
106     @Throws(BluePrintException::class)
107     @PreAuthorize("hasRole('USER')")
108     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
109         this.bluePrintModelHandler.deleteBlueprintModel(id)
110     }
111
112     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
113     @ResponseBody
114     @Throws(BluePrintException::class)
115     @PreAuthorize("hasRole('USER')")
116     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
117                                      @PathVariable(value = "version") version: String)
118             : Mono<BlueprintModelSearch> = monoMdc {
119         bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
120     }
121
122     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
123     @ResponseBody
124     @Throws(BluePrintException::class)
125     @PreAuthorize("hasRole('USER')")
126     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
127                                           @PathVariable(value = "version") version: String)
128             : Mono<ResponseEntity<Resource>> = monoMdc {
129         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
130     }
131
132     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
133     @ResponseBody
134     @Throws(BluePrintException::class)
135     @PreAuthorize("hasRole('USER')")
136     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
137         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
138     }
139
140     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
141     @ResponseBody
142     @Throws(BluePrintException::class)
143     @PreAuthorize("hasRole('USER')")
144     fun downloadBluePrint(@PathVariable(value = "id") id: String): Mono<ResponseEntity<Resource>> = monoMdc {
145         bluePrintModelHandler.downloadBlueprintModelFile(id)
146     }
147
148     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
149             .MULTIPART_FORM_DATA_VALUE])
150     @ResponseBody
151     @Throws(BluePrintException::class)
152     @PreAuthorize("hasRole('USER')")
153     fun enrichBlueprint(@RequestPart("file") file: FilePart): Mono<ResponseEntity<Resource>> = monoMdc {
154         bluePrintModelHandler.enrichBlueprint(file)
155     }
156
157     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
158     @ResponseBody
159     @Throws(BluePrintException::class)
160     @PreAuthorize("hasRole('USER')")
161     fun publishBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> = monoMdc {
162         bluePrintModelHandler.publishBlueprint(file)
163     }
164
165     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
166     @ResponseBody
167     @PreAuthorize("hasRole('USER')")
168     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
169         return this.bluePrintModelHandler.searchBlueprintModels(tags)
170     }
171
172     @DeleteMapping("/name/{name}/version/{version}")
173     @ApiOperation(value = "Delete a CBA",
174             notes = "Delete the CBA package identified by its name and version.",
175             produces = MediaType.APPLICATION_JSON_VALUE)
176     @PreAuthorize("hasRole('USER')")
177     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
178                         @PathVariable(value = "name") name: String,
179                         @ApiParam(value = "Version of the CBA.", required = true)
180                         @PathVariable(value = "version") version: String) = monoMdc {
181         bluePrintModelHandler.deleteBlueprintModel(name, version)
182     }
183 }