From ae235a33287834a5541c4914ab7f54e821d9731d Mon Sep 17 00:00:00 2001 From: Steve Alphonse Siani Date: Fri, 28 Dec 2018 14:58:27 -0500 Subject: 1st drop integration with BluePrintCatalogService Change-Id: I0824dcaf0a36e4616c12f3ac53530e6863bee290 Issue-ID: CCSDK-418 Signed-off-by: Steve Alphonse Siani --- .../enhancer/BluePrintPolicyTypeEnhancerImpl.kt | 1 - .../service/load/BluePrintCatalogLoadService.kt | 2 +- .../service/load/BluePrintCatalogServiceImpl.kt | 103 +++++++++++++++++---- .../service/utils/BluePrintEnhancerUtils.kt | 43 +++++++++ 4 files changed, 130 insertions(+), 19 deletions(-) mode change 100644 => 100755 ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogServiceImpl.kt (limited to 'ms/controllerblueprints/modules/service/src/main/kotlin') diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintPolicyTypeEnhancerImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintPolicyTypeEnhancerImpl.kt index 7a9fbb67..80a84fcd 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintPolicyTypeEnhancerImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintPolicyTypeEnhancerImpl.kt @@ -16,7 +16,6 @@ package org.onap.ccsdk.apps.controllerblueprints.service.enhancer -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintError import org.onap.ccsdk.apps.controllerblueprints.core.data.PolicyType import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintPolicyTypeEnhancer import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService 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 07ee0a9b..d49bcdff 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) + bluePrintCatalogService.uploadToDataBase(file.absolutePath, true) } 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/BluePrintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogServiceImpl.kt old mode 100644 new mode 100755 index 8cddbb4c..761dd25a --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogServiceImpl.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 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. @@ -16,39 +17,59 @@ package org.onap.ccsdk.apps.controllerblueprints.service.load +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants 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 org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants +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.BlueprintModelContentRepository +import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelRepository import org.springframework.stereotype.Service import java.io.File -import java.util.* +import java.nio.file.Files @Service class BluePrintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, - private val bluePrintValidatorService: BluePrintValidatorService) : BluePrintCatalogService { + private val bluePrintValidatorService: BluePrintValidatorService, + private val blueprintModelContentRepository: BlueprintModelContentRepository, + private val blueprintModelRepository: BlueprintModelRepository) : BluePrintCatalogService { - override fun uploadToDataBase(file: String): String { - val id = UUID.randomUUID().toString() + 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 bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintFile.absolutePath) - val valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService) - if (valid) { - val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/${id}.zip") - // zip the directory - BluePrintArchiveUtils.compress(blueprintFile, zipFile, true) - - // TODO(Upload to the Data Base) - - // After Upload to Database delete the zip file - zipFile.deleteOnExit() - } + + val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/$fileName") + // zip the directory + BluePrintArchiveUtils.compress(blueprintFile, zipFile, true) + + // Upload to the Data Base + saveToDataBase(blueprintFile, id, zipFile) + + // After Upload to Database delete the zip file + zipFile.delete() + } else { // If the file is ZIP - // TODO(Upload to the Data Base) + // unzip the CBA file to validate before store in database + val targetDir = "${bluePrintLoadConfiguration.blueprintDeployPath}/$id/" + val extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir) + + // Upload to the Data Base + saveToDataBase(extractedDirectory, id, blueprintFile) + + // After Upload to Database delete the zip file + blueprintFile.delete() + extractedDirectory.delete() } + return id } @@ -58,9 +79,57 @@ class BluePrintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePr TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun downloadFromDataBase(uuid: String, path: String): String { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun prepareBluePrint(name: String, version: String): String { val preparedPath = "${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version" downloadFromDataBase(name, version, preparedPath) return preparedPath } + + private fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean? = false) { + // Upload to the Data Base + //val id = "save-$uuid" + 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) + } + + 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 + 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}" + + 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()) + + // Set the Blueprint Model into blueprintModelContent + blueprintModelContent.blueprintModel = blueprintModel + + // Set the Blueprint Model Content into blueprintModel + blueprintModel.blueprintModelContent = blueprintModelContent + + blueprintModelRepository.saveAndFlush(blueprintModel) + } + } } \ 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 801c22a1..2f18abfd 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 @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 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. @@ -23,6 +24,13 @@ import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType import org.onap.ccsdk.apps.controllerblueprints.core.format import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext +import org.springframework.http.codec.multipart.FilePart +import org.springframework.util.StringUtils +import reactor.core.publisher.Mono +import java.io.File +import java.io.IOException +import java.nio.file.Path +import java.util.* class BluePrintEnhancerUtils { @@ -57,5 +65,40 @@ class BluePrintEnhancerUtils { bluePrintContext.serviceTemplate.artifactTypes?.put(artifactTypeName, artifactType) return artifactType } + + /** + * This is a saveCBAFile method + * take a [FilePart], transfer it to disk using a Flux of FilePart and return a [Mono] representing the CBA file name + * + * @param (filePart, targetDirectory) - the request part containing the file to be saved and the default directory where to save + * @return a [Mono] String representing the result of the operation + * @throws (BluePrintException, IOException) BluePrintException, IOException + */ + @Throws(BluePrintException::class, IOException::class) + fun saveCBAFile(filePart: FilePart, targetDirectory: Path): Mono { + + // Normalize file name + val fileName = StringUtils.cleanPath(filePart.filename()) + + // Check if the file's extension is "CBA" + if (StringUtils.getFilenameExtension(fileName) != "zip") { + throw BluePrintException("Invalid file extension required ZIP") + } + + // Change file name to match a pattern + val changedFileName = UUID.randomUUID().toString() + ".zip" + //String changedFileName = BluePrintFileUtils.Companion.getCBAGeneratedFileName(fileName, this.CBA_FILE_NAME_PATTERN); + + // Copy file to the target location (Replacing existing file with the same name) + val targetLocation = targetDirectory.resolve(changedFileName) + + // if a file with the same name already exists in a repository, delete and recreate it + val file = File(targetLocation.toString()) + if (file.exists()) + file.delete() + file.createNewFile() + + return filePart.transferTo(file).thenReturn(changedFileName) + } } } -- cgit 1.2.3-korg