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.*
33 * Exposes Template Resolution API to store and retrieve rendered template results.
35 * @author Serge Simard
39 @RequestMapping("/api/v1/template")
40 @Api(value = "/api/v1/template",
41 description = "Interaction with resolved template.")
42 open class TemplateController(private val templateResolutionService: TemplateResolutionService) {
44 @RequestMapping(path = ["/health-check"],
45 method = [RequestMethod.GET],
46 produces = [MediaType.APPLICATION_JSON_VALUE])
48 @ApiOperation(value = "Health Check", hidden = true)
49 fun templateControllerHealthCheck(): JsonNode = runBlocking {
50 JacksonUtils.getJsonNode("Success")
53 @RequestMapping(path = [""],
54 method = [RequestMethod.GET],
55 produces = [MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE])
56 @ApiOperation(value = "Retrieve a resolved template.",
57 notes = "Retrieve a config template for a given CBA's action, identified by its blueprint name, blueprint version, " +
58 "artifact name and resolution key. An extra 'format' parameter can be passed to tell what content-type" +
59 " to expect in return")
61 @PreAuthorize("hasRole('USER')")
63 @ApiParam(value = "Name of the CBA.", required = true)
64 @RequestParam(value = "bpName") bpName: String,
65 @ApiParam(value = "Version of the CBA.", required = true)
66 @RequestParam(value = "bpVersion") bpVersion: String,
67 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
68 @RequestParam(value = "artifactName") artifactName: String,
69 @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
70 @RequestParam(value = "resolutionKey") resolutionKey: String,
71 @ApiParam(value = "Resource Type associated with the resolution.", required = false)
72 @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
73 @ApiParam(value = "Resource Id associated with the resolution.", required = false)
74 @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String,
75 @ApiParam(value = "Expected format of the template being retrieved.",
76 defaultValue = MediaType.TEXT_PLAIN_VALUE,
78 @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
79 : ResponseEntity<String> = runBlocking {
83 if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
84 throw ResolutionException("Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
85 } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
86 result = templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
91 } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
93 templateResolutionService.findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
100 throw ResolutionException("Missing param. Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
104 var expectedContentType = format
105 if (expectedContentType.indexOf('/') < 0) {
106 expectedContentType = "application/$expectedContentType"
108 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
110 ResponseEntity.ok().contentType(expectedMediaType).body(result)
114 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
115 @ApiOperation(value = "Store a resolved template w/ resolution-key",
116 notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
117 "artifact name and resolution key.",
118 response = TemplateResolution::class,
119 produces = MediaType.APPLICATION_JSON_VALUE)
121 @PreAuthorize("hasRole('USER')")
122 fun postWithResolutionKey(
123 @ApiParam(value = "Name of the CBA.", required = true)
124 @PathVariable(value = "bpName") bpName: String,
125 @ApiParam(value = "Version of the CBA.", required = true)
126 @PathVariable(value = "bpVersion") bpVersion: String,
127 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
128 @PathVariable(value = "artifactName") artifactName: String,
129 @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
130 @PathVariable(value = "resolutionKey") resolutionKey: String,
131 @ApiParam(value = "Template to store.", required = true)
132 @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
135 templateResolutionService.write(bpName, bpVersion, artifactName, result, resolutionKey = resolutionKey)
137 ResponseEntity.ok().body(resultStored)
140 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resourceType}/{resourceId}",
141 produces = [MediaType.APPLICATION_JSON_VALUE])
142 @ApiOperation(value = "Store a resolved template w/ resourceId and resourceType",
143 notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
144 "artifact name, resourceId and resourceType.",
145 response = TemplateResolution::class,
146 produces = MediaType.APPLICATION_JSON_VALUE)
148 @PreAuthorize("hasRole('USER')")
149 fun postWithResourceIdAndResourceType(
150 @ApiParam(value = "Name of the CBA.", required = true)
151 @PathVariable(value = "bpName") bpName: String,
152 @ApiParam(value = "Version of the CBA.", required = true)
153 @PathVariable(value = "bpVersion") bpVersion: String,
154 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
155 @PathVariable(value = "artifactName") artifactName: String,
156 @ApiParam(value = "Resource Type associated with the resolution.", required = false)
157 @PathVariable(value = "resourceType", required = true) resourceType: String,
158 @ApiParam(value = "Resource Id associated with the resolution.", required = false)
159 @PathVariable(value = "resourceId", required = true) resourceId: String,
160 @ApiParam(value = "Template to store.", required = true)
161 @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
164 templateResolutionService.write(bpName, bpVersion, artifactName, result, resourceId = resourceId, resourceType = resourceType)
166 ResponseEntity.ok().body(resultStored)