Add GRPC log tracing service.
[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  *
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.rest.service.monoMdc
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.springframework.core.io.Resource
27 import org.springframework.http.MediaType
28 import org.springframework.http.ResponseEntity
29 import org.springframework.http.codec.multipart.FilePart
30 import org.springframework.security.access.prepost.PreAuthorize
31 import org.springframework.web.bind.annotation.*
32 import reactor.core.publisher.Mono
33
34 /**
35  * BlueprintModelController Purpose: Handle controllerBlueprint API request
36  *
37  * @author Vinal Patel
38  * @version 1.0
39  */
40 @RestController
41 @RequestMapping("/api/v1/blueprint-model")
42 open class BlueprintModelController(private val bluePrintModelHandler: BluePrintModelHandler) {
43
44     @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
45     @ResponseBody
46     @Throws(BluePrintException::class)
47     @PreAuthorize("hasRole('USER')")
48     fun saveBlueprint(@RequestPart("file") filePart: FilePart): Mono<BlueprintModelSearch> = monoMdc {
49         bluePrintModelHandler.saveBlueprintModel(filePart)
50     }
51
52     @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
53     @ResponseBody
54     @PreAuthorize("hasRole('USER')")
55     fun allBlueprintModel(): List<BlueprintModelSearch> {
56         return this.bluePrintModelHandler.allBlueprintModel()
57     }
58
59     @DeleteMapping("/{id}")
60     @Throws(BluePrintException::class)
61     @PreAuthorize("hasRole('USER')")
62     fun deleteBlueprint(@PathVariable(value = "id") id: String) {
63         this.bluePrintModelHandler.deleteBlueprintModel(id)
64     }
65
66     @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
67     @ResponseBody
68     @Throws(BluePrintException::class)
69     @PreAuthorize("hasRole('USER')")
70     fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
71                                      @PathVariable(value = "version") version: String)
72             : Mono<BlueprintModelSearch> = monoMdc {
73         bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
74     }
75
76     @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
77     @ResponseBody
78     @Throws(BluePrintException::class)
79     @PreAuthorize("hasRole('USER')")
80     fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String,
81                                           @PathVariable(value = "version") version: String)
82             : Mono<ResponseEntity<Resource>> = monoMdc {
83         bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
84     }
85
86     @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
87     @ResponseBody
88     @Throws(BluePrintException::class)
89     @PreAuthorize("hasRole('USER')")
90     fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch {
91         return this.bluePrintModelHandler.getBlueprintModelSearch(id)
92     }
93
94     @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
95     @ResponseBody
96     @Throws(BluePrintException::class)
97     @PreAuthorize("hasRole('USER')")
98     fun downloadBluePrint(@PathVariable(value = "id") id: String): Mono<ResponseEntity<Resource>> = monoMdc {
99         bluePrintModelHandler.downloadBlueprintModelFile(id)
100     }
101
102     @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType
103             .MULTIPART_FORM_DATA_VALUE])
104     @ResponseBody
105     @Throws(BluePrintException::class)
106     @PreAuthorize("hasRole('USER')")
107     fun enrichBlueprint(@RequestPart("file") file: FilePart): Mono<ResponseEntity<Resource>> = monoMdc {
108         bluePrintModelHandler.enrichBlueprint(file)
109     }
110
111     @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE])
112     @ResponseBody
113     @Throws(BluePrintException::class)
114     @PreAuthorize("hasRole('USER')")
115     fun publishBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> = monoMdc {
116         bluePrintModelHandler.publishBlueprint(file)
117     }
118
119     @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
120     @ResponseBody
121     @PreAuthorize("hasRole('USER')")
122     fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> {
123         return this.bluePrintModelHandler.searchBlueprintModels(tags)
124     }
125
126     @DeleteMapping("/name/{name}/version/{version}")
127     @ApiOperation(value = "Delete a CBA",
128             notes = "Delete the CBA package identified by its name and version.",
129             produces = MediaType.APPLICATION_JSON_VALUE)
130     @PreAuthorize("hasRole('USER')")
131     fun deleteBlueprint(@ApiParam(value = "Name of the CBA.", required = true)
132                         @PathVariable(value = "name") name: String,
133                         @ApiParam(value = "Version of the CBA.", required = true)
134                         @PathVariable(value = "version") version: String) = monoMdc {
135         bluePrintModelHandler.deleteBlueprintModel(name, version)
136     }
137 }