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 = true)
70 @RequestParam(value = "resolutionKey") resolutionKey: String,
71 @ApiParam(value = "Expected format of the template being retrieved.",
72 defaultValue = MediaType.TEXT_PLAIN_VALUE,
74 @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
75 : ResponseEntity<String> = runBlocking {
77 val result = templateResolutionService.read(bpName, bpVersion, artifactName, resolutionKey)
79 var expectedContentType = format
80 if (expectedContentType.indexOf('/') < 0) {
81 expectedContentType = "application/$expectedContentType"
83 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
85 ResponseEntity.ok().contentType(expectedMediaType).body(result)
89 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
90 @ApiOperation(value = "Store a resolved template",
91 notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
92 "artifact name and resolution key.",
93 response = TemplateResolution::class,
94 produces = MediaType.APPLICATION_JSON_VALUE)
96 @PreAuthorize("hasRole('USER')")
97 fun post(@ApiParam(value = "Name of the CBA.", required = true)
98 @PathVariable(value = "bpName") bpName: String,
99 @ApiParam(value = "Version of the CBA.", required = true)
100 @PathVariable(value = "bpVersion") bpVersion: String,
101 @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
102 @PathVariable(value = "artifactName") artifactName: String,
103 @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
104 @PathVariable(value = "resolutionKey") resolutionKey: String,
105 @ApiParam(value = "Template to store.", required = true)
106 @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
109 templateResolutionService.write(bpName, bpVersion, resolutionKey, artifactName, result)
111 ResponseEntity.ok().body(resultStored)