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