83e8130799ffd599fb7d183beeb40c0a16e07dee
[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 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.*
31
32 /**
33  * Exposes Template Resolution API to store and retrieve rendered template results.
34  *
35  * @author Serge Simard
36  * @version 1.0
37  */
38 @RestController
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) {
43
44     @RequestMapping(path = ["/health-check"],
45         method = [RequestMethod.GET],
46         produces = [MediaType.APPLICATION_JSON_VALUE])
47     @ResponseBody
48     @ApiOperation(value = "Health Check", hidden = true)
49     fun templateControllerHealthCheck(): JsonNode = runBlocking {
50         JacksonUtils.getJsonNode("Success")
51     }
52
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")
60     @ResponseBody
61     @PreAuthorize("hasRole('USER')")
62     fun get(
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,
73             required = true)
74         @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
75             : ResponseEntity<String> = runBlocking {
76
77         val result = templateResolutionService.read(bpName, bpVersion, artifactName, resolutionKey)
78
79         var expectedContentType = format
80         if (expectedContentType.indexOf('/') < 0) {
81             expectedContentType = "application/$expectedContentType"
82         }
83         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
84
85         ResponseEntity.ok().contentType(expectedMediaType).body(result)
86     }
87
88
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)
95     @ResponseBody
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 {
107
108         val resultStored =
109             templateResolutionService.write(bpName, bpVersion, resolutionKey, artifactName, result)
110
111         ResponseEntity.ok().body(resultStored)
112     }
113 }