d3ebd5c49b82f21111aa54257e1fd53a691cb80d
[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 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.*
27
28 /**
29  * Exposes Template Resolution API to store and retrieve rendered template results.
30  *
31  * @author Serge Simard
32  * @version 1.0
33  */
34 @RestController
35 @RequestMapping("/api/v1/template")
36 open class TemplateController(private val templateResolutionService: TemplateResolutionService) {
37
38     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
39     @ResponseBody
40     fun ping(): String = runBlocking {
41         "Success"
42     }
43
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")
49     @ResponseBody
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 {
57
58         val result = templateResolutionService.read(bpName, bpVersion, artifactName, resolutionKey)
59
60         var expectedContentType = format
61         if (expectedContentType.indexOf('/') < 0) {
62             expectedContentType = "application/$expectedContentType"
63         }
64         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
65
66         ResponseEntity.ok().contentType(expectedMediaType).body(result)
67     }
68
69
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.")
74     @ResponseBody
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 {
81
82         val resultStored =
83             templateResolutionService.write(bpName, bpVersion, resolutionKey, artifactName, result)
84
85         ResponseEntity.ok().body(resultStored)
86     }
87 }