From 20dcdbc1384a1173d7e63dd666d530908c845a1e Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Fri, 6 Mar 2020 13:04:37 -0500 Subject: Refactoring BP Code with ErrorCatalog Issue-ID: CCSDK-2124 Signed-off-by: Steve Siani Change-Id: Ief468a56f9e7b3ef86c357965aa7b15f0a4cfa22 --- .../configs/api/ErrorHandling.kt | 36 ++++++++ .../api/ResourceConfigSnapshotController.kt | 16 +++- .../configs/api/ResourceConfigSnapshotException.kt | 20 ---- .../api/ResourceConfigSnapshotExceptionHandler.kt | 102 ++------------------- 4 files changed, 54 insertions(+), 120 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ErrorHandling.kt delete mode 100644 ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotException.kt (limited to 'ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap') diff --git a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ErrorHandling.kt b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ErrorHandling.kt new file mode 100644 index 000000000..e742e6375 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ErrorHandling.kt @@ -0,0 +1,36 @@ +/* + * Copyright © 2018-2019 AT&T 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.configs.api + +object ConfigsApiDomains { + // ConfigsApi Domains Constants + const val CONFIGS_API = "org.onap.ccsdk.cds.blueprintsprocessor.configs.api" +} + +object ConfigsApiHttpErrorCodes { + init { + // Register HttpErrorCodes + // HttpErrorCodes.register("", 200) + } +} + +object ConfigsGrpcErrorCodes { + init { + // Register GrpcErrorCodes + // GrpcErrorCodes.register("", 3) + } +} diff --git a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotController.kt b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotController.kt index 0b18fb01f..2a5f4c3ea 100644 --- a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotController.kt +++ b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotController.kt @@ -24,6 +24,9 @@ import kotlinx.coroutines.runBlocking import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshot import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshotService import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException +import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes +import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize @@ -101,12 +104,17 @@ open class ResourceConfigSnapshotController(private val resourceConfigSnapshotSe resourceType, ResourceConfigSnapshot.Status.valueOf(status.toUpperCase()) ) } catch (ex: NoSuchElementException) { - throw ResourceConfigSnapshotException( - "Could not find configuration snapshot entry for type $resourceType and Id $resourceId" - ) + throw httpProcessorException(ErrorCatalogCodes.RESOURCE_NOT_FOUND, ConfigsApiDomains.CONFIGS_API, + "Could not find configuration snapshot entry for type $resourceType and Id $resourceId", + ex.errorCauseOrDefault()) + } catch (ex: Exception) { + throw httpProcessorException(ErrorCatalogCodes.INVALID_REQUEST_FORMAT, ConfigsApiDomains.CONFIGS_API, + "Could not find configuration snapshot entry for type $resourceType and Id $resourceId", + ex.errorCauseOrDefault()) } } else { - throw IllegalArgumentException("Missing param. You must specify resource-id and resource-type.") + throw httpProcessorException(ErrorCatalogCodes.INVALID_REQUEST_FORMAT, ConfigsApiDomains.CONFIGS_API, + "Missing param. You must specify resource-id and resource-type.") } var expectedContentType = format diff --git a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotException.kt b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotException.kt deleted file mode 100644 index 1eeea9893..000000000 --- a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotException.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) 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.configs.api - -class ResourceConfigSnapshotException(message: String) : RuntimeException(message) { - var code: Int = 404 -} diff --git a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotExceptionHandler.kt b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotExceptionHandler.kt index d4c31e780..2b9bb31f6 100644 --- a/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotExceptionHandler.kt +++ b/ms/blueprintsprocessor/modules/inbounds/configs-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotExceptionHandler.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Bell Canada + * Copyright © 2019 - 2020 IBM, Bell Canada * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,106 +16,16 @@ package org.onap.ccsdk.cds.blueprintsprocessor.configs.api -import com.fasterxml.jackson.annotation.JsonFormat -import com.fasterxml.jackson.annotation.JsonInclude -import com.fasterxml.jackson.annotation.JsonTypeInfo -import com.fasterxml.jackson.annotation.JsonTypeName -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException -import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode -import org.slf4j.LoggerFactory -import org.springframework.dao.EmptyResultDataAccessException -import org.springframework.dao.IncorrectResultSizeDataAccessException -import org.springframework.http.HttpStatus -import org.springframework.http.ResponseEntity -import org.springframework.orm.jpa.JpaObjectRetrievalFailureException -import org.springframework.web.bind.annotation.ExceptionHandler +import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogExceptionHandler +import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogService import org.springframework.web.bind.annotation.RestControllerAdvice -import org.springframework.web.server.ServerWebInputException -import java.io.Serializable -import java.util.Date /** * Handle exceptions in ResourceConfigSnapshot API and provide relevant HTTP status codes and messages * * @author Serge Simard - * @version 1.0 + * @version 2.0 */ @RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.configs.api") -open class ResourceConfigSnapshotExceptionHandler { - - private val log = LoggerFactory.getLogger(ResourceConfigSnapshotExceptionHandler::class.toString()) - - private val debugMsg = "Resource_Config_Snapshot_ExceptionHandler_Error_Message" - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: BluePrintProcessorException): ResponseEntity { - val errorCode = ErrorCode.BLUEPRINT_PATH_MISSING - return returnError(e, errorCode) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: ServerWebInputException): ResponseEntity { - val errorCode = ErrorCode.INVALID_REQUEST_FORMAT - return returnError(e, errorCode, false) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: IllegalArgumentException): ResponseEntity { - val errorCode = ErrorCode.INVALID_REQUEST_FORMAT - return returnError(e, errorCode, false) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: IncorrectResultSizeDataAccessException): ResponseEntity { - val errorCode = ErrorCode.DUPLICATE_DATA - return returnError(e, errorCode) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity { - val errorCode = ErrorCode.RESOURCE_NOT_FOUND - return returnError(e, errorCode, false) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity { - val errorCode = ErrorCode.RESOURCE_NOT_FOUND - return returnError(e, errorCode, false) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: Exception): ResponseEntity { - val errorCode = ErrorCode.GENERIC_FAILURE - return returnError(e, errorCode) - } - - @ExceptionHandler - fun resourceConfigSnapshotExceptionHandler(e: ResourceConfigSnapshotException): ResponseEntity { - val errorCode = ErrorCode.RESOURCE_NOT_FOUND - return returnError(e, errorCode, false) - } - - fun returnError(e: Exception, errorCode: ErrorCode, toBeLogged: Boolean = true): ResponseEntity { - if (toBeLogged) { - log.error(e.message, e) - } else { - log.error(e.message) - } - val errorMessage = - ErrorMessage( - errorCode.message(e.message!!), - errorCode.value, - debugMsg - ) - return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)!!) - } -} - -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonTypeName("errorMessage") -@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) -class ErrorMessage(var message: String?, var code: Int?, var debugMessage: String?) : Serializable { - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - var timestamp = Date() -} +open class ResourceConfigSnapshotExceptionHandler(private val errorCatalogService: ErrorCatalogService) : + ErrorCatalogExceptionHandler(errorCatalogService) -- cgit 1.2.3-korg