4a2a55930471e656042f2c83f80a1c373e68c52a
[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.httpProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
29 import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload
30 import org.springframework.http.HttpStatus
31 import org.springframework.http.MediaType
32 import org.springframework.http.ResponseEntity
33 import org.springframework.security.access.prepost.PreAuthorize
34 import org.springframework.web.bind.annotation.RequestMapping
35 import org.springframework.web.bind.annotation.RequestMethod
36 import org.springframework.web.bind.annotation.RequestParam
37 import org.springframework.web.bind.annotation.ResponseBody
38 import org.springframework.web.bind.annotation.RestController
39
40 @RestController
41 @RequestMapping("/api/v1/resources")
42 @Api(
43     value = "Resources",
44     description = "Interaction with resolved resources"
45 )
46 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
47
48     @RequestMapping(
49         path = ["/health-check"],
50         method = [RequestMethod.GET],
51         produces = [MediaType.APPLICATION_JSON_VALUE]
52     )
53     @ResponseBody
54     @ApiOperation(value = "Health Check", hidden = true)
55     fun resourceControllerHealthCheck(): JsonNode = runBlocking {
56         JacksonUtils.getJsonNode("Success")
57     }
58
59     @RequestMapping(
60         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]
61     )
62     @ApiOperation(
63         value = "Get all resolved resources using the resolution key",
64         notes = "Retrieve all stored resolved resources using the blueprint name, blueprint version, " +
65             "artifact name and the resolution-key.",
66         response = ResourceResolution::class,
67         responseContainer = "List",
68         produces = MediaType.APPLICATION_JSON_VALUE
69     )
70     @ResponseBody
71     @PreAuthorize("hasRole('USER')")
72     fun getAllFromResolutionKeyOrFromResourceTypeAndId(
73         @ApiParam(value = "Name of the CBA", required = true)
74         @RequestParam(value = "bpName", required = true) bpName: String,
75         @ApiParam(value = "Version of the CBA", required = true)
76         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
77         @ApiParam(value = "Artifact name for which to retrieve a resolved resource", required = true)
78         @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
79         @ApiParam(value = "Resolution Key associated with the resolution", required = false)
80         @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
81         @ApiParam(value = "Resource Type associated with the resolution", required = false)
82         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
83         @ApiParam(value = "Resource Id associated with the resolution", required = false)
84         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String
85     ):
86         ResponseEntity<List<ResourceResolution>> = runBlocking {
87
88             if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
89                 throw httpProcessorException(
90                     ErrorCatalogCodes.REQUEST_NOT_FOUND, ResourceApiDomains.RESOURCE_API,
91                     "Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type."
92                 )
93             } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
94                 ResponseEntity.ok()
95                     .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
96             } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
97                 ResponseEntity.ok()
98                     .body(
99                         resourceResolutionDBService.readWithResourceIdAndResourceType(
100                             bpName,
101                             bpVersion,
102                             resourceId,
103                             resourceType
104                         )
105                     )
106             } else {
107                 throw httpProcessorException(
108                     ErrorCatalogCodes.REQUEST_NOT_FOUND, ResourceApiDomains.RESOURCE_API,
109                     "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type."
110                 )
111             }
112         }
113
114     @RequestMapping(
115         method = [RequestMethod.DELETE], produces = [MediaType.APPLICATION_JSON_VALUE]
116     )
117     @ApiOperation(
118         value = "Delete resources using resolution key",
119         notes = "Delete all the resources associated to a resolution-key using blueprint metadata, artifact name and the resolution-key."
120     )
121     @PreAuthorize("hasRole('USER')")
122     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
123         @ApiParam(value = "Name of the CBA", required = true)
124         @RequestParam(value = "bpName", required = true) bpName: String,
125         @ApiParam(value = "Version of the CBA", required = true)
126         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
127         @ApiParam(value = "Artifact name for which to retrieve a resolved resource", required = true)
128         @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
129         @ApiParam(value = "Resolution Key associated with the resolution", required = true)
130         @RequestParam(value = "resolutionKey", required = true) resolutionKey: String
131     ) = runBlocking {
132         ResponseEntity.ok()
133             .body(
134                 resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
135                     bpName,
136                     bpVersion,
137                     artifactName,
138                     resolutionKey
139                 )
140             )
141     }
142
143     @RequestMapping(
144         path = ["/resource"],
145         method = [RequestMethod.GET],
146         produces = [MediaType.APPLICATION_JSON_VALUE]
147     )
148     @ApiOperation(
149         value = "Fetch a resource value using resolution key",
150         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve."
151     )
152     @ResponseBody
153     @PreAuthorize("hasRole('USER')")
154     fun getOneFromResolutionKey(
155         @ApiParam(value = "Name of the CBA", required = true)
156         @RequestParam(value = "bpName", required = true) bpName: String,
157         @ApiParam(value = "Version of the CBA", required = true)
158         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
159         @ApiParam(value = "Artifact name for which to retrieve a resolved resource", required = true)
160         @RequestParam(value = "artifactName", required = true) artifactName: String,
161         @ApiParam(value = "Resolution Key associated with the resolution", required = true)
162         @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
163         @ApiParam(value = "Name of the resource to retrieve", required = true)
164         @RequestParam(value = "name", required = true) name: String
165     ):
166         ResponseEntity<out Any>? = runBlocking {
167
168             var result: ResourceResolution? = resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name)
169             if (result != null) {
170                 ResponseEntity.ok().body(result)
171             } else {
172                 var errorPayload = ErrorPayload(HttpStatus.NOT_FOUND.value(), ErrorCatalogCodes.GENERIC_FAILURE, "No records found")
173                 ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorPayload)
174             }
175         }
176 }