2 * Copyright © 2019 Bell Canada
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.resource.api
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
41 @RequestMapping("/api/v1/resources")
44 description = "Interaction with resolved resources"
46 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
49 path = ["/health-check"],
50 method = [RequestMethod.GET],
51 produces = [MediaType.APPLICATION_JSON_VALUE]
54 @ApiOperation(value = "Health Check", hidden = true)
55 fun resourceControllerHealthCheck(): JsonNode = runBlocking {
56 JacksonUtils.getJsonNode("Success")
60 method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]
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
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
86 ResponseEntity<List<ResourceResolution>> = runBlocking {
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."
93 } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
95 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
96 } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
99 resourceResolutionDBService.readWithResourceIdAndResourceType(
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."
115 path = ["/occurrences"],
116 method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]
119 value = "Get the map of resolved resources with 'occurrence' as the keys to the resolved resources ",
120 notes = "With optional 'occurrence' options, subset of stored resolved resources can be retrieved " +
121 "using the blueprint name, blueprint version, artifact name and the resolution-key.",
122 response = ResourceResolution::class,
123 responseContainer = "List",
124 produces = MediaType.APPLICATION_JSON_VALUE
127 @PreAuthorize("hasRole('USER')")
129 @ApiParam(value = "Name of the CBA.", required = true)
130 @RequestParam(value = "bpName", required = true) bpName: String,
131 @ApiParam(value = "Version of the CBA.", required = true)
132 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
133 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
134 @RequestParam(value = "artifactName", required = true, defaultValue = "") artifactName: String,
135 @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
136 @RequestParam(value = "resolutionKey", required = true, defaultValue = "") resolutionKey: String,
137 @ApiParam(value = "Number of earlier N occurrences of the resolutions.", required = false)
138 @RequestParam(value = "firstN", required = false) firstN: Int?,
139 @ApiParam(value = "Number of latest N occurrences of the resolutions.", required = false)
140 @RequestParam(value = "lastN", required = false) lastN: Int?,
141 @ApiParam(value = "For Range option - 'begin' is the start occurrence of range of the resolutions.", required = false)
142 @RequestParam(value = "begin", required = false) begin: Int?,
143 @ApiParam(value = "For Range option - 'end' is the end occurrence of the range of the resolutions.", required = false)
144 @RequestParam(value = "end", required = false) end: Int?
145 ): ResponseEntity<Map<Int, List<ResourceResolution>>> = runBlocking {
147 artifactName.isEmpty() -> "'artifactName' must not be empty"
148 resolutionKey.isEmpty() -> "'resolutionKey' must not be empty"
149 // Optional options - validate if provided
150 (firstN != null && lastN != null) -> "Retrieve occurrences using either 'firstN' OR 'lastN' option"
151 ((firstN != null || lastN != null) && (begin != null || end != null)) -> "Retrieve occurrences using either 'firstN' OR 'lastN' OR 'begin' and 'end' option."
152 ((begin != null && end == null) || (begin == null && end != null)) -> " Retrieving occurrences within range - please provide both 'begin' and 'end' option"
154 }?.let { throw httpProcessorException(ErrorCatalogCodes.REQUEST_NOT_FOUND, ResourceApiDomains.RESOURCE_API, it) }
158 resourceResolutionDBService.findFirstNOccurrences(bpName, bpVersion, artifactName, resolutionKey, firstN)
160 resourceResolutionDBService.findLastNOccurrences(bpName, bpVersion, artifactName, resolutionKey, lastN)
161 begin != null && end != null ->
162 resourceResolutionDBService.findOccurrencesWithinRange(bpName, bpVersion, artifactName, resolutionKey, begin, end)
164 resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey).groupBy(ResourceResolution::occurrence).toSortedMap(reverseOrder())
165 }.let { result -> ResponseEntity.ok().body(result) }
169 method = [RequestMethod.DELETE], produces = [MediaType.APPLICATION_JSON_VALUE]
172 value = "Delete resources using resolution key",
173 notes = "Delete all the resources associated to a resolution-key using blueprint metadata, artifact name and the resolution-key."
175 @PreAuthorize("hasRole('USER')")
176 fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
177 @ApiParam(value = "Name of the CBA", required = true)
178 @RequestParam(value = "bpName", required = true) bpName: String,
179 @ApiParam(value = "Version of the CBA", required = true)
180 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
181 @ApiParam(value = "Artifact name for which to retrieve a resolved resource", required = true)
182 @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
183 @ApiParam(value = "Resolution Key associated with the resolution", required = true)
184 @RequestParam(value = "resolutionKey", required = true) resolutionKey: String
188 resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
198 path = ["/resource"],
199 method = [RequestMethod.GET],
200 produces = [MediaType.APPLICATION_JSON_VALUE]
203 value = "Fetch a resource value using resolution key",
204 notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve."
207 @PreAuthorize("hasRole('USER')")
208 fun getOneFromResolutionKey(
209 @ApiParam(value = "Name of the CBA", required = true)
210 @RequestParam(value = "bpName", required = true) bpName: String,
211 @ApiParam(value = "Version of the CBA", required = true)
212 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
213 @ApiParam(value = "Artifact name for which to retrieve a resolved resource", required = true)
214 @RequestParam(value = "artifactName", required = true) artifactName: String,
215 @ApiParam(value = "Resolution Key associated with the resolution", required = true)
216 @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
217 @ApiParam(value = "Name of the resource to retrieve", required = true)
218 @RequestParam(value = "name", required = true) name: String
220 ResponseEntity<out Any>? = runBlocking {
222 var result: ResourceResolution? = resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name)
223 if (result != null) {
224 ResponseEntity.ok().body(result)
226 var errorPayload = ErrorPayload(HttpStatus.NOT_FOUND.value(), ErrorCatalogCodes.GENERIC_FAILURE, "No records found")
227 ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorPayload)