Adding some minor features
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / ResourceDictionaryController.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
18
19 import io.swagger.annotations.Api
20 import io.swagger.annotations.ApiOperation
21 import io.swagger.annotations.ApiParam
22 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.ResourceDictionaryHandler
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.DictionarySortByOption
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.mdcWebCoroutineScope
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
29 import org.springframework.data.domain.Page
30 import org.springframework.data.domain.PageRequest
31 import org.springframework.data.domain.Sort
32 import org.springframework.http.MediaType
33 import org.springframework.web.bind.annotation.DeleteMapping
34 import org.springframework.web.bind.annotation.GetMapping
35 import org.springframework.web.bind.annotation.PathVariable
36 import org.springframework.web.bind.annotation.PostMapping
37 import org.springframework.web.bind.annotation.RequestBody
38 import org.springframework.web.bind.annotation.RequestMapping
39 import org.springframework.web.bind.annotation.RequestParam
40 import org.springframework.web.bind.annotation.ResponseBody
41 import org.springframework.web.bind.annotation.RestController
42
43 @RestController
44 @RequestMapping(value = ["/api/v1/dictionary"])
45 @Api(
46     value = "Resource Dictionary",
47     description = "Interaction with stored dictionaries"
48 )
49 open class ResourceDictionaryController(private val resourceDictionaryHandler: ResourceDictionaryHandler) {
50
51     @GetMapping(path = ["/{name}"], produces = [MediaType.APPLICATION_JSON_VALUE])
52     @ApiOperation(
53         value = "Retrieve a resource dictionary",
54         notes = "Retrieve a resource dictionary by name provided.",
55         response = ResourceDictionary::class
56     )
57     @ResponseBody
58     @Throws(BluePrintException::class)
59     suspend fun getResourceDictionaryByName(
60         @ApiParam(value = "Name of the resource", required = true, example = "\"hostname\"")
61         @PathVariable(value = "name") name: String
62     ): ResourceDictionary =
63         mdcWebCoroutineScope {
64             resourceDictionaryHandler.getResourceDictionaryByName(name)
65         }
66
67     @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
68     @ApiOperation(
69         value = "Get Blueprints Dictionary ordered",
70         notes = "Lists all blueprint Dictionary which are saved in CDS in an ordered mode.",
71         nickname = "BlueprintModelController_allBlueprintDictionaryPaged_GET.org.onap.ccsdk.cds.blueprintsprocessor.designer.api"
72     )
73     @ResponseBody
74     suspend fun allBlueprintModel(
75         @ApiParam(value = "Maximum number of returned blueprint dictionary") @RequestParam(defaultValue = "20") limit: Int,
76         @ApiParam(value = "Offset") @RequestParam(defaultValue = "0") offset: Int,
77         @ApiParam(value = "Order of returned blueprint dictionary") @RequestParam(defaultValue = "DATE") sort: DictionarySortByOption,
78         @ApiParam(value = "Ascend or descend ordering") @RequestParam(defaultValue = "ASC") sortType: String
79     ): Page<ResourceDictionary> {
80         val pageRequest = PageRequest.of(
81             offset, limit,
82             Sort.Direction.fromString(sortType), sort.columnName
83         )
84         return resourceDictionaryHandler.getAllDictionary(pageRequest)
85     }
86
87     @PostMapping(
88         produces = [MediaType.APPLICATION_JSON_VALUE],
89         consumes = [MediaType.APPLICATION_JSON_VALUE]
90     )
91     @ApiOperation(
92         value = "Save a resource dictionary",
93         notes = "Save a resource dictionary by dictionary provided.",
94         response = ResourceDictionary::class
95     )
96     @ResponseBody
97     @Throws(BluePrintException::class)
98     suspend fun saveResourceDictionary(
99         @ApiParam(value = "Resource dictionary to store", required = true)
100         @RequestBody resourceDictionary: ResourceDictionary
101     ): ResourceDictionary =
102         mdcWebCoroutineScope {
103             resourceDictionaryHandler.saveResourceDictionary(resourceDictionary)
104         }
105
106     @PostMapping(
107         path = ["/definition"],
108         produces = [MediaType.APPLICATION_JSON_VALUE],
109         consumes = [MediaType.APPLICATION_JSON_VALUE]
110     )
111     @ApiOperation(
112         value = "Save a resource dictionary",
113         notes = "Save a resource dictionary by provided resource definition json.",
114         nickname = "ResourceDictionaryController_saveResourceDictionary_1_POST.org.onap.ccsdk.cds.blueprintsprocessor.designer.api",
115         response = ResourceDictionary::class
116     )
117     @ResponseBody
118     @Throws(BluePrintException::class)
119     suspend fun saveResourceDefinition(
120         @ApiParam(value = "Resource definition to generate Resource Dictionary", required = true)
121         @RequestBody resourceDefinition: ResourceDefinition
122     ): ResourceDictionary =
123         mdcWebCoroutineScope {
124             resourceDictionaryHandler.saveResourceDefinition(resourceDefinition)
125         }
126
127     @PostMapping(
128         path = ["/definition-bulk"],
129         produces = [MediaType.APPLICATION_JSON_VALUE],
130         consumes = [MediaType.APPLICATION_JSON_VALUE]
131     )
132     @ApiOperation(
133         value = "Save multiple resource dictionaries",
134         notes = "Save multiple resource dictionaries by provided resource definition json array.",
135         nickname = "ResourceDictionaryController_saveAllResourceDictionary_1_POST.org.onap.ccsdk.cds.blueprintsprocessor.designer.api",
136         response = ResourceDictionary::class
137     )
138     @ResponseBody
139     @Throws(BluePrintException::class)
140     suspend fun saveAllResourceDictionary(
141         @ApiParam(value = "Resource definition json array to generate Resource Dictionaries", required = true)
142         @RequestBody resourceDefinition: List<ResourceDefinition>
143     ): MutableList<ResourceDictionary> =
144         mdcWebCoroutineScope {
145             resourceDictionaryHandler.saveAllResourceDefinition(resourceDefinition)
146         }
147
148     @DeleteMapping(path = ["/{name}"])
149     @ApiOperation(
150         value = "Remove a resource dictionary",
151         notes = "Remove a resource dictionary by name provided."
152     )
153     suspend fun deleteResourceDictionaryByName(
154         @ApiParam(value = "Name of the resource", required = true)
155         @PathVariable(value = "name") name: String
156     ) = mdcWebCoroutineScope {
157         resourceDictionaryHandler.deleteResourceDictionary(name)
158     }
159
160     @PostMapping(
161         path = ["/by-names"],
162         produces = [MediaType.APPLICATION_JSON_VALUE],
163         consumes = [MediaType.APPLICATION_JSON_VALUE]
164     )
165     @ApiOperation(
166         value = "Search for a resource dictionary",
167         notes = "Search for a resource dictionary by names provided.",
168         responseContainer = "List",
169         response = ResourceDictionary::class
170     )
171     @ResponseBody
172     suspend fun searchResourceDictionaryByNames(
173         @ApiParam(value = "List of names", required = true)
174         @RequestBody names: List<String>
175     ): List<ResourceDictionary> =
176         mdcWebCoroutineScope {
177             resourceDictionaryHandler.searchResourceDictionaryByNames(names)
178         }
179
180     @GetMapping(path = ["/search/{tags}"], produces = [MediaType.APPLICATION_JSON_VALUE])
181     @ApiOperation(
182         value = "Search for a resource dictionary",
183         notes = "Search for a resource dictionary by tags provided.",
184         responseContainer = "List",
185         response = ResourceDictionary::class
186     )
187     @ResponseBody
188     suspend fun searchResourceDictionaryByTags(
189         @ApiParam(value = "Tags list", required = true, example = "\"status\"")
190         @PathVariable(value = "tags") tags: String
191     ): List<ResourceDictionary> =
192         mdcWebCoroutineScope {
193             resourceDictionaryHandler.searchResourceDictionaryByTags(tags)
194         }
195
196     @GetMapping(path = ["/source-mapping"], produces = [MediaType.APPLICATION_JSON_VALUE])
197     @ApiOperation(
198         value = "Search for a source mapping",
199         notes = "Search for a source mapping.",
200         response = ResourceSourceMapping::class
201     )
202     @ResponseBody
203     suspend fun getResourceSourceMapping(): ResourceSourceMapping = mdcWebCoroutineScope {
204         resourceDictionaryHandler.getResourceSourceMapping()
205     }
206
207     @GetMapping(path = ["/resource_dictionary_group"], produces = [MediaType.APPLICATION_JSON_VALUE])
208     @ApiOperation(
209         value = "Retrieve all resource dictionary groups",
210         notes = "Retrieve all resource dictionary groups.",
211         responseContainer = "List",
212         response = String::class
213     )
214     @ResponseBody
215     suspend fun getResourceDictionaryDistinct(): List<String> = mdcWebCoroutineScope {
216         resourceDictionaryHandler.getResourceDictionaryDistinct()
217     }
218 }