diff options
author | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-01-12 15:48:20 -0500 |
---|---|---|
committer | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-01-18 13:56:01 -0500 |
commit | c70b934e10fcd07c01d1720b6e5cb1cec3d34732 (patch) | |
tree | d3b1d300dae8ca600a77d6aa1cb9e11e19a13710 /ms | |
parent | 79495ad94899f49c8a637094af785dd9727bbe55 (diff) |
Implement BluePrintCatalogService
Change-Id: Ifcb0d730daec4da747d704c270b72b991e01f474
Issue-ID: CCSDK-908
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
Diffstat (limited to 'ms')
6 files changed, 153 insertions, 134 deletions
diff --git a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt index 881e3bc40..3ba729d1c 100644 --- a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt @@ -17,66 +17,66 @@ package org.onap.ccsdk.apps.controllerblueprints.db.resources -import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService +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.BluePrintFileUtils +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils import java.io.File +import java.nio.file.Path +import java.util.* import javax.persistence.MappedSuperclass @MappedSuperclass -abstract class BlueprintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration) : BluePrintCatalogService { - - override fun uploadToDataBase(file: String, validate: Boolean): String { - // The file name provided here is unique as we transform to UUID before storing - val blueprintFile = File(file) - val fileName = blueprintFile.name - val id = BluePrintFileUtils.stripFileExtension(fileName) - // If the file is directory - if (blueprintFile.isDirectory) { - - val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/$fileName") - // zip the directory - BluePrintArchiveUtils.compress(blueprintFile, zipFile, true) +abstract class BlueprintCatalogServiceImpl(private val blueprintValidator: BluePrintValidatorService) + : BluePrintCatalogService { - // Upload to the Data Base - saveToDataBase(blueprintFile, id, zipFile) + override fun saveToDatabase(blueprintFile: File, validate: Boolean): String { + val extractedDirectory: File + val archivedDirectory: File + val toDeleteDirectory: File + val blueprintId = UUID.randomUUID().toString() - // After Upload to Database delete the zip file - zipFile.delete() + if (blueprintFile.isDirectory) { + extractedDirectory = blueprintFile + archivedDirectory = File(":$blueprintFile.zip") + toDeleteDirectory = archivedDirectory + if (!BluePrintArchiveUtils.compress(blueprintFile, archivedDirectory, true)) { + throw BluePrintException("Fail to compress blueprint") + } } else { - // If the file is ZIP - // unzip the CBA file to validate before store in database - val targetDir = "${bluePrintLoadConfiguration.blueprintDeployPath}/$id/" - val extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir) + val targetDir = "${blueprintFile.parent}/${BluePrintFileUtils.stripFileExtension(blueprintFile.name)}" - // Upload to the Data Base - saveToDataBase(extractedDirectory, id, blueprintFile) + extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir) + archivedDirectory = blueprintFile + toDeleteDirectory = extractedDirectory + } - // After Upload to Database delete the zip file - blueprintFile.delete() - extractedDirectory.delete() + if (validate) { + blueprintValidator.validateBluePrints(extractedDirectory.path) } - return id - } + val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(blueprintId, extractedDirectory.path) + val metadata = bluePrintRuntimeService.bluePrintContext().metadata!! + metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId - override fun downloadFromDataBase(name: String, version: String, path: String): String { - // If path ends with zip, then compress otherwise download as extracted folder + save(metadata, archivedDirectory) - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } + toDeleteDirectory.deleteRecursively() - override fun downloadFromDataBase(uuid: String, path: String): String { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return blueprintId } - override fun prepareBluePrint(name: String, version: String): String { - val preparedPath = "${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version" - downloadFromDataBase(name, version, preparedPath) - return preparedPath - } + override fun getFromDatabase(name: String, version: String, extract: Boolean): Path = get(name, version, extract) + ?: throw BluePrintException("Could not find blueprint $name:$version from database") + + override fun deleteFromDatabase(name: String, version: String) = delete(name, version) + + abstract fun save(metadata: MutableMap<String, String>, archiveFile: File) + abstract fun get(name: String, version: String, extract: Boolean): Path? + abstract fun delete(name: String, version: String) - abstract fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean? = false) }
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelContentRepository.kt b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelContentRepository.kt index 4965677ef..680f1b2ce 100644 --- a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelContentRepository.kt +++ b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelContentRepository.kt @@ -43,25 +43,24 @@ interface ModelContentRepository<T, B> : JpaRepository<B, String> { * * @param blueprintModel blueprintModel * @param contentType contentType - * @return Optional<B> + * @return B? */ - fun findTopByBlueprintModelAndContentType(blueprintModel: T, - contentType: String): Optional<B> + fun findTopByBlueprintModelAndContentType(blueprintModel: T, contentType: String): B? /** * This is a findByBlueprintModelAndContentType method * * @param blueprintModel blueprintModel * @param contentType contentType - * @return Optional<BlueprintModelContent> + * @return List<B> */ fun findByBlueprintModelAndContentType(blueprintModel: T, contentType: String): List<B> /** * This is a findByBlueprintModel method * - * @param blueprintModel B - * @return Optional<T> + * @param blueprintModel T + * @return List<B> */ fun findByBlueprintModel(blueprintModel: T): List<B> @@ -71,15 +70,14 @@ interface ModelContentRepository<T, B> : JpaRepository<B, String> { * @param blueprintModel blueprintModel * @param contentType contentType * @param name name - * @return Optional<B> + * @return B? */ - fun findByBlueprintModelAndContentTypeAndName(blueprintModel: T, - contentType: String, name: String): Optional<B> + fun findByBlueprintModelAndContentTypeAndName(blueprintModel: T, contentType: String, name: String): B? /** * This is a deleteByMdeleteByBlueprintModelodelName method * - * @param blueprintModel B + * @param blueprintModel T */ fun deleteByBlueprintModel(blueprintModel: T) @@ -90,4 +88,4 @@ interface ModelContentRepository<T, B> : JpaRepository<B, String> { */ override fun deleteById(@NotNull id: String) -}
\ No newline at end of file +} diff --git a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelRepository.kt b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelRepository.kt index c31f009a6..e796c3665 100644 --- a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelRepository.kt +++ b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/db/resources/repository/ModelRepository.kt @@ -18,9 +18,10 @@ package org.onap.ccsdk.apps.controllerblueprints.db.resources.repository import org.jetbrains.annotations.NotNull -import java.util.Optional import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.repository.NoRepositoryBean +import java.util.* +import javax.transaction.Transactional /** * @param <T> Model @@ -42,23 +43,23 @@ interface ModelRepository<T> : JpaRepository<T, String> { * * @param artifactName artifactName * @param artifactVersion artifactVersion - * @return Optional<T> + * @return T? */ - fun findByArtifactNameAndArtifactVersion(artifactName: String, artifactVersion: String): Optional<T> + fun findByArtifactNameAndArtifactVersion(artifactName: String, artifactVersion: String): T? /** * This is a findTopByArtifactNameOrderByArtifactIdDesc method * * @param artifactName artifactName - * @return Optional<T> + * @return T? */ - fun findTopByArtifactNameOrderByArtifactVersionDesc(artifactName: String): Optional<T> + fun findTopByArtifactNameOrderByArtifactVersionDesc(artifactName: String): T? /** * This is a findTopByArtifactName method * * @param artifactName artifactName - * @return Optional<T> + * @return List<T> */ fun findTopByArtifactName(artifactName: String): List<T> @@ -66,7 +67,7 @@ interface ModelRepository<T> : JpaRepository<T, String> { * This is a findByTagsContainingIgnoreCase method * * @param tags tags - * @return Optional<ModelType> + * @return List<T> */ fun findByTagsContainingIgnoreCase(tags: String): List<T> @@ -76,6 +77,7 @@ interface ModelRepository<T> : JpaRepository<T, String> { * @param artifactName artifactName * @param artifactVersion artifactVersion */ + @Transactional fun deleteByArtifactNameAndArtifactVersion(artifactName: String, artifactVersion: String) /** @@ -85,4 +87,4 @@ interface ModelRepository<T> : JpaRepository<T, String> { */ override fun deleteById(@NotNull id: String) -} +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java index e80fa8cdc..3cf144f17 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java @@ -17,6 +17,10 @@ package org.onap.ccsdk.apps.controllerblueprints.service;
+import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; import org.jetbrains.annotations.NotNull;
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;
@@ -41,11 +45,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;
-import java.io.IOException;
-import java.nio.file.Path;
-import java.util.List;
-import java.util.Optional;
-
/**
* BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService
*
@@ -73,7 +72,7 @@ public class BlueprintModelService { private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";
private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +
- " and version(%s) from repo";
+ " and version(%s) from repo"; /**
* This is a saveBlueprintModel method
@@ -85,15 +84,20 @@ public class BlueprintModelService { public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {
try {
Path cbaLocation = BluePrintFileUtils.Companion
- .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
+ .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath); return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
- String blueprintId = bluePrintCatalogService
- .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);
+ String blueprintId = null; + try { + blueprintId = bluePrintCatalogService + .saveToDatabase(cbaLocation.toFile(), false); + } catch (BluePrintException e) { + // FIXME handle expection + } return blueprintModelSearchRepository.findById(blueprintId).get();
});
} catch (IOException e) {
throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),
- String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e);
+ String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e); }
}
@@ -136,15 +140,15 @@ public class BlueprintModelService { * @throws BluePrintException BluePrintException
*/
public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException {
+ throws BluePrintException { BlueprintModelSearch blueprintModelSearch;
Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
- .findByArtifactNameAndArtifactVersion(name, version);
+ .findByArtifactNameAndArtifactVersion(name, version); if (dbBlueprintModel.isPresent()) {
blueprintModelSearch = dbBlueprintModel.get();
} else {
throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),
- String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
+ String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)); }
return blueprintModelSearch;
}
@@ -152,19 +156,20 @@ public class BlueprintModelService { /**
* This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
*
- * @param name name
+ * @param name name * @param version version
* @return ResponseEntity<Resource>
* @throws BluePrintException BluePrintException
*/
- public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException {
+ public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, + @NotNull String version) + throws BluePrintException { BlueprintModel blueprintModel;
try {
blueprintModel = getBlueprintModelByNameAndVersion(name, version);
} catch (BluePrintException e) {
throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
- "downloading the CBA file: %s", e.getMessage()), e);
+ "downloading the CBA file: %s", e.getMessage()), e); }
String fileName = blueprintModel.getId() + ".zip";
byte[] file = blueprintModel.getBlueprintModelContent().getContent();
@@ -183,7 +188,7 @@ public class BlueprintModelService { blueprintModel = getBlueprintModel(id);
} catch (BluePrintException e) {
throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
- "downloading the CBA file: %s", e.getMessage()), e);
+ "downloading the CBA file: %s", e.getMessage()), e); }
String fileName = blueprintModel.getId() + ".zip";
byte[] file = blueprintModel.getBlueprintModelContent().getContent();
@@ -191,15 +196,13 @@ public class BlueprintModelService { }
/**
- *
- * @param (fileName, file)
* @return ResponseEntity<Resource>
*/
private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {
return ResponseEntity.ok()
- .contentType(MediaType.parseMediaType("text/plain"))
- .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
- .body(new ByteArrayResource(file));
+ .contentType(MediaType.parseMediaType("text/plain")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") + .body(new ByteArrayResource(file)); }
/**
@@ -224,22 +227,21 @@ public class BlueprintModelService { /**
* This is a getBlueprintModelByNameAndVersion method
*
- * @param name name
+ * @param name name * @param version version
* @return BlueprintModel
* @throws BluePrintException BluePrintException
*/
private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException {
- BlueprintModel blueprintModel;
- Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version);
- if (dbBlueprintModel.isPresent()) {
- blueprintModel = dbBlueprintModel.get();
+ throws BluePrintException { + BlueprintModel blueprintModel = blueprintModelRepository + .findByArtifactNameAndArtifactVersion(name, version); + if (blueprintModel != null) { + return blueprintModel; } else {
String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);
throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
}
- return blueprintModel;
}
/**
diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogLoadService.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogLoadService.kt index d49bcdff5..4fd66ed57 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogLoadService.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogLoadService.kt @@ -59,7 +59,7 @@ open class BluePrintCatalogLoadService(private val bluePrintCatalogService: Blue open fun loadBluePrintModelCatalog(errorBuilder: StrBuilder, file: File) { try { - bluePrintCatalogService.uploadToDataBase(file.absolutePath, true) + bluePrintCatalogService.saveToDatabase(file) } catch (e: Exception) { errorBuilder.appendln("Couldn't load DataType(${file.name}: ${e.message}") } 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 6b367c4db..04071dd20 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 @@ -18,74 +18,91 @@ package org.onap.ccsdk.apps.controllerblueprints.service.load import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException 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.ControllerBlueprintModelRepository +import org.slf4j.LoggerFactory import org.springframework.dao.DataIntegrityViolationException import org.springframework.stereotype.Service import java.io.File import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths /** -Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl] + * Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl] */ @Service -class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration, - private val bluePrintValidatorService: BluePrintValidatorService, +class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService, + private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, private val blueprintModelRepository: ControllerBlueprintModelRepository) - : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) { + : BlueprintCatalogServiceImpl(bluePrintValidatorService) { - override fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean?) { - var valid = false - val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory) - val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem - // Validate Blueprint - val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory) - // Check Validity of blueprint - if (checkValidity!!) { - valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService) - } + private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString()) - if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) { - val metaData = bluePrintRuntimeService.bluePrintContext().metadata!! - val blueprintModel = BlueprintModel() - blueprintModel.id = id - blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL - blueprintModel.published = ApplicationConstants.ACTIVE_N - blueprintModel.artifactName = metaData[BluePrintConstants.METADATA_TEMPLATE_NAME] - blueprintModel.artifactVersion = metaData[BluePrintConstants.METADATA_TEMPLATE_VERSION] - blueprintModel.updatedBy = metaData[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] - blueprintModel.tags = metaData[BluePrintConstants.METADATA_TEMPLATE_TAGS] - blueprintModel.artifactDescription = "Controller Blueprint for ${blueprintModel.artifactName}:${blueprintModel.artifactVersion}" + init { + log.info("BlueprintProcessorCatalogServiceImpl initialized") + } - val blueprintModelContent = BlueprintModelContent() - blueprintModelContent.id = id // For quick access both id's are same.always have one to one mapping. - blueprintModelContent.contentType = "CBA_ZIP" - blueprintModelContent.name = "${blueprintModel.artifactName}:${blueprintModel.artifactVersion}" - blueprintModelContent.description = "(${blueprintModel.artifactName}:${blueprintModel.artifactVersion} CBA Zip Content" - blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath()) + override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version) - // Set the Blueprint Model into blueprintModelContent - blueprintModelContent.blueprintModel = blueprintModel + override fun get(name: String, version: String, extract: Boolean): Path? { + val path = if (extract) { + Paths.get("${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version") + } else { + Paths.get("${bluePrintLoadConfiguration.blueprintArchivePath}/$name/$version.zip") + } + blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also { + it.blueprintModelContent.run { + path.toFile().writeBytes(this!!.content!!).let { + return path + } + } + } + return null + } + + override fun save(metadata: MutableMap<String, String>, archiveFile: File) { - // Set the Blueprint Model Content into blueprintModel - blueprintModel.blueprintModelContent = blueprintModelContent + val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] + val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] - 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) + log.isDebugEnabled.apply { + blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let { + log.debug("Overwriting blueprint model :$artifactName::$artifactVersion") } } + + val blueprintModel = BlueprintModel() + blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] + blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL + blueprintModel.published = ApplicationConstants.ACTIVE_N + blueprintModel.artifactName = artifactName + blueprintModel.artifactVersion = artifactVersion + blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] + blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] + blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion" + + val blueprintModelContent = BlueprintModelContent() + blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] + blueprintModelContent.contentType = "CBA_ZIP" + blueprintModelContent.name = "$artifactName:$artifactVersion" + blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content" + blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath()) + blueprintModelContent.blueprintModel = 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 |