Merge "Optimizing Imports and Formatting code"
[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.*
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 = false)
70         @RequestParam(value = "resolutionKey") resolutionKey: String,
71         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
72         @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
73         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
74         @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String,
75         @ApiParam(value = "Expected format of the template being retrieved.",
76             defaultValue = MediaType.TEXT_PLAIN_VALUE,
77             required = true)
78         @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
79             : ResponseEntity<String> = runBlocking {
80
81         var result = ""
82
83         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
84             throw ResolutionException("Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
85         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
86             result = templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
87                 bpName,
88                 bpVersion,
89                 artifactName,
90                 resolutionKey)
91         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
92             result =
93                 templateResolutionService.findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
94                     bpName,
95                     bpVersion,
96                     artifactName,
97                     resourceId,
98                     resourceType)
99         } else {
100             throw ResolutionException("Missing param. Either retrieve resolved template using artifact name and resolution-key OR using resource-id and resource-type.")
101         }
102
103
104         var expectedContentType = format
105         if (expectedContentType.indexOf('/') < 0) {
106             expectedContentType = "application/$expectedContentType"
107         }
108         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
109
110         ResponseEntity.ok().contentType(expectedMediaType).body(result)
111     }
112
113
114     @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
115     @ApiOperation(value = "Store a resolved template w/ resolution-key",
116         notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
117                 "artifact name and resolution key.",
118         response = TemplateResolution::class,
119         produces = MediaType.APPLICATION_JSON_VALUE)
120     @ResponseBody
121     @PreAuthorize("hasRole('USER')")
122     fun postWithResolutionKey(
123         @ApiParam(value = "Name of the CBA.", required = true)
124         @PathVariable(value = "bpName") bpName: String,
125         @ApiParam(value = "Version of the CBA.", required = true)
126         @PathVariable(value = "bpVersion") bpVersion: String,
127         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
128         @PathVariable(value = "artifactName") artifactName: String,
129         @ApiParam(value = "Resolution Key associated with the resolution.", required = true)
130         @PathVariable(value = "resolutionKey") resolutionKey: String,
131         @ApiParam(value = "Template to store.", required = true)
132         @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
133
134         val resultStored =
135             templateResolutionService.write(bpName, bpVersion, artifactName, result, resolutionKey = resolutionKey)
136
137         ResponseEntity.ok().body(resultStored)
138     }
139
140     @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resourceType}/{resourceId}",
141         produces = [MediaType.APPLICATION_JSON_VALUE])
142     @ApiOperation(value = "Store a resolved template w/ resourceId and resourceType",
143         notes = "Store a template for a given CBA's action, identified by its blueprint name, blueprint version, " +
144                 "artifact name, resourceId and resourceType.",
145         response = TemplateResolution::class,
146         produces = MediaType.APPLICATION_JSON_VALUE)
147     @ResponseBody
148     @PreAuthorize("hasRole('USER')")
149     fun postWithResourceIdAndResourceType(
150         @ApiParam(value = "Name of the CBA.", required = true)
151         @PathVariable(value = "bpName") bpName: String,
152         @ApiParam(value = "Version of the CBA.", required = true)
153         @PathVariable(value = "bpVersion") bpVersion: String,
154         @ApiParam(value = "Artifact name for which to retrieve a resolved resource.", required = true)
155         @PathVariable(value = "artifactName") artifactName: String,
156         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
157         @PathVariable(value = "resourceType", required = true) resourceType: String,
158         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
159         @PathVariable(value = "resourceId", required = true) resourceId: String,
160         @ApiParam(value = "Template to store.", required = true)
161         @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking {
162
163         val resultStored =
164             templateResolutionService.write(bpName, bpVersion, artifactName, result, resourceId = resourceId, resourceType = resourceType)
165
166         ResponseEntity.ok().body(resultStored)
167     }
168 }