0b18fb01f0897686661f2e304aa1fc44e7e85762
[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.PathVariable
31 import org.springframework.web.bind.annotation.PostMapping
32 import org.springframework.web.bind.annotation.RequestBody
33 import org.springframework.web.bind.annotation.RequestMapping
34 import org.springframework.web.bind.annotation.RequestMethod
35 import org.springframework.web.bind.annotation.RequestParam
36 import org.springframework.web.bind.annotation.ResponseBody
37 import org.springframework.web.bind.annotation.RestController
38
39 /**
40  * Exposes Resource Configuration Snapshot API to store and retrieve stored resource configurations.
41  *
42  * @author Serge Simard
43  * @version 1.0
44  */
45 @RestController
46 @RequestMapping("/api/v1/configs")
47 @Api(
48     value = "/api/v1/configs",
49     description = "Interaction with stored configurations."
50 )
51 open class ResourceConfigSnapshotController(private val resourceConfigSnapshotService: ResourceConfigSnapshotService) {
52
53     @RequestMapping(
54         path = ["/health-check"],
55         method = [RequestMethod.GET],
56         produces = [MediaType.APPLICATION_JSON_VALUE]
57     )
58     @ResponseBody
59     @ApiOperation(value = "Health Check", hidden = true)
60     fun ressCfgSnapshotControllerHealthCheck(): JsonNode = runBlocking {
61         "Success".asJsonPrimitive()
62     }
63
64     @RequestMapping(
65         path = [""],
66         method = [RequestMethod.GET],
67         produces = [MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE]
68     )
69     @ApiOperation(
70         value = "Retrieve a resource configuration snapshot.",
71         notes = "Retrieve a config snapshot, identified by its Resource Id and Type. " +
72                 "An extra 'format' parameter can be passed to tell what content-type is expected."
73     )
74     @ResponseBody
75     @PreAuthorize("hasRole('USER')")
76     fun get(
77         @ApiParam(value = "Resource Type associated of the resource configuration snapshot.", required = false)
78         @RequestParam(value = "resourceType", required = true) resourceType: String,
79
80         @ApiParam(value = "Resource Id associated of the resource configuration snapshot.", required = false)
81         @RequestParam(value = "resourceId", required = true) resourceId: String,
82
83         @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = false)
84         @RequestParam(value = "status", required = false, defaultValue = "RUNNING") status: String,
85
86         @ApiParam(
87             value = "Expected format of the snapshot being retrieved.", defaultValue = MediaType.TEXT_PLAIN_VALUE,
88             required = false
89         )
90         @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String
91     ):
92
93             ResponseEntity<String> = runBlocking {
94
95         var configSnapshot = ""
96
97         if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
98             try {
99                 configSnapshot = resourceConfigSnapshotService.findByResourceIdAndResourceTypeAndStatus(
100                     resourceId,
101                     resourceType, ResourceConfigSnapshot.Status.valueOf(status.toUpperCase())
102                 )
103             } catch (ex: NoSuchElementException) {
104                 throw ResourceConfigSnapshotException(
105                     "Could not find configuration snapshot entry for type $resourceType and Id $resourceId"
106                 )
107             }
108         } else {
109             throw IllegalArgumentException("Missing param. You must specify resource-id and resource-type.")
110         }
111
112         var expectedContentType = format
113         if (expectedContentType.indexOf('/') < 0) {
114             expectedContentType = "application/$expectedContentType"
115         }
116         val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
117
118         ResponseEntity.ok().contentType(expectedMediaType).body(configSnapshot)
119     }
120
121     @PostMapping(
122         "/{resourceType}/{resourceId}/{status}",
123         produces = [MediaType.APPLICATION_JSON_VALUE]
124     )
125     @ApiOperation(
126         value = "Store a resource configuration snapshot identified by resourceId, resourceType, status.",
127         notes = "Store a resource configuration snapshot, identified by its resourceId and resourceType, " +
128                 "and optionally its status, either RUNNING or CANDIDATE.",
129         response = ResourceConfigSnapshot::class, produces = MediaType.APPLICATION_JSON_VALUE
130     )
131     @ResponseBody
132     @PreAuthorize("hasRole('USER')")
133     fun postWithResourceIdAndResourceType(
134         @ApiParam(value = "Resource Type associated with the resolution.", required = false)
135         @PathVariable(value = "resourceType", required = true) resourceType: String,
136         @ApiParam(value = "Resource Id associated with the resolution.", required = false)
137         @PathVariable(value = "resourceId", required = true) resourceId: String,
138         @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = true)
139         @PathVariable(value = "status", required = true) status: String,
140         @ApiParam(value = "Config snapshot to store.", required = true)
141         @RequestBody snapshot: String
142     ): ResponseEntity<ResourceConfigSnapshot> = runBlocking {
143
144         val resultStored =
145             resourceConfigSnapshotService.write(
146                 snapshot, resourceId, resourceType,
147                 ResourceConfigSnapshot.Status.valueOf(status.toUpperCase())
148             )
149
150         ResponseEntity.ok().body(resultStored)
151     }
152 }