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.TemplateResolution
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
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.PathVariable
31 import org.springframework.web.bind.annotation.PostMapping
32 import org.springframework.web.bind.annotation.RequestBody
33 import org.springframework.web.bind.annotation.RequestMapping
34 import org.springframework.web.bind.annotation.RequestMethod
35 import org.springframework.web.bind.annotation.RequestParam
36 import org.springframework.web.bind.annotation.ResponseBody
37 import org.springframework.web.bind.annotation.RestController
40 * Exposes Template Resolution API to store and retrieve rendered template results.
42 * @author Serge Simard
46 @RequestMapping("/api/v1/template")
48 value = "/api/v1/template",
49 description = "Interaction with resolved template."
51 open class TemplateController(private val templateResolutionService: TemplateResolutionService) {
54 path = ["/health-check"],
55 method = [RequestMethod.GET],
56 produces = [MediaType.APPLICATION_JSON_VALUE]
59 @ApiOperation(value = "Health Check", hidden = true)
60 fun templateControllerHealthCheck(): JsonNode = runBlocking {
61 JacksonUtils.getJsonNode("Success")
66 method = [RequestMethod.GET],
67 produces = [MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE]
70 value = "Retrieve a resolved template.",
71 notes = "Retrieve a config template for a given CBA's action, identified by its blueprint name, blueprint version, " +
72 "artifact name and resolution key. An extra 'format' parameter can be passed to tell what content-type" +
73 " to expect in return"
76 @PreAuthorize("hasRole('USER')")
78 @ApiParam(value = "Name of the CBA.", required = true)
79 @RequestParam(value = "bpName") bpName: String,
80 @ApiParam(value = "Version of the CBA.", required = true)
81 @RequestParam(value = "bpVersion") bpVersion: String,
82 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
83 @RequestParam(value = "artifactName") artifactName: String,
84 @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
85 @RequestParam(value = "resolutionKey") resolutionKey: String,
86 @ApiParam(value = "Resource Type associated with the resolution.", required = false)
87 @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
88 @ApiParam(value = "Resource Id associated with the resolution.", required = false)
89 @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String,
91 value = "Expected format of the template being retrieved.",
92 defaultValue = MediaType.TEXT_PLAIN_VALUE,
95 @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String
97 ResponseEntity<String> = runBlocking {
101 if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
102 throw ResolutionException("Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
103 } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
104 result = templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
110 } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
112 templateResolutionService.findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
120 throw ResolutionException("Missing param. Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
123 var expectedContentType = format
124 if (expectedContentType.indexOf('/') < 0) {
125 expectedContentType = "application/$expectedContentType"
127 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
129 ResponseEntity.ok().contentType(expectedMediaType).body(result)
132 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
134 value = "Store a resolved template w/ resolution-key",
135 notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
136 "artifact name and resolution key.",
137 response = TemplateResolution::class,
138 produces = MediaType.APPLICATION_JSON_VALUE
141 @PreAuthorize("hasRole('USER')")
142 fun postWithResolutionKey(
143 @ApiParam(value = "Name of the CBA.", required = true)
144 @PathVariable(value = "bpName") bpName: String,
145 @ApiParam(value = "Version of the CBA.", required = true)
146 @PathVariable(value = "bpVersion") bpVersion: String,
147 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
148 @PathVariable(value = "artifactName") artifactName: String,
149 @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
150 @PathVariable(value = "resolutionKey") resolutionKey: String,
151 @ApiParam(value = "Template to store.", required = true)
152 @RequestBody result: String
153 ): ResponseEntity<TemplateResolution> = runBlocking {
156 templateResolutionService.write(bpName, bpVersion, artifactName, result, resolutionKey = resolutionKey)
158 ResponseEntity.ok().body(resultStored)
162 "/{bpName}/{bpVersion}/{artifactName}/{resourceType}/{resourceId}",
163 produces = [MediaType.APPLICATION_JSON_VALUE]
166 value = "Store a resolved template w/ resourceId and resourceType",
167 notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
168 "artifact name, resourceId and resourceType.",
169 response = TemplateResolution::class,
170 produces = MediaType.APPLICATION_JSON_VALUE
173 @PreAuthorize("hasRole('USER')")
174 fun postWithResourceIdAndResourceType(
175 @ApiParam(value = "Name of the CBA.", required = true)
176 @PathVariable(value = "bpName") bpName: String,
177 @ApiParam(value = "Version of the CBA.", required = true)
178 @PathVariable(value = "bpVersion") bpVersion: String,
179 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
180 @PathVariable(value = "artifactName") artifactName: String,
181 @ApiParam(value = "Resource Type associated with the resolution.", required = false)
182 @PathVariable(value = "resourceType", required = true) resourceType: String,
183 @ApiParam(value = "Resource Id associated with the resolution.", required = false)
184 @PathVariable(value = "resourceId", required = true) resourceId: String,
185 @ApiParam(value = "Template to store.", required = true)
186 @RequestBody result: String
187 ): ResponseEntity<TemplateResolution> = runBlocking {
190 templateResolutionService.write(bpName, bpVersion, artifactName, result, resourceId = resourceId, resourceType = resourceType)
192 ResponseEntity.ok().body(resultStored)