10cc0a44a832c4b3fa1d0c0a71c6ee6070acb8d9
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
19
20 import io.swagger.annotations.ApiOperation
21 import io.swagger.annotations.ApiParam
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.BluePrintModelHandler
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BlueprintSortByOption
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.monoMdc
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.springframework.core.io.Resource
28 import org.springframework.data.domain.Page
29 import org.springframework.data.domain.PageRequest
30 import org.springframework.data.domain.Sort
31 import org.springframework.http.MediaType
32 import org.springframework.http.ResponseEntity
33 import org.springframework.http.codec.multipart.FilePart
34 import org.springframework.security.access.prepost.PreAuthorize
35 import org.springframework.web.bind.annotation.*
36 import reactor.core.publisher.Mono
37
38 /**
39  * BlueprintModelController Purpose: Handle controllerBlueprint API request
40  *
41  * @author Vinal Patel
42  * @version 1.0
43  */
44 @RestController
45 @RequestMapping("/api/v1/blueprint-model")
46 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
47
48     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
49     @ResponseBody
50     @Throws(BluePrintException::class)
51     @PreAuthorize("hasRole('USER')")
52     fun saveBlueprint(@RequestPart("file") filePart: FilePart): Mono<BlueprintModelSearch> = monoMdc {
53         bluePrintModelHandler.saveBlueprintModel(filePart)
54     }
55
56     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
57     @ResponseBody
58     @PreAuthorize("hasRole('USER')")
59     fun allBlueprintModel(): List<BlueprintModelSearch> {
60         return this.bluePrintModelHandler.allBlueprintModel()
61     }
62
63     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
64     @ResponseBody
65     @PreAuthorize("hasRole('USER')")
66     fun allBlueprintModel(@RequestParam(defaultValue = "20") limit: Int,
67                           @RequestParam(defaultValue = "0") offset: Int,
68                           @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption): Page<BlueprintModelSearch> {
69         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
70         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
71     }
72
73     @DeleteMapping("/{id}")
74     @Throws(BluePrintException::class)
75     @PreAuthorize("hasRole('USER')")
76     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
77         this.bluePrintModelHandler.deleteBlueprintModel(id)
78     }
79
80     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
81     @ResponseBody
82     @Throws(BluePrintException::class)
83     @PreAuthorize("hasRole('USER')")
84     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
85                                      @PathVariable(value = "version") version: String)
86             : Mono<BlueprintModelSearch> = monoMdc {
87         bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
88     }
89
90     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
91     @ResponseBody
92     @Throws(BluePrintException::class)
93     @PreAuthorize("hasRole('USER')")
94     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
95                                           @PathVariable(value = "version") version: String)
96             : Mono<ResponseEntity<Resource>> = monoMdc {
97         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
98     }
99
100     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
101     @ResponseBody
102     @Throws(BluePrintException::class)
103     @PreAuthorize("hasRole('USER')")
104     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
105         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
106     }
107
108     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
109     @ResponseBody
110     @Throws(BluePrintException::class)
111     @PreAuthorize("hasRole('USER')")
112     fun downloadBluePrint(@PathVariable(value = "id") id: String): Mono<ResponseEntity<Resource>> = monoMdc {
113         bluePrintModelHandler.downloadBlueprintModelFile(id)
114     }
115
116     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
117             .MULTIPART_FORM_DATA_VALUE])
118     @ResponseBody
119     @Throws(BluePrintException::class)
120     @PreAuthorize("hasRole('USER')")
121     fun enrichBlueprint(@RequestPart("file") file: FilePart): Mono<ResponseEntity<Resource>> = monoMdc {
122         bluePrintModelHandler.enrichBlueprint(file)
123     }
124
125     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
126     @ResponseBody
127     @Throws(BluePrintException::class)
128     @PreAuthorize("hasRole('USER')")
129     fun publishBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> = monoMdc {
130         bluePrintModelHandler.publishBlueprint(file)
131     }
132
133     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
134     @ResponseBody
135     @PreAuthorize("hasRole('USER')")
136     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
137         return this.bluePrintModelHandler.searchBlueprintModels(tags)
138     }
139
140     @DeleteMapping("/name/{name}/version/{version}")
141     @ApiOperation(value = "Delete a CBA",
142             notes = "Delete the CBA package identified by its name and version.",
143             produces = MediaType.APPLICATION_JSON_VALUE)
144     @PreAuthorize("hasRole('USER')")
145     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
146                         @PathVariable(value = "name") name: String,
147                         @ApiParam(value = "Version of the CBA.", required = true)
148                         @PathVariable(value = "version") version: String) = monoMdc {
149         bluePrintModelHandler.deleteBlueprintModel(name, version)
150     }
151 }