Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / resource-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / resource / api / ResourceController.kt
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.RequestMapping
31 import org.springframework.web.bind.annotation.RequestMethod
32 import org.springframework.web.bind.annotation.RequestParam
33 import org.springframework.web.bind.annotation.ResponseBody
34 import org.springframework.web.bind.annotation.RestController
35
36 @RestController
37 @RequestMapping("/api/v1/resources")
38 @Api(
39     value = "/api/v1/resources",
40     description = "Interaction with resolved resources."
41 )
42 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
43
44     @RequestMapping(
45         path = ["/health-check"],
46         method = [RequestMethod.GET],
47         produces = [MediaType.APPLICATION_JSON_VALUE]
48     )
49     @ResponseBody
50     @ApiOperation(value = "Health Check", hidden = true)
51     fun resourceControllerHealthCheck(): JsonNode = runBlocking {
52         JacksonUtils.getJsonNode("Success")
53     }
54
55     @RequestMapping(
56         path = [""],
57         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]
58     )
59     @ApiOperation(
60         value = "Get all resolved resources using the resolution key. ",
61         notes = "Retrieve all stored resolved resources using the blueprint name, blueprint version, " +
62                 "artifact name and the resolution-key.",
63         response = ResourceResolution::class,
64         responseContainer = "List",
65         produces = MediaType.APPLICATION_JSON_VALUE
66     )
67     @ResponseBody
68     @PreAuthorize("hasRole('USER')")
69     fun getAllFromResolutionKeyOrFromResourceTypeAndId(
70         @ApiParam(value = "Name of the CBA.", required = true)
71         @RequestParam(value = "bpName", required = true) bpName: String,
72         @ApiParam(value = "Version of the CBA.", required = true)
73         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
74         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
75         @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
76         @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
77         @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
78         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
79         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
80         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
81         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String
82     ):
83             ResponseEntity<List<ResourceResolution>> = runBlocking {
84
85         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
86             throw ResolutionException("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
87         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
88             ResponseEntity.ok()
89                 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
90         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
91             ResponseEntity.ok()
92                 .body(
93                     resourceResolutionDBService.readWithResourceIdAndResourceType(
94                         bpName,
95                         bpVersion,
96                         resourceId,
97                         resourceType
98                     )
99                 )
100         } else {
101             throw ResolutionException("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
102         }
103     }
104
105     @RequestMapping(
106         path = ["/resource"],
107         method = [RequestMethod.GET],
108         produces = [MediaType.APPLICATION_JSON_VALUE]
109     )
110     @ApiOperation(
111         value = "Fetch a resource value using resolution key.",
112         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.",
113         produces = MediaType.APPLICATION_JSON_VALUE
114     )
115     @ResponseBody
116     @PreAuthorize("hasRole('USER')")
117     fun getOneFromResolutionKey(
118         @ApiParam(value = "Name of the CBA.", required = true)
119         @RequestParam(value = "bpName", required = true) bpName: String,
120         @ApiParam(value = "Version of the CBA.", required = true)
121         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
122         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
123         @RequestParam(value = "artifactName", required = true) artifactName: String,
124         @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
125         @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
126         @ApiParam(value = "Name of the resource to retrieve.", required = true)
127         @RequestParam(value = "name", required = true) name: String
128     ):
129             ResponseEntity<ResourceResolution> = runBlocking {
130
131         ResponseEntity.ok()
132             .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))
133     }
134 }