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.resource.api
 
  19 import io.swagger.annotations.ApiOperation
 
  20 import kotlinx.coroutines.runBlocking
 
  21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
 
  22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
 
  23 import org.springframework.http.MediaType
 
  24 import org.springframework.http.ResponseEntity
 
  25 import org.springframework.security.access.prepost.PreAuthorize
 
  26 import org.springframework.web.bind.annotation.*
 
  29 @RequestMapping("/api/v1/resources")
 
  30 open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) {
 
  32     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
 
  34     fun ping(): String = runBlocking {
 
  38     @RequestMapping(path = [""],
 
  39         method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
 
  40     @ApiOperation(value = "Fetch all resource values associated to a resolution key. ",
 
  41         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name and the resolution-key.",
 
  42         produces = MediaType.APPLICATION_JSON_VALUE)
 
  44     @PreAuthorize("hasRole('USER')")
 
  45     fun getAllFromResolutionKeyOrFromResourceTypeAndId(@RequestParam(value = "bpName", required = true) bpName: String,
 
  46                                 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
 
  47                                 @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String,
 
  48                                 @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String,
 
  49                                 @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String,
 
  50                                 @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String)
 
  51             : ResponseEntity<List<ResourceResolution>> = runBlocking {
 
  53         if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
 
  54             throw ResourceException("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
 
  55         } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) {
 
  57                 .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey))
 
  58         } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()){
 
  60                     .body(resourceResolutionDBService.readWithResourceIdAndResourceType(bpName, bpVersion, resourceId, resourceType))
 
  62             throw ResourceException("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.")
 
  66     @RequestMapping(path = ["/resource"],
 
  67         method = [RequestMethod.GET],
 
  68         produces = [MediaType.APPLICATION_JSON_VALUE])
 
  69     @ApiOperation(value = "Fetch a resource value using resolution key.",
 
  70         notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.",
 
  71         produces = MediaType.APPLICATION_JSON_VALUE)
 
  73     @PreAuthorize("hasRole('USER')")
 
  74     fun getOneFromResolutionKey(@RequestParam(value = "bpName", required = true) bpName: String,
 
  75                                 @RequestParam(value = "bpVersion", required = true) bpVersion: String,
 
  76                                 @RequestParam(value = "artifactName", required = true) artifactName: String,
 
  77                                 @RequestParam(value = "resolutionKey", required = true) resolutionKey: String,
 
  78                                 @RequestParam(value = "name", required = true) name: String)
 
  79             : ResponseEntity<ResourceResolution> = runBlocking {
 
  82             .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))