1aae8aa1c1779d74b5e5d13147f314af9ad03873
[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 = "/api/v1/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         path = [""],
59         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]
60     )
61     @ApiOperation(
62         value = "Get all resolved resources using the resolution key. ",
63         notes = "Retrieve all stored resolved resources using the blueprint name, blueprint version, " +
64                 "artifact name and the resolution-key.",
65         response = ResourceResolution::class,
66         responseContainer = "List",
67         produces = MediaType.APPLICATION_JSON_VALUE
68     )
69     @ResponseBody
70     @PreAuthorize("hasRole('USER')")
71     fun getAllFromResolutionKeyOrFromResourceTypeAndId(
72         @ApiParam(value = "Name of the CBA.", required = true)
73         @RequestParam(value = "bpName", required = true) bpName: String,
74         @ApiParam(value = "Version of the CBA.", required = true)
75         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
76         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
77         @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
78         @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
79         @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
80         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
81         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
82         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
83         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String
84     ):
85             ResponseEntity<List<ResourceResolution>> = runBlocking {
86
87         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
88             throw httpProcessorException(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         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
91             ResponseEntity.ok()
92                 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
93         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
94             ResponseEntity.ok()
95                 .body(
96                     resourceResolutionDBService.readWithResourceIdAndResourceType(
97                         bpName,
98                         bpVersion,
99                         resourceId,
100                         resourceType
101                     )
102                 )
103         } else {
104             throw httpProcessorException(ErrorCatalogCodes.REQUEST_NOT_FOUND, ResourceApiDomains.RESOURCE_API,
105                     "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
106         }
107     }
108
109     @RequestMapping(
110             path = [""],
111             method = [RequestMethod.DELETE], produces = [MediaType.APPLICATION_JSON_VALUE]
112     )
113     @ApiOperation(value = "Delete resources using resolution key",
114             notes = "Delete all the resources associated to a resolution-key using blueprint metadata, artifact name and the resolution-key.",
115             produces = MediaType.APPLICATION_JSON_VALUE)
116     @PreAuthorize("hasRole('USER')")
117     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
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 = false, defaultValue = "") artifactName: String,
124         @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
125         @RequestParam(value = "resolutionKey", required = true) resolutionKey: String
126     ) = runBlocking {
127         ResponseEntity.ok()
128                 .body(resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
129     }
130
131     @RequestMapping(
132         path = ["/resource"],
133         method = [RequestMethod.GET],
134         produces = [MediaType.APPLICATION_JSON_VALUE]
135     )
136     @ApiOperation(
137         value = "Fetch a resource value using resolution key.",
138         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.",
139         produces = MediaType.APPLICATION_JSON_VALUE
140     )
141     @ResponseBody
142     @PreAuthorize("hasRole('USER')")
143     fun getOneFromResolutionKey(
144         @ApiParam(value = "Name of the CBA.", required = true)
145         @RequestParam(value = "bpName", required = true) bpName: String,
146         @ApiParam(value = "Version of the CBA.", required = true)
147         @RequestParam(value = "bpVersion", required = true) bpVersion: String,
148         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
149         @RequestParam(value = "artifactName", required = true) artifactName: String,
150         @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
151         @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
152         @ApiParam(value = "Name of the resource to retrieve.", required = true)
153         @RequestParam(value = "name", required = true) name: String
154     ):
155             ResponseEntity<ResourceResolution> = runBlocking {
156
157         ResponseEntity.ok()
158             .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))
159     }
160 }