61a9541d397c17cd7ac8731eec93db8091352c8c
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2018-2019 Bell Canada Intellectual Property.
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.resolutionresults.api
18
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.*
26
27 /**
28  * Exposes Resolution Results API to store and retrieve resource resolution results from external processes,
29  * like python or ansible scripts
30  *
31  * @author Serge Simard
32  * @version 1.0
33  */
34 @RestController
35 @RequestMapping("/api/v1/resolution-results")
36 open class ResolutionResultsServiceController {
37
38     @Autowired
39     lateinit var resolutionResultsServiceHandler: ResolutionResultsServiceHandler
40
41     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
42     @ResponseBody
43     fun ping(): String = runBlocking {
44         "Success"
45     }
46
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")
50     @ResponseBody
51     @PreAuthorize("hasRole('USER')")
52     fun getStoredResultById(@PathVariable(value = "resolution_result_id") resolutionResultId: String)
53             : String = runBlocking {
54         resolutionResultsServiceHandler.loadStoredResultById(resolutionResultId)
55     }
56
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")
60     @ResponseBody
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 {
68
69         val payload = resolutionResultsServiceHandler.loadStoredResult(bpName, bpVersion, artifactName, resolutionKey)
70
71         var expectedContentType = format
72         if (expectedContentType.indexOf('/') < 0) {
73             expectedContentType = "application/$expectedContentType"
74         }
75         val expectedMediaType : MediaType = MediaType.valueOf(expectedContentType)
76
77         ResponseEntity.ok().contentType(expectedMediaType).body(payload)
78     }
79
80
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.")
84     @ResponseBody
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
92     }
93
94
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")
98     @ResponseBody
99     @PreAuthorize("hasRole('USER')")
100     fun deleteStoredResult(@PathVariable(value = "resolution_result_id") resolutionResultId: String) = runBlocking {
101         resolutionResultsServiceHandler.removeStoredResultById(resolutionResultId)
102     }
103
104 }