c50853ff28bb110486a173d9ddcd892dd6e3e275
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada
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.resource.api
18
19 import io.swagger.annotations.ApiOperation
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
23 import org.springframework.http.MediaType
24 import org.springframework.http.ResponseEntity
25 import org.springframework.security.access.prepost.PreAuthorize
26 import org.springframework.web.bind.annotation.*
27
28 @RestController
29 @RequestMapping("/api/v1/resources")
30 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
31
32     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
33     @ResponseBody
34     fun ping(): String = runBlocking {
35         "Success"
36     }
37
38     @RequestMapping(path = [""],
39         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
40     @ApiOperation(value = "Fetch all resource values associated to a resolution key. ",
41         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name and the resolution-key.",
42         produces = MediaType.APPLICATION_JSON_VALUE)
43     @ResponseBody
44     @PreAuthorize("hasRole('USER')")
45     fun getAllFromResolutionKeyOrFromResourceTypeAndId(@RequestParam(value = "bpName", required = true) bpName: String,
46                                 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
47                                 @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
48                                 @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
49                                 @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
50                                 @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String)
51             : ResponseEntity<List<ResourceResolution>> = runBlocking {
52
53         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
54             throw ResourceException("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
55         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
56             ResponseEntity.ok()
57                 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
58         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()){
59                 ResponseEntity.ok()
60                     .body(resourceResolutionDBService.readWithResourceIdAndResourceType(bpName, bpVersion, resourceId, resourceType))
61         } else {
62             throw ResourceException("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
63         }
64     }
65
66     @RequestMapping(path = ["/resource"],
67         method = [RequestMethod.GET],
68         produces = [MediaType.APPLICATION_JSON_VALUE])
69     @ApiOperation(value = "Fetch a resource value using resolution key.",
70         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.",
71         produces = MediaType.APPLICATION_JSON_VALUE)
72     @ResponseBody
73     @PreAuthorize("hasRole('USER')")
74     fun getOneFromResolutionKey(@RequestParam(value = "bpName", required = true) bpName: String,
75                                 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
76                                 @RequestParam(value = "artifactName", required = true) artifactName: String,
77                                 @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
78                                 @RequestParam(value = "name", required = true) name: String)
79             : ResponseEntity<ResourceResolution> = runBlocking {
80
81         ResponseEntity.ok()
82             .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))
83     }
84 }