Merge "Resource Configuration Snapshots Executor and API"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / configs-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / configs / api / ResourceConfigSnapshotController.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.configs.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.config.snapshots.db.ResourceConfigSnapshot
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshotService
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
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 Resource Configuration Snapshot API to store and retrieve stored resource configurations.
34  *
35  * @author Serge Simard
36  * @version 1.0
37  */
38 @RestController
39 @RequestMapping("/api/v1/configs")
40 @Api(value = "/api/v1/configs",
41     description = "Interaction with stored configurations.")
42 open class ResourceConfigSnapshotController(private val resourceConfigSnapshotService: ResourceConfigSnapshotService) {
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 ressCfgSnapshotControllerHealthCheck(): JsonNode = runBlocking {
50         "Success".asJsonPrimitive()
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 resource configuration snapshot.",
57         notes = "Retrieve a config snapshot, identified by its Resource Id and Type. " +
58                 "An extra 'format' parameter can be passed to tell what content-type is expected.")
59     @ResponseBody
60     @PreAuthorize("hasRole('USER')")
61     fun get(
62             @ApiParam(value = "Resource Type associated of the resource configuration snapshot.", required = false)
63         @RequestParam(value = "resourceType", required = true) resourceType: String,
64
65             @ApiParam(value = "Resource Id associated of the resource configuration snapshot.", required = false)
66         @RequestParam(value = "resourceId", required = true) resourceId: String,
67
68             @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = false)
69         @RequestParam(value = "status", required = false, defaultValue = "RUNNING") status: String,
70
71             @ApiParam(value = "Expected format of the snapshot being retrieved.", defaultValue = MediaType.TEXT_PLAIN_VALUE,
72             required = false)
73         @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE)  format: String)
74
75         : ResponseEntity<String> = runBlocking {
76
77         var configSnapshot = ""
78
79         if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
80             try {
81                 configSnapshot = resourceConfigSnapshotService.findByResourceIdAndResourceTypeAndStatus(resourceId,
82                         resourceType, ResourceConfigSnapshot.Status.valueOf(status.toUpperCase()))
83             } catch (ex : NoSuchElementException) {
84                 throw ResourceConfigSnapshotException(
85                         "Could not find configuration snapshot entry for type $resourceType and Id $resourceId")
86             }
87         } else {
88             throw IllegalArgumentException("Missing param. You must specify resource-id and resource-type.")
89         }
90
91         var expectedContentType = format
92         if (expectedContentType.indexOf('/') < 0) {
93             expectedContentType = "application/$expectedContentType"
94         }
95         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
96
97         ResponseEntity.ok().contentType(expectedMediaType).body(configSnapshot)
98     }
99
100     @PostMapping("/{resourceType}/{resourceId}/{status}",
101         produces = [MediaType.APPLICATION_JSON_VALUE])
102     @ApiOperation(value = "Store a resource configuration snapshot identified by resourceId, resourceType, status.",
103         notes = "Store a resource configuration snapshot, identified by its resourceId and resourceType, " +
104                 "and optionally its status, either RUNNING or CANDIDATE.",
105         response = ResourceConfigSnapshot::class, produces = MediaType.APPLICATION_JSON_VALUE)
106     @ResponseBody
107     @PreAuthorize("hasRole('USER')")
108     fun postWithResourceIdAndResourceType(
109         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
110         @PathVariable(value = "resourceType", required = true) resourceType: String,
111         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
112         @PathVariable(value = "resourceId", required = true) resourceId: String,
113         @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = true)
114         @PathVariable(value = "status", required = true) status: String,
115         @ApiParam(value = "Config snapshot to store.", required = true)
116         @RequestBody snapshot: String): ResponseEntity<ResourceConfigSnapshot> = runBlocking {
117
118         val resultStored =
119                 resourceConfigSnapshotService.write(snapshot, resourceId, resourceType,
120                                                     ResourceConfigSnapshot.Status.valueOf(status.toUpperCase()))
121
122         ResponseEntity.ok().body(resultStored)
123     }
124 }