Blueprint exception handler and REST responses
authorSteve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com>
Wed, 9 Jan 2019 19:34:06 +0000 (14:34 -0500)
committerBalazinski <bartosz.balazinski@ibm.com>
Fri, 11 Jan 2019 13:51:07 +0000 (08:51 -0500)
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>
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/common/ErrorMessage.java
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModel.java
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModelSearch.java
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt [new file with mode: 0644]
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt

index ca0e243..e80fa8c 100644 (file)
 \r
 package org.onap.ccsdk.apps.controllerblueprints.service;\r
 \r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
 import org.jetbrains.annotations.NotNull;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
+import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration;\r
+import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;\r
 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;\r
@@ -56,8 +56,6 @@ import java.util.Optional;
 @Service\r
 public class BlueprintModelService {\r
 \r
-    private static EELFLogger log = EELFManager.getInstance().getLogger(BlueprintModelService.class);\r
-\r
     @Autowired\r
     private BluePrintLoadConfiguration bluePrintLoadConfiguration;\r
 \r
@@ -73,9 +71,9 @@ public class BlueprintModelService {
     @Autowired\r
     private ControllerBlueprintModelContentRepository blueprintModelContentRepository;\r
 \r
-    private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%d) from repo";\r
-    private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%d)" +\r
-        " and version(%d) from repo";\r
+    private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";\r
+    private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +\r
+                                                                    " and version(%s) from repo";\r
 \r
     /**\r
      * This is a saveBlueprintModel method\r
@@ -87,28 +85,36 @@ public class BlueprintModelService {
     public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {\r
         try {\r
             Path cbaLocation = BluePrintFileUtils.Companion\r
-                .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);\r
+                    .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);\r
             return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {\r
                 String blueprintId = bluePrintCatalogService\r
-                    .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);\r
+                        .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);\r
                 return blueprintModelSearchRepository.findById(blueprintId).get();\r
             });\r
-\r
-        } catch (IOException | BluePrintException e) {\r
-            return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));\r
+        } catch (IOException e) {\r
+            throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),\r
+                    String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e);\r
         }\r
     }\r
 \r
     /**\r
-     * This is a publishBlueprintModel method\r
+     * This is a publishBlueprintModel method to change the status published to YES\r
      *\r
      * @param id id\r
      * @return BlueprintModelSearch\r
      * @throws BluePrintException BluePrintException\r
      */\r
     public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {\r
-        // TODO Implement publish Functionality\r
-        return null;\r
+        BlueprintModelSearch blueprintModelSearch;\r
+        Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);\r
+        if (dbBlueprintModel.isPresent()) {\r
+            blueprintModelSearch = dbBlueprintModel.get();\r
+        } else {\r
+            String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
+        }\r
+        blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);\r
+        return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);\r
     }\r
 \r
     /**\r
@@ -122,44 +128,78 @@ public class BlueprintModelService {
     }\r
 \r
     /**\r
-     * This is a getBlueprintModelByNameAndVersion method\r
+     * This is a getBlueprintModelSearchByNameAndVersion method\r
      *\r
      * @param name name\r
      * @param version version\r
      * @return BlueprintModelSearch\r
+     * @throws BluePrintException BluePrintException\r
      */\r
-    public BlueprintModelSearch getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)\r
-        throws BluePrintException {\r
+    public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)\r
+            throws BluePrintException {\r
         BlueprintModelSearch blueprintModelSearch;\r
         Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository\r
-            .findByArtifactNameAndArtifactVersion(name, version);\r
+                .findByArtifactNameAndArtifactVersion(name, version);\r
         if (dbBlueprintModel.isPresent()) {\r
             blueprintModelSearch = dbBlueprintModel.get();\r
         } else {\r
-            throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),\r
+                    String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));\r
         }\r
