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