de5843a668531dd420f762f389102b5808ba3354
[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.dao.EmptyResultDataAccessException
28 import org.springframework.http.MediaType
29 import org.springframework.http.ResponseEntity
30 import org.springframework.security.access.prepost.PreAuthorize
31 import org.springframework.web.bind.annotation.*
32
33 /**
34  * Exposes Template Resolution API to store and retrieve rendered template results.
35  *
36  * @author Serge Simard
37  * @version 1.0
38  */
39 @RestController
40 @RequestMapping("/api/v1/template")
41 @Api(value = "/api/v1/template",
42     description = "Interaction with resolved template.")
43 open class TemplateController(private val templateResolutionService: TemplateResolutionService) {
44
45     @RequestMapping(path = ["/health-check"],
46         method = [RequestMethod.GET],
47         produces = [MediaType.APPLICATION_JSON_VALUE])
48     @ResponseBody
49     @ApiOperation(value = "Health Check", hidden = true)
50     fun templateControllerHealthCheck(): JsonNode = runBlocking {
51         JacksonUtils.getJsonNode("Success")
52     }
53
54     @RequestMapping(path = [""],
55         method = [RequestMethod.GET],
56         produces = [MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE])
57     @ApiOperation(value = "Retrieve a resolved template.",
58         notes = "Retrieve a config template for a given CBA's action, identified by its blueprint name, blueprint version, " +
59                 "artifact name and resolution key. An extra 'format' parameter can be passed to tell what content-type" +
60                 " to expect in return")
61     @ResponseBody
62     @PreAuthorize("hasRole('USER')")
63     fun get(
64         @ApiParam(value = "Name of the CBA.", required = true)
65         @RequestParam(value = "bpName") bpName: String,
66         @ApiParam(value = "Version of the CBA.", required = true)
67         @RequestParam(value = "bpVersion") bpVersion: String,
68         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
69         @RequestParam(value = "artifactName") artifactName: String,
70         @ApiParam(value = "Resolution Key associated with the resolution.", required = false)
71         @RequestParam(value = "resolutionKey") resolutionKey: String,
72         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
73         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
74         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
75         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String,
76         @ApiParam(value = "Expected format of the template being retrieved.",
77             defaultValue = MediaType.TEXT_PLAIN_VALUE,
78             required = true)
79         @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
80             : ResponseEntity<String> = runBlocking {
81
82         var result = ""
83
84         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
85             throw ResolutionException("Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
86         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
87             result = templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
88                 bpName,
89                 bpVersion,
90                 artifactName,
91                 resolutionKey)
92         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
93             result =
94                 templateResolutionService.findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
95                     bpName,
96                     bpVersion,
97                     artifactName,
98                     resourceId,
99                     resourceType)
100         } else {
101             throw ResolutionException("Missing param. Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
102         }
103
104
105         var expectedContentType = format
106         if (expectedContentType.indexOf('/') < 0) {
107             expectedContentType = "application/$expectedContentType"
108         }
109         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
110
111         ResponseEntity.ok().contentType(expectedMediaType).body(result)
112     }
113
114
115     @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
116     @ApiOperation(value = "Store a resolved template w/ resolution-key",
117         notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
118                 "artifact name and resolution key.",
119         response = TemplateResolution::class,
120         produces = MediaType.APPLICATION_JSON_VALUE)
121     @ResponseBody
122     @PreAuthorize("hasRole('USER')")
123     fun postWithResolutionKey(
124         @ApiParam(value = "Name of the CBA.", required = true)
125         @PathVariable(value = "bpName") bpName: String,
126         @ApiParam(value = "Version of the CBA.", required = true)
127         @PathVariable(value = "bpVersion") bpVersion: String,
128         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
129         @PathVariable(value = "artifactName") artifactName: String,
130         @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
131         @PathVariable(value = "resolutionKey") resolutionKey: String,
132         @ApiParam(value = "Template to store.", required = true)
133         @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
134
135         val resultStored =
136             templateResolutionService.write(bpName, bpVersion, artifactName, result, resolutionKey = resolutionKey)
137
138         ResponseEntity.ok().body(resultStored)
139     }
140
141     @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resourceType}/{resourceId}",
142         produces = [MediaType.APPLICATION_JSON_VALUE])
143     @ApiOperation(value = "Store a resolved template w/ resourceId and resourceType",
144         notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
145                 "artifact name, resourceId and resourceType.",
146         response = TemplateResolution::class,
147         produces = MediaType.APPLICATION_JSON_VALUE)
148     @ResponseBody
149     @PreAuthorize("hasRole('USER')")
150     fun postWithResourceIdAndResourceType(
151         @ApiParam(value = "Name of the CBA.", required = true)
152         @PathVariable(value = "bpName") bpName: String,
153         @ApiParam(value = "Version of the CBA.", required = true)
154         @PathVariable(value = "bpVersion") bpVersion: String,
155         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
156         @PathVariable(value = "artifactName") artifactName: String,
157         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
158         @PathVariable(value = "resourceType", required = true) resourceType: String,
159         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
160         @PathVariable(value = "resourceId", required = true) resourceId: String,
161         @ApiParam(value = "Template to store.", required = true)
162         @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
163
164         val resultStored =
165             templateResolutionService.write(bpName, bpVersion, artifactName, result, resourceId = resourceId, resourceType = resourceType)
166
167         ResponseEntity.ok().body(resultStored)
168     }
169 }