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