\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
@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
@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
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
}\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
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
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
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
\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
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
--- /dev/null
+/*
+ * 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
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
@Service
class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration,
private val bluePrintValidatorService: BluePrintValidatorService,
- private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository,
private val blueprintModelRepository: ControllerBlueprintModelRepository)
: BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) {
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
// 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