4c8fc193cb7c4fd596fda28bd2da36d895d97cc8
[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 com.fasterxml.jackson.databind.JsonNode
20 import io.swagger.annotations.Api
21 import io.swagger.annotations.ApiOperation
22 import io.swagger.annotations.ApiParam
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.springframework.http.MediaType
28 import org.springframework.http.ResponseEntity
29 import org.springframework.security.access.prepost.PreAuthorize
30 import org.springframework.web.bind.annotation.*
31
32 @RestController
33 @RequestMapping("/api/v1/resources")
34 @Api(value = "/api/v1/resources",
35     description = "Interaction with resolved resources.")
36 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
37
38     @RequestMapping(path = ["/health-check"],
39         method = [RequestMethod.GET],
40         produces = [MediaType.APPLICATION_JSON_VALUE])
41     @ResponseBody
42     @ApiOperation(value = "Health Check", hidden = true)
43     fun resourceControllerHealthCheck(): JsonNode = runBlocking {
44         JacksonUtils.getJsonNode("Success")
45     }
46
47     @RequestMapping(path = [""],
48         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
49     @ApiOperation(value = "Get all resolved resources using the resolution key. ",
50         notes = "Retrieve all stored resolved resources using the blueprint name, blueprint version, " +
51                 "artifact name and the resolution-key.",
52         response = ResourceResolution::class,
53         responseContainer = "List",
54         produces = MediaType.APPLICATION_JSON_VALUE)
55     @ResponseBody
56     @PreAuthorize("hasRole('USER')")
57     fun getAllFromResolutionKeyOrFromResourceTypeAndId(
58         @ApiParam(value = "Name of the CBA.", required = true)
59         @RequestParam(value = "bpName", required = true) bpName: String,
60         @ApiParam(value = "Version of the CBA.", required = true)
61         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
62         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
63         @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
64         @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
65         @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
66         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
67         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
68         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
69         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String)
70             : ResponseEntity<List<ResourceResolution>> = runBlocking {
71
72         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
73             throw ResourceException("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
74         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
75             ResponseEntity.ok()
76                 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
77         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
78             ResponseEntity.ok()
79                 .body(resourceResolutionDBService.readWithResourceIdAndResourceType(bpName,
80                     bpVersion,
81                     resourceId,
82                     resourceType))
83         } else {
84             throw ResourceException("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
85         }
86     }
87
88     @RequestMapping(path = ["/resource"],
89         method = [RequestMethod.GET],
90         produces = [MediaType.APPLICATION_JSON_VALUE])
91     @ApiOperation(value = "Fetch a resource value using resolution key.",
92         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.",
93         produces = MediaType.APPLICATION_JSON_VALUE)
94     @ResponseBody
95     @PreAuthorize("hasRole('USER')")
96     fun getOneFromResolutionKey(@ApiParam(value = "Name of the CBA.", required = true)
97                                 @RequestParam(value = "bpName", required = true) bpName: String,
98                                 @ApiParam(value = "Version of the CBA.", required = true)
99                                 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
100                                 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.",
101                                     required = true)
102                                 @RequestParam(value = "artifactName", required = true) artifactName: String,
103                                 @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
104                                 @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
105                                 @ApiParam(value = "Name of the resource to retrieve.", required = true)
106                                 @RequestParam(value = "name", required = true) name: String)
107             : ResponseEntity<ResourceResolution> = runBlocking {
108
109         ResponseEntity.ok()
110             .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))
111     }
112 }