-\r
         return blueprintModelSearch;\r
     }\r
 \r
     /**\r
-     * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO\r
+     * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version\r
      *\r
+     * @param name    name\r
+     * @param version version\r
      * @return ResponseEntity<Resource>\r
+     * @throws BluePrintException BluePrintException\r
+     */\r
+    public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, @NotNull String version)\r
+            throws BluePrintException {\r
+        BlueprintModel blueprintModel;\r
+        try {\r
+            blueprintModel = getBlueprintModelByNameAndVersion(name, version);\r
+        } catch (BluePrintException e) {\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +\r
+                    "downloading the CBA file: %s", e.getMessage()), e);\r
+        }\r
+        String fileName = blueprintModel.getId() + ".zip";\r
+        byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
+        return prepareResourceEntity(fileName, file);\r
+    }\r
+\r
+    /**\r
+     * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource\r
+     *\r
+     * @return ResponseEntity<Resource>\r
+     * @throws BluePrintException BluePrintException\r
      */\r
     public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {\r
         BlueprintModel blueprintModel;\r
         try {\r
             blueprintModel = getBlueprintModel(id);\r
         } catch (BluePrintException e) {\r
-            throw new BluePrintException("Error uploading the CBA file in channel.", e);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +\r
+                    "downloading the CBA file: %s", e.getMessage()), e);\r
         }\r
         String fileName = blueprintModel.getId() + ".zip";\r
         byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
+        return prepareResourceEntity(fileName, file);\r
+    }\r
+\r
+    /**\r
+     *\r
+     * @param (fileName, file)\r
+     * @return ResponseEntity<Resource>\r
+     */\r
+    private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {\r
         return ResponseEntity.ok()\r
-            .contentType(MediaType.parseMediaType("text/plain"))\r
-            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")\r
-            .body(new ByteArrayResource(file));\r
+                .contentType(MediaType.parseMediaType("text/plain"))\r
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")\r
+                .body(new ByteArrayResource(file));\r
     }\r
 \r
     /**\r
@@ -175,9 +215,30 @@ public class BlueprintModelService {
         if (dbBlueprintModel.isPresent()) {\r
             blueprintModel = dbBlueprintModel.get();\r
         } else {\r
-            throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
+            String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
         }\r
+        return blueprintModel;\r
+    }\r
 \r
+    /**\r
+     * This is a getBlueprintModelByNameAndVersion method\r
+     *\r
+     * @param name    name\r
+     * @param version version\r
+     * @return BlueprintModel\r
+     * @throws BluePrintException BluePrintException\r
+     */\r
+    private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)\r
+            throws BluePrintException {\r
+        BlueprintModel blueprintModel;\r
+        Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version);\r
+        if (dbBlueprintModel.isPresent()) {\r
+            blueprintModel = dbBlueprintModel.get();\r
+        } else {\r
+            String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
+        }\r
         return blueprintModel;\r
     }\r
 \r
@@ -194,7 +255,8 @@ public class BlueprintModelService {
         if (dbBlueprintModel.isPresent()) {\r
             blueprintModelSearch = dbBlueprintModel.get();\r
         } else {\r
-            throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
+            String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
         }\r
 \r
         return blueprintModelSearch;\r
@@ -213,17 +275,17 @@ public class BlueprintModelService {
             blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());\r
             blueprintModelRepository.delete(dbBlueprintModel.get());\r
         } else {\r
-            throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
+            String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
+            throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
         }\r
     }\r
 \r
     /**\r
      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database\r
      *\r
-     * @return List<BlueprintModelSearch> list with the controller blueprint archives\r
+     * @return List<BlueprintModelSearch> list of the controller blueprint archives\r
      */\r
     public List<BlueprintModelSearch> getAllBlueprintModel() {\r
         return blueprintModelSearchRepository.findAll();\r
     }\r
-\r
 }\r
index 4316412..5486262 100644 (file)
@@ -19,11 +19,15 @@ package org.onap.ccsdk.apps.controllerblueprints.service.common;
 import com.fasterxml.jackson.annotation.JsonFormat;\r
 import com.fasterxml.jackson.annotation.JsonInclude;\r
 import com.fasterxml.jackson.annotation.JsonInclude.Include;\r
+import com.fasterxml.jackson.annotation.JsonTypeInfo;\r
+import com.fasterxml.jackson.annotation.JsonTypeName;\r
 \r
 import java.io.Serializable;\r
 import java.util.Date;\r
 \r
 @JsonInclude(Include.NON_NULL)\r
