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 io.swagger.annotations.ApiOperation
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolution
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
23 import org.springframework.http.MediaType
24 import org.springframework.http.ResponseEntity
25 import org.springframework.security.access.prepost.PreAuthorize
26 import org.springframework.web.bind.annotation.*
29 * Exposes Template Resolution API to store and retrieve rendered template results.
31 * @author Serge Simard
35 @RequestMapping("/api/v1/template")
36 open class TemplateController(private val templateResolutionService: TemplateResolutionService) {
38 @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
40 fun ping(): String = runBlocking {
44 @RequestMapping(path = [""], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE])
45 @ApiOperation(value = "Retrieve a meshed template.",
46 notes = "Retrieve a meshed template for a given CBA's action, identified by its blueprint name, blueprint version, " +
47 "artifact name and resolution key. And extra 'format' parameter can be passed to tell what content-type" +
48 " to expect in return")
50 @PreAuthorize("hasRole('USER')")
51 fun get(@RequestParam(value = "bpName") bpName: String,
52 @RequestParam(value = "bpVersion") bpVersion: String,
53 @RequestParam(value = "artifactName") artifactName: String,
54 @RequestParam(value = "resolutionKey") resolutionKey: String,
55 @RequestParam(value = "format", required = false, defaultValue = "text/plain") format: String)
56 : ResponseEntity<String> = runBlocking {
58 val result = templateResolutionService.read(bpName, bpVersion, artifactName, resolutionKey)
60 var expectedContentType = format
61 if (expectedContentType.indexOf('/') < 0) {
62 expectedContentType = "application/$expectedContentType"
64 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
66 ResponseEntity.ok().contentType(expectedMediaType).body(result)
70 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
71 @ApiOperation(value = "Store a meshed template",
72 notes = "Store a meshed template for a given CBA's action, identified by its blueprint name, blueprint version, " +
73 "artifact name and resolution key.")
75 @PreAuthorize("hasRole('USER')")
76 fun post(@PathVariable(value = "bpName") bpName: String,
77 @PathVariable(value = "bpVersion") bpVersion: String,
78 @PathVariable(value = "artifactName") artifactName: String,
79 @PathVariable(value = "resolutionKey") resolutionKey: String,
80 @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
83 templateResolutionService.write(bpName, bpVersion, resolutionKey, artifactName, result)
85 ResponseEntity.ok().body(resultStored)