ea5023cd5f0a2df505d2f7de8805c49239360fdb
[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.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("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
51     @ResponseBody
52     @Throws(BluePrintException::class)
53     @PreAuthorize("hasRole('USER')")
54     fun saveBlueprint(@RequestPart("file") filePart: FilePart): Mono<BlueprintModelSearch> = monoMdc {
55         bluePrintModelHandler.saveBlueprintModel(filePart)
56     }
57
58     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
59     @ResponseBody
60     @PreAuthorize("hasRole('USER')")
61     fun allBlueprintModel(): List<BlueprintModelSearch> {
62         return this.bluePrintModelHandler.allBlueprintModel()
63     }
64
65     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
66     @ResponseBody
67     @PreAuthorize("hasRole('USER')")
68     fun allBlueprintModel(@RequestParam(defaultValue = "20") limit: Int,
69                           @RequestParam(defaultValue = "0") offset: Int,
70                           @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption): Page<BlueprintModelSearch> {
71         val pageRequest = PageRequest.of(offset, limit, Sort.Direction.ASC, sort.columnName)
72         return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
73     }
74
75     @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
76     @ResponseBody
77     @PreAuthorize("hasRole('USER')")
78     fun allBlueprintModelMetaData(@NotNull @PathVariable(value = "keyword") keyWord: String): List<BlueprintModelSearch> {
79         return this.bluePrintModelHandler.searchBluePrintModelsByKeyWord(keyWord)
80     }
81
82
83     @DeleteMapping("/{id}")
84     @Throws(BluePrintException::class)
85     @PreAuthorize("hasRole('USER')")
86     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
87         this.bluePrintModelHandler.deleteBlueprintModel(id)
88     }
89
90     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
91     @ResponseBody
92     @Throws(BluePrintException::class)
93     @PreAuthorize("hasRole('USER')")
94     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
95                                      @PathVariable(value = "version") version: String)
96             : Mono<BlueprintModelSearch> = monoMdc {
97         bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
98     }
99
100     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
101     @ResponseBody
102     @Throws(BluePrintException::class)
103     @PreAuthorize("hasRole('USER')")
104     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
105                                           @PathVariable(value = "version") version: String)
106             : Mono<ResponseEntity<Resource>> = monoMdc {
107         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
108     }
109
110     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
111     @ResponseBody
112     @Throws(BluePrintException::class)
113     @PreAuthorize("hasRole('USER')")
114     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
115         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
116     }
117
118     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
119     @ResponseBody
120     @Throws(BluePrintException::class)
121     @PreAuthorize("hasRole('USER')")
122     fun downloadBluePrint(@PathVariable(value = "id") id: String): Mono<ResponseEntity<Resource>> = monoMdc {
123         bluePrintModelHandler.downloadBlueprintModelFile(id)
124     }
125
126     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
127             .MULTIPART_FORM_DATA_VALUE])
128     @ResponseBody
129     @Throws(BluePrintException::class)
130     @PreAuthorize("hasRole('USER')")
131     fun enrichBlueprint(@RequestPart("file") file: FilePart): Mono<ResponseEntity<Resource>> = monoMdc {
132         bluePrintModelHandler.enrichBlueprint(file)
133     }
134
135     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
136     @ResponseBody
137     @Throws(BluePrintException::class)
138     @PreAuthorize("hasRole('USER')")
139     fun publishBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> = monoMdc {
140         bluePrintModelHandler.publishBlueprint(file)
141     }
142
143     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
144     @ResponseBody
145     @PreAuthorize("hasRole('USER')")
146     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
147         return this.bluePrintModelHandler.searchBlueprintModels(tags)
148     }
149
150     @DeleteMapping("/name/{name}/version/{version}")
151     @ApiOperation(value = "Delete a CBA",
152             notes = "Delete the CBA package identified by its name and version.",
153             produces = MediaType.APPLICATION_JSON_VALUE)
154     @PreAuthorize("hasRole('USER')")
155     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
156                         @PathVariable(value = "name") name: String,
157                         @ApiParam(value = "Version of the CBA.", required = true)
158                         @PathVariable(value = "version") version: String) = monoMdc {
159         bluePrintModelHandler.deleteBlueprintModel(name, version)
160     }
161 }