2 * Copyright © 2018-2019 Bell Canada Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api
19 import io.swagger.annotations.ApiOperation
20 import kotlinx.coroutines.runBlocking
21 import org.springframework.beans.factory.annotation.Autowired
22 import org.springframework.http.MediaType
23 import org.springframework.http.ResponseEntity
24 import org.springframework.security.access.prepost.PreAuthorize
25 import org.springframework.web.bind.annotation.*
28 * Exposes Resolution Results API to store and retrieve resource resolution results from external processes,
29 * like python or ansible scripts
31 * @author Serge Simard
35 @RequestMapping("/api/v1/resolution-results")
36 open class ResolutionResultsServiceController {
39 lateinit var resolutionResultsServiceHandler: ResolutionResultsServiceHandler
41 @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
43 fun ping(): String = runBlocking {
47 @RequestMapping(path = ["/{resolution_result_id}"], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE])
48 @ApiOperation(value = "Fetch a stored result by ID",
49 notes = "Loads a stored result using the resolution_result_id primary key")
51 @PreAuthorize("hasRole('USER')")
52 fun getStoredResultById(@PathVariable(value = "resolution_result_id") resolutionResultId: String)
53 : String = runBlocking {
54 resolutionResultsServiceHandler.loadStoredResultById(resolutionResultId)
57 @RequestMapping(path = ["/"], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE])
58 @ApiOperation(value = "Fetch a stored result ",
59 notes = "Loads a stored result using the blueprint metadata, artifact name and resolution-key")
61 @PreAuthorize("hasRole('USER')")
62 fun getStoredResult(@RequestParam(value = "bpName") bpName: String,
63 @RequestParam(value = "bpVersion") bpVersion: String,
64 @RequestParam(value = "artifactName") artifactName: String,
65 @RequestParam(value = "resolutionKey") resolutionKey: String,
66 @RequestParam(value = "format", required = false, defaultValue = "text/plain") format: String)
67 : ResponseEntity<String> = runBlocking {
69 val payload = resolutionResultsServiceHandler.loadStoredResult(bpName, bpVersion, artifactName, resolutionKey)
71 var expectedContentType = format
72 if (expectedContentType.indexOf('/') < 0) {
73 expectedContentType = "application/$expectedContentType"
75 val expectedMediaType : MediaType = MediaType.valueOf(expectedContentType)
77 ResponseEntity.ok().contentType(expectedMediaType).body(payload)
81 @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.TEXT_PLAIN_VALUE])
82 @ApiOperation(value = "Store result ",
83 notes = "Store result under resolution-key for the specified blueprint/version/artifact.")
85 @PreAuthorize("hasRole('USER')")
86 fun putStoredResult(@PathVariable(value = "bpName") bpName: String,
87 @PathVariable(value = "bpVersion") bpVersion: String,
88 @PathVariable(value = "artifactName") artifactName: String,
89 @PathVariable(value = "resolutionKey") resolutionKey: String,
90 @RequestBody result : String): String? = runBlocking {
91 resolutionResultsServiceHandler.saveNewStoredResult(bpName, bpVersion, artifactName, resolutionKey, result).id
95 @DeleteMapping(path = ["/{resolution_result_id}"])
96 @ApiOperation(value = "Deletes a stored result by ID",
97 notes = "Removes a stored result, using the resolution_result_id primary key")
99 @PreAuthorize("hasRole('USER')")
100 fun deleteStoredResult(@PathVariable(value = "resolution_result_id") resolutionResultId: String) = runBlocking {
101 resolutionResultsServiceHandler.removeStoredResultById(resolutionResultId)