diff options
author | Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-01-09 14:34:06 -0500 |
---|---|---|
committer | Balazinski <bartosz.balazinski@ibm.com> | 2019-01-11 08:51:07 -0500 |
commit | 83eab67a658e57548f398a7ddd872de4ebab5ca3 (patch) | |
tree | ca47e10091ac397ccb3f1ace727eb2578bc64d74 /ms/controllerblueprints/modules/service/src/main/kotlin | |
parent | 6213aad62019a361126ee218b6b550e539e3582f (diff) |
Blueprint exception handler and REST responses
Change-Id: I5727238cd4c3f3f5475c3f3022e56f1acc0d73bf
Issue-ID: CCSDK-418
Signed-off-by: Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com>
Signed-off-by: Balazinski <bartosz.balazinski@ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/service/src/main/kotlin')
3 files changed, 61 insertions, 5 deletions
diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt new file mode 100644 index 00000000..a0e47d72 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt @@ -0,0 +1,50 @@ +/* + * 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.apps.controllerblueprints.service.controller + +import org.springframework.web.bind.annotation.RestControllerAdvice +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode +import org.onap.ccsdk.apps.controllerblueprints.service.common.ErrorMessage +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ExceptionHandler + +/** + * ControllerBlueprintExceptionHandler.java Purpose: Handle exceptions in controllerBlueprint API and provide the wright + * HTTP code status + * + * @author Vinal Patel + * @version 1.0 + */ +@RestControllerAdvice("org.onap.ccsdk.apps.controllerblueprints") +open class ControllerBlueprintExeptionHandler { + + @ExceptionHandler + fun ControllerBlueprintException(e: BluePrintException): ResponseEntity<ErrorMessage> { + var errorCode = ErrorCode.valueOf(e.code) + val errorMessage = ErrorMessage(errorCode?.message(e.message!!), errorCode?.value, "ControllerBluePrint_Error_Message") + return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode!!.httpCode)) + } + + @ExceptionHandler + fun ControllerBlueprintException(e: Exception): ResponseEntity<ErrorMessage> { + var errorCode = ErrorCode.GENERIC_FAILURE + val errorMessage = ErrorMessage(errorCode?.message(e.message!!), errorCode?.value, "ControllerBluePrint_Error_Message") + return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode!!.httpCode)) + } +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt index ac81f8fa..6b367c4d 100755 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt @@ -20,14 +20,16 @@ package org.onap.ccsdk.apps.controllerblueprints.service.load import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelContent -import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository +import org.springframework.dao.DataIntegrityViolationException import org.springframework.stereotype.Service import java.io.File import java.nio.file.Files @@ -38,7 +40,6 @@ Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintP @Service class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration, private val bluePrintValidatorService: BluePrintValidatorService, - private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository, private val blueprintModelRepository: ControllerBlueprintModelRepository) : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) { @@ -56,7 +57,6 @@ class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrin if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) { val metaData = bluePrintRuntimeService.bluePrintContext().metadata!! - // FIXME("Check Duplicate for Artifact Name and Artifact Version") val blueprintModel = BlueprintModel() blueprintModel.id = id blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL @@ -80,7 +80,12 @@ class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrin // Set the Blueprint Model Content into blueprintModel blueprintModel.blueprintModelContent = blueprintModelContent - blueprintModelRepository.saveAndFlush(blueprintModel) + try { + blueprintModelRepository.saveAndFlush(blueprintModel) + } catch (ex: DataIntegrityViolationException) { + throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " + + "is already exist in database: ${ex.message}", ex) + } } } }
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt index c2d6f6aa..5e715f78 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt @@ -20,6 +20,7 @@ package org.onap.ccsdk.apps.controllerblueprints.service.utils import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType +import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType import org.onap.ccsdk.apps.controllerblueprints.core.data.RelationshipType import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService @@ -92,7 +93,7 @@ class BluePrintEnhancerUtils { // Check if the file's extension is "CBA" if (StringUtils.getFilenameExtension(fileName) != "zip") { - throw BluePrintException("Invalid file extension required ZIP") + throw BluePrintException(ErrorCode.INVALID_FILE_EXTENSION.value, "Invalid file extension required ZIP") } // Change file name to match a pattern |