2 * Copyright © 2019 Bell Canada
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.configs.api
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
40 * Exposes Resource Configuration Snapshot API to store and retrieve stored resource configurations.
42 * @author Serge Simard
46 @RequestMapping("/api/v1/configs")
48 value = "/api/v1/configs",
49 description = "Interaction with stored configurations."
51 open class ResourceConfigSnapshotController(private val resourceConfigSnapshotService: ResourceConfigSnapshotService) {
54 path = ["/health-check"],
55 method = [RequestMethod.GET],
56 produces = [MediaType.APPLICATION_JSON_VALUE]
59 @ApiOperation(value = "Health Check", hidden = true)
60 fun ressCfgSnapshotControllerHealthCheck(): JsonNode = runBlocking {
61 "Success".asJsonPrimitive()
66 method = [RequestMethod.GET],
67 produces = [MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE]
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."
75 @PreAuthorize("hasRole('USER')")
77 @ApiParam(value = "Resource Type associated of the resource configuration snapshot.", required = false)
78 @RequestParam(value = "resourceType", required = true) resourceType: String,
80 @ApiParam(value = "Resource Id associated of the resource configuration snapshot.", required = false)
81 @RequestParam(value = "resourceId", required = true) resourceId: String,
83 @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = false)
84 @RequestParam(value = "status", required = false, defaultValue = "RUNNING") status: String,
87 value = "Expected format of the snapshot being retrieved.", defaultValue = MediaType.TEXT_PLAIN_VALUE,
90 @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String
93 ResponseEntity<String> = runBlocking {
95 var configSnapshot = ""
97 if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
99 configSnapshot = resourceConfigSnapshotService.findByResourceIdAndResourceTypeAndStatus(
101 resourceType, ResourceConfigSnapshot.Status.valueOf(status.toUpperCase())
103 } catch (ex: NoSuchElementException) {
104 throw ResourceConfigSnapshotException(
105 "Could not find configuration snapshot entry for type $resourceType and Id $resourceId"
109 throw IllegalArgumentException("Missing param. You must specify resource-id and resource-type.")
112 var expectedContentType = format
113 if (expectedContentType.indexOf('/') < 0) {
114 expectedContentType = "application/$expectedContentType"
116 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
118 ResponseEntity.ok().contentType(expectedMediaType).body(configSnapshot)
122 "/{resourceType}/{resourceId}/{status}",
123 produces = [MediaType.APPLICATION_JSON_VALUE]
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
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 {
145 resourceConfigSnapshotService.write(
146 snapshot, resourceId, resourceType,
147 ResourceConfigSnapshot.Status.valueOf(status.toUpperCase())
150 ResponseEntity.ok().body(resultStored)