+@JsonTypeName("errorMessage")\r
+@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)\r
 public class ErrorMessage implements Serializable {\r
     private String message;\r
     private Integer code;\r
index 93954da..245e4a8 100755 (executable)
@@ -36,7 +36,7 @@ import java.util.Date;
 \r
 @EntityListeners({AuditingEntityListener.class})\r
 @Entity\r
-@Table(name = "CONFIG_MODEL")\r
+@Table(name = "CONFIG_MODEL", uniqueConstraints=@UniqueConstraint(columnNames={"artifact_name","artifact_version"}))\r
 @Proxy(lazy=false)\r
 public class BlueprintModel implements Serializable {\r
     private static final long serialVersionUID = 1L;\r
index 8b51bce..33753b2 100644 (file)
@@ -18,6 +18,8 @@
 package org.onap.ccsdk.apps.controllerblueprints.service.domain;\r
 \r
 import com.fasterxml.jackson.annotation.JsonFormat;\r
+import com.fasterxml.jackson.annotation.JsonTypeInfo;\r
+import com.fasterxml.jackson.annotation.JsonTypeName;\r
 import org.springframework.data.annotation.LastModifiedDate;\r
 \r
 import javax.persistence.*;\r
@@ -26,6 +28,8 @@ import java.util.Date;
 \r
 @Entity\r
 @Table(name = "CONFIG_MODEL")\r
+@JsonTypeName("blueprintModel")\r
+@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)\r
 public class BlueprintModelSearch implements Serializable {\r
     private static final long serialVersionUID = 1L;\r
 \r
index 9d0b1e3..255137b 100644 (file)
@@ -42,31 +42,38 @@ public class BlueprintModelRest {
 \r
     @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)\r
     public @ResponseBody\r
-    Mono<BlueprintModelSearch> saveBluePrint(@RequestPart("file") FilePart file) throws BluePrintException{\r
+    Mono<BlueprintModelSearch> saveBlueprint(@RequestPart("file") FilePart file) throws BluePrintException{\r
         return blueprintModelService.saveBlueprintModel(file);\r
     }\r
 \r
     @DeleteMapping(path = "/{id}")\r
-    public void deleteBluePrint(@PathVariable(value = "id") String id) throws BluePrintException {\r
+    public void deleteBlueprint(@PathVariable(value = "id") String id) throws BluePrintException {\r
         this.blueprintModelService.deleteBlueprintModel(id);\r
     }\r
 \r
     @GetMapping(path = "/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)\r
     public @ResponseBody\r
-    BlueprintModelSearch getBluePrintByNameAndVersion(@PathVariable(value = "name") String name,\r
-                                                          @PathVariable(value = "version") String version) throws BluePrintException {\r
-        return this.blueprintModelService.getBlueprintModelByNameAndVersion(name, version);\r
+    BlueprintModelSearch getBlueprintByNameAndVersion(@PathVariable(value = "name") String name,\r
+                                                      @PathVariable(value = "version") String version) throws BluePrintException {\r
+        return this.blueprintModelService.getBlueprintModelSearchByNameAndVersion(name, version);\r
+    }\r
+\r
+    @GetMapping(path = "/download/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)\r
+    public @ResponseBody\r
+    ResponseEntity<Resource> downloadBlueprintByNameAndVersion(@PathVariable(value = "name") String name,\r
+                                                               @PathVariable(value = "version") String version) throws BluePrintException {\r
+        return this.blueprintModelService.downloadBlueprintModelFileByNameAndVersion(name, version);\r
     }\r
 \r
     @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)\r
     public @ResponseBody\r
-    BlueprintModelSearch getCBA(@PathVariable(value = "id") String id) throws BluePrintException {\r
+    BlueprintModelSearch getBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {\r
         return this.blueprintModelService.getBlueprintModelSearch(id);\r
     }\r
 \r
     @GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)\r
     public @ResponseBody\r
-    List<BlueprintModelSearch> getAllCBA() {\r
+    List<BlueprintModelSearch> getAllBlueprintModel() {\r
         return this.blueprintModelService.getAllBlueprintModel();\r
     }\r
 \r
@@ -76,7 +83,7 @@ public class BlueprintModelRest {
         return this.blueprintModelService.downloadBlueprintModelFile(id);\r
     }\r
 \r
-    @GetMapping(path = "/publish/{id}", produces = MediaType.APPLICATION_JSON_VALUE)\r
+    @PutMapping(path = "/publish/{id}", produces = MediaType.APPLICATION_JSON_VALUE)\r
     public @ResponseBody\r
     BlueprintModelSearch publishBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {\r
         return this.blueprintModelService.publishBlueprintModel(id);\r
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 (file)
index 0000000..a0e47d7
--- /dev/null
@@ -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
index ac81f8f..6b367c4 100755 (executable)
@@ -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
index c2d6f6a..5e715f7 100644 (file)
@@ -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