diff options
author | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-06-20 22:48:12 -0400 |
---|---|---|
committer | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-07-04 15:53:42 -0400 |
commit | c5276ff19122435871c3deedbf0d983962ac2537 (patch) | |
tree | 6a96054ac6863d641c836e14e76fb3fc7f3a1ece /ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap | |
parent | d4b56bc36db1169024fef1f54f924bf3b8f351b6 (diff) |
Refactor resolution controllers
tempalte and resources for get/put API
Change-Id: Ia00bdb08f5f52231481edc6de232f850c66874e9
Issue-ID: CCSDK-1423
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
Diffstat (limited to 'ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap')
6 files changed, 123 insertions, 207 deletions
diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceController.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceController.kt deleted file mode 100644 index 61a9541d3..000000000 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceController.kt +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright © 2018-2019 Bell Canada Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api - -import io.swagger.annotations.ApiOperation -import kotlinx.coroutines.runBlocking -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.http.MediaType -import org.springframework.http.ResponseEntity -import org.springframework.security.access.prepost.PreAuthorize -import org.springframework.web.bind.annotation.* - -/** - * Exposes Resolution Results API to store and retrieve resource resolution results from external processes, - * like python or ansible scripts - * - * @author Serge Simard - * @version 1.0 - */ -@RestController -@RequestMapping("/api/v1/resolution-results") -open class ResolutionResultsServiceController { - - @Autowired - lateinit var resolutionResultsServiceHandler: ResolutionResultsServiceHandler - - @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]) - @ResponseBody - fun ping(): String = runBlocking { - "Success" - } - - @RequestMapping(path = ["/{resolution_result_id}"], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE]) - @ApiOperation(value = "Fetch a stored result by ID", - notes = "Loads a stored result using the resolution_result_id primary key") - @ResponseBody - @PreAuthorize("hasRole('USER')") - fun getStoredResultById(@PathVariable(value = "resolution_result_id") resolutionResultId: String) - : String = runBlocking { - resolutionResultsServiceHandler.loadStoredResultById(resolutionResultId) - } - - @RequestMapping(path = ["/"], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE]) - @ApiOperation(value = "Fetch a stored result ", - notes = "Loads a stored result using the blueprint metadata, artifact name and resolution-key") - @ResponseBody - @PreAuthorize("hasRole('USER')") - fun getStoredResult(@RequestParam(value = "bpName") bpName: String, - @RequestParam(value = "bpVersion") bpVersion: String, - @RequestParam(value = "artifactName") artifactName: String, - @RequestParam(value = "resolutionKey") resolutionKey: String, - @RequestParam(value = "format", required = false, defaultValue = "text/plain") format: String) - : ResponseEntity<String> = runBlocking { - - val payload = resolutionResultsServiceHandler.loadStoredResult(bpName, bpVersion, artifactName, resolutionKey) - - var expectedContentType = format - if (expectedContentType.indexOf('/') < 0) { - expectedContentType = "application/$expectedContentType" - } - val expectedMediaType : MediaType = MediaType.valueOf(expectedContentType) - - ResponseEntity.ok().contentType(expectedMediaType).body(payload) - } - - - @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.TEXT_PLAIN_VALUE]) - @ApiOperation(value = "Store result ", - notes = "Store result under resolution-key for the specified blueprint/version/artifact.") - @ResponseBody - @PreAuthorize("hasRole('USER')") - fun putStoredResult(@PathVariable(value = "bpName") bpName: String, - @PathVariable(value = "bpVersion") bpVersion: String, - @PathVariable(value = "artifactName") artifactName: String, - @PathVariable(value = "resolutionKey") resolutionKey: String, - @RequestBody result : String): String? = runBlocking { - resolutionResultsServiceHandler.saveNewStoredResult(bpName, bpVersion, artifactName, resolutionKey, result).id - } - - - @DeleteMapping(path = ["/{resolution_result_id}"]) - @ApiOperation(value = "Deletes a stored result by ID", - notes = "Removes a stored result, using the resolution_result_id primary key") - @ResponseBody - @PreAuthorize("hasRole('USER')") - fun deleteStoredResult(@PathVariable(value = "resolution_result_id") resolutionResultId: String) = runBlocking { - resolutionResultsServiceHandler.removeStoredResultById(resolutionResultId) - } - -} diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceHandler.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceHandler.kt deleted file mode 100644 index 1fb34d789..000000000 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceHandler.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright © 2018-2019 Bell Canada Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api - -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionResult -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionResultService -import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService -import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils -import org.springframework.stereotype.Service -import java.util.* - -/** - * Process Resolution Results API request to store and retrieve resource resolution results using database acess layer - * ResourceResolutionResultService and corresponding entities - * - * @author Serge Simard - * @version 1.0 - */ -@Service -class ResolutionResultsServiceHandler(private val bluePrintCatalogService: BluePrintCatalogService, - private var resolutionResultService: ResourceResolutionResultService) { - - suspend fun loadStoredResultById(resolutionResultId: String): String { - - return resolutionResultService.readByKey(resolutionResultId) - } - - suspend fun loadStoredResult(blueprintName : String, blueprintVersion : String, artifactTemplate: String, - resolutionKey: String): String { - - val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion) - val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(UUID.randomUUID().toString(), - basePath.toString()) - - return resolutionResultService.read(blueprintRuntimeService, artifactTemplate, resolutionKey) - } - - suspend fun saveNewStoredResult(blueprintName : String, blueprintVersion : String, artifactTemplate: String, - resolutionKey: String, result: String): ResourceResolutionResult { - - val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion) - val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(UUID.randomUUID().toString(), - basePath.toString()) - - val properties = mapOf(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY to resolutionKey) - - val resultStored = resolutionResultService.write(properties, result, blueprintRuntimeService, artifactTemplate) - - return resultStored - } - - suspend fun removeStoredResultById(resolutionResultId: String): Unit { - - return resolutionResultService.deleteByKey(resolutionResultId) - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceController.kt index 40aa1a3e6..c50853ff2 100644 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceController.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api +package org.onap.ccsdk.cds.blueprintsprocessor.resource.api import io.swagger.annotations.ApiOperation import kotlinx.coroutines.runBlocking diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceException.kt index 6815c05ef..19c42f2ea 100644 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceException.kt @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api +package org.onap.ccsdk.cds.blueprintsprocessor.resource.api class ResourceException(message: String) : RuntimeException(message) { var code: Int = 404 } - diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceExceptionHandler.kt index 7f8f7da79..f01dccff7 100644 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceExceptionHandler.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2018-2019 Bell Canada Intellectual Property. + * Copyright © 2019 Bell Canada * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api +package org.onap.ccsdk.cds.blueprintsprocessor.resource.api import com.fasterxml.jackson.annotation.JsonFormat import com.fasterxml.jackson.annotation.JsonInclude @@ -27,6 +27,7 @@ import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.orm.jpa.JpaObjectRetrievalFailureException import org.springframework.dao.EmptyResultDataAccessException +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.web.server.ServerWebInputException import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice @@ -34,57 +35,61 @@ import java.io.Serializable import java.util.* /** - * Handle exceptions in Resolution Results API and provide relevant HTTP status codes and messages + * Handle exceptions in Resolution API and provide relevant HTTP status codes and messages * * @author Serge Simard * @version 1.0 */ -@RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults") -open class ResolutionResultsServiceExceptionHandler { +@RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.resource.api") +open class ResourceExceptionHandler { - private val log = LoggerFactory.getLogger(ResolutionResultsServiceExceptionHandler::class.toString()) + private val log = LoggerFactory.getLogger(ExceptionHandler::class.toString()) - private val debugMsg = "ResolutionResultsService_Error_Message" + private val debugMsg = "Resolution_Service_Error_Message" @ExceptionHandler - fun ResolutionResultsServiceExceptionHandler(e: BluePrintProcessorException): ResponseEntity<ErrorMessage> { - log.error(e.message, e) + fun resolutionResultsServiceExceptionHandler(e: BluePrintProcessorException): ResponseEntity<ErrorMessage> { val errorCode = ErrorCode.BLUEPRINT_PATH_MISSING - val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) + return returnError(e, errorCode) } @ExceptionHandler - fun ResolutionResultsServiceExceptionHandler(e: ServerWebInputException): ResponseEntity<ErrorMessage> { - log.error(e.message, e) + fun resolutionResultsServiceExceptionHandler(e: ServerWebInputException): ResponseEntity<ErrorMessage> { val errorCode = ErrorCode.INVALID_REQUEST_FORMAT - val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) + return returnError(e, errorCode) } @ExceptionHandler - fun ResolutionResultsServiceExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity<ErrorMessage> { - log.error(e.message, e) - var errorCode = ErrorCode.RESOURCE_NOT_FOUND - val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) + fun resolutionResultsServiceExceptionHandler(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorMessage> { + val errorCode = ErrorCode.DUPLICATE_DATA + return returnError(e, errorCode) } @ExceptionHandler - fun ResolutionResultsServiceExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorMessage> { - log.error(e.message, e) + fun resolutionResultsServiceExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity<ErrorMessage> { + val errorCode = ErrorCode.RESOURCE_NOT_FOUND + return returnError(e, errorCode) + } - var errorCode = ErrorCode.RESOURCE_NOT_FOUND - val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) + @ExceptionHandler + fun resolutionResultsServiceExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorMessage> { + val errorCode = ErrorCode.RESOURCE_NOT_FOUND + return returnError(e, errorCode) } @ExceptionHandler - fun ResolutionResultsServiceExceptionHandler(e: Exception): ResponseEntity<ErrorMessage> { + fun resolutionResultsServiceExceptionHandler(e: Exception): ResponseEntity<ErrorMessage> { + val errorCode = ErrorCode.GENERIC_FAILURE + return returnError(e, errorCode) + } + + fun returnError(e: Exception, errorCode: ErrorCode): ResponseEntity<ErrorMessage> { log.error(e.message, e) - var errorCode = ErrorCode.GENERIC_FAILURE - val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) + val errorMessage = + ErrorMessage(errorCode.message(e.message!!), + errorCode.value, + debugMsg) + return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)!!) } @ExceptionHandler diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/TemplateController.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/TemplateController.kt new file mode 100644 index 000000000..d3ebd5c49 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/TemplateController.kt @@ -0,0 +1,87 @@ +/* + * Copyright © 2019 Bell Canada + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.resource.api + +import io.swagger.annotations.ApiOperation +import kotlinx.coroutines.runBlocking +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolution +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.* + +/** + * Exposes Template Resolution API to store and retrieve rendered template results. + * + * @author Serge Simard + * @version 1.0 + */ +@RestController +@RequestMapping("/api/v1/template") +open class TemplateController(private val templateResolutionService: TemplateResolutionService) { + + @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + fun ping(): String = runBlocking { + "Success" + } + + @RequestMapping(path = [""], method = [RequestMethod.GET], produces = [MediaType.TEXT_PLAIN_VALUE]) + @ApiOperation(value = "Retrieve a meshed template.", + notes = "Retrieve a meshed template for a given CBA's action, identified by its blueprint name, blueprint version, " + + "artifact name and resolution key. And extra 'format' parameter can be passed to tell what content-type" + + " to expect in return") + @ResponseBody + @PreAuthorize("hasRole('USER')") + fun get(@RequestParam(value = "bpName") bpName: String, + @RequestParam(value = "bpVersion") bpVersion: String, + @RequestParam(value = "artifactName") artifactName: String, + @RequestParam(value = "resolutionKey") resolutionKey: String, + @RequestParam(value = "format", required = false, defaultValue = "text/plain") format: String) + : ResponseEntity<String> = runBlocking { + + val result = templateResolutionService.read(bpName, bpVersion, artifactName, resolutionKey) + + var expectedContentType = format + if (expectedContentType.indexOf('/') < 0) { + expectedContentType = "application/$expectedContentType" + } + val expectedMediaType: MediaType = MediaType.valueOf(expectedContentType) + + ResponseEntity.ok().contentType(expectedMediaType).body(result) + } + + + @PostMapping("/{bpName}/{bpVersion}/{artifactName}/{resolutionKey}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ApiOperation(value = "Store a meshed template", + notes = "Store a meshed template for a given CBA's action, identified by its blueprint name, blueprint version, " + + "artifact name and resolution key.") + @ResponseBody + @PreAuthorize("hasRole('USER')") + fun post(@PathVariable(value = "bpName") bpName: String, + @PathVariable(value = "bpVersion") bpVersion: String, + @PathVariable(value = "artifactName") artifactName: String, + @PathVariable(value = "resolutionKey") resolutionKey: String, + @RequestBody result: String): ResponseEntity<TemplateResolution> = runBlocking { + + val resultStored = + templateResolutionService.write(bpName, bpVersion, resolutionKey, artifactName, result) + + ResponseEntity.ok().body(resultStored) + } +} |