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.*
33 * Exposes Resource Configuration Snapshot API to store and retrieve stored resource configurations.
35 * @author Serge Simard
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) {
44 @RequestMapping(path = ["/health-check"],
45 method = [RequestMethod.GET],
46 produces = [MediaType.APPLICATION_JSON_VALUE])
48 @ApiOperation(value = "Health Check", hidden = true)
49 fun ressCfgSnapshotControllerHealthCheck(): JsonNode = runBlocking {
50 "Success".asJsonPrimitive()
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.")
60 @PreAuthorize("hasRole('USER')")
62 @ApiParam(value = "Resource Type associated of the resource configuration snapshot.", required = false)
63 @RequestParam(value = "resourceType", required = true) resourceType: String,
65 @ApiParam(value = "Resource Id associated of the resource configuration snapshot.", required = false)
66 @RequestParam(value = "resourceId", required = true) resourceId: String,
68 @ApiParam(value = "Status of the snapshot being retrieved.", defaultValue = "RUNNING", required = false)
69 @RequestParam(value = "status", required = false, defaultValue = "RUNNING") status: String,
71 @ApiParam(value = "Expected format of the snapshot being retrieved.", defaultValue = MediaType.TEXT_PLAIN_VALUE,
73 @RequestParam(value = "format", required = false, defaultValue = MediaType.TEXT_PLAIN_VALUE) format: String)
75 : ResponseEntity<String> = runBlocking {
77 var configSnapshot = ""
79 if (resourceType.isNotEmpty() && resourceId.isNotEmpty()) {
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")
88 throw IllegalArgumentException("Missing param. You must specify resource-id and resource-type.")
91 var expectedContentType = format
92 if (expectedContentType.indexOf('/') < 0) {
93 expectedContentType = "application/$expectedContentType"
95 val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType)
97 ResponseEntity.ok().contentType(expectedMediaType).body(configSnapshot)
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)
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 {
119 resourceConfigSnapshotService.write(snapshot, resourceId, resourceType,
120 ResourceConfigSnapshot.Status.valueOf(status.toUpperCase()))
122 ResponseEntity.ok().body(resultStored)