diff options
author | Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com> | 2019-03-25 17:01:08 -0400 |
---|---|---|
committer | Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com> | 2019-03-27 13:23:06 -0400 |
commit | c3811effed948926f8d94615df7530c99d490092 (patch) | |
tree | dff68af7c67bc9db332fa43b7a882d1a1b05b4db /ms/controllerblueprints/modules/service/src/main/kotlin | |
parent | 10164a7ab851859bfd548e32b4fe3c0610f3d614 (diff) |
Improve blueprint save
Change-Id: Ibac2ef9cd7e217db809a6a695ea0ee39a6bd2e21
Issue-ID: CCSDK-1137
Signed-off-by: Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/service/src/main/kotlin')
7 files changed, 124 insertions, 40 deletions
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 new file mode 100644 index 000000000..0ae618f3a --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/BluePrintCatalogLoadService.kt @@ -0,0 +1,69 @@ +/* + * Copyright © 2017-2018 AT&T 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.load + +import com.att.eelf.configuration.EELFManager +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +import org.apache.commons.lang3.text.StrBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService +import org.springframework.stereotype.Service +import java.io.File +import java.util.* + +@Service +open class BluePrintCatalogLoadService(private val bluePrintCatalogService: BluePrintCatalogService) { + + private val log = EELFManager.getInstance().getLogger(BluePrintCatalogLoadService::class.java) + + open fun loadPathsBluePrintModelCatalog(paths: List<String>) { + paths.forEach { loadPathBluePrintModelCatalog(it) } + } + + open fun loadPathBluePrintModelCatalog(path: String) { + + val files = File(path).listFiles() + runBlocking { + val errorBuilder = StrBuilder() + val deferredResults = mutableListOf<Deferred<Unit>>() + + for (file in files) { + deferredResults += async { + loadBluePrintModelCatalog(errorBuilder, file) + } + } + + for (deferredResult in deferredResults) { + deferredResult.await() + } + + if (!errorBuilder.isEmpty) { + log.error(errorBuilder.toString()) + } + } + } + + open suspend fun loadBluePrintModelCatalog(errorBuilder: StrBuilder, file: File) { + try { + bluePrintCatalogService.saveToDatabase(UUID.randomUUID().toString(), file) + } catch (e: Exception) { + errorBuilder.appendln("Couldn't load BlueprintModel(${file.name}: ${e.message}") + } + } + +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/ControllerBluePrintCoreConfiguration.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/ControllerBluePrintCoreConfiguration.kt index 8a7c01851..790c61ebc 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/ControllerBluePrintCoreConfiguration.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/ControllerBluePrintCoreConfiguration.kt @@ -34,7 +34,7 @@ open class ControllerBluePrintCoreConfiguration(private val bluePrintProperties: } @Bean - open fun controlelrBlueprintLoadConfiguration(): BluePrintLoadConfiguration { + open fun bluePrintLoadConfiguration(): BluePrintLoadConfiguration { return bluePrintProperties .propertyBeanType(PREFIX_BLUEPRINT_LOAD_CONFIGURATION, BluePrintLoadConfiguration::class.java) } @@ -53,7 +53,7 @@ open class ControllerBlueprintPropertyConfiguration { } @Service -open class ControllerBlueprintProperties(var bluePrintPropertyBinder: Binder) { +open class ControllerBlueprintProperties(private var bluePrintPropertyBinder: Binder) { fun <T> propertyBeanType(prefix: String, type: Class<T>): T { return bluePrintPropertyBinder.bind(prefix, Bindable.of(type)).get() } diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/controller/BlueprintModelController.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/controller/BlueprintModelController.kt index 0ea753fd1..a214f961e 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/controller/BlueprintModelController.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/controller/BlueprintModelController.kt @@ -26,7 +26,6 @@ import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.http.codec.multipart.FilePart import org.springframework.web.bind.annotation.* -import reactor.core.publisher.Mono /** * BlueprintModelController Purpose: Handle controllerBlueprint API request @@ -41,8 +40,8 @@ open class BlueprintModelController(private val bluePrintModelHandler: BluePrint @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) @ResponseBody @Throws(BluePrintException::class) - fun saveBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> { - return bluePrintModelHandler.saveBlueprintModel(file) + fun saveBlueprint(@RequestPart("file") filePart: FilePart): BlueprintModelSearch = runBlocking { + bluePrintModelHandler.saveBlueprintModel(filePart) } @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE]) diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/handler/BluePrintModelHandler.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/handler/BluePrintModelHandler.kt index 72c27adc1..c54bf87fe 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/handler/BluePrintModelHandler.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/handler/BluePrintModelHandler.kt @@ -18,19 +18,18 @@ package org.onap.ccsdk.cds.controllerblueprints.service.handler -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintEnhancerService -import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName -import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModel import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelRepository import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository import org.onap.ccsdk.cds.controllerblueprints.service.utils.BluePrintEnhancerUtils +import org.slf4j.LoggerFactory import org.springframework.core.io.ByteArrayResource import org.springframework.core.io.Resource import org.springframework.http.HttpHeaders @@ -39,7 +38,6 @@ import org.springframework.http.ResponseEntity import org.springframework.http.codec.multipart.FilePart import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional -import reactor.core.publisher.Mono import java.io.File import java.io.IOException import java.util.* @@ -59,6 +57,8 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository, private val bluePrintEnhancerService: BluePrintEnhancerService) { + private val log = LoggerFactory.getLogger(BluePrintModelHandler::class.java)!! + /** * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database * @@ -76,23 +76,28 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC * @throws BluePrintException BluePrintException </BlueprintModelSearch> */ @Throws(BluePrintException::class) - open fun saveBlueprintModel(filePart: FilePart): Mono<BlueprintModelSearch> { + open suspend fun saveBlueprintModel(filePart: FilePart): BlueprintModelSearch { + val saveId = UUID.randomUUID().toString() + val blueprintArchive = normalizedPathName(bluePrintLoadConfiguration.blueprintArchivePath, saveId) try { - val cbaLocation = BluePrintFileUtils.getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath) - return BluePrintEnhancerUtils.saveCBAFile(filePart, cbaLocation).map { fileName -> - var blueprintId: String? = null - try { - blueprintId = bluePrintCatalogService.saveToDatabase(cbaLocation.resolve(fileName).toFile(), false) - } catch (e: BluePrintException) { - // FIXME handle expection - } - blueprintModelSearchRepository.findById(blueprintId!!).get() - } + //Recreate the Dir + normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath, saveId).reCreateDirs() + val deCompressedFile = normalizedFile(blueprintArchive, "cba.zip") + // Copy the File Part to Local File + BluePrintEnhancerUtils.copyFromFilePart(filePart, deCompressedFile) + // Save the Copied file to Database + val blueprintId = bluePrintCatalogService.saveToDatabase(saveId, deCompressedFile, false) + // Check and Return the Saved File + val blueprintModelSearch = blueprintModelSearchRepository.findById(blueprintId).get() + log.info("Save($saveId) successful for blueprint(${blueprintModelSearch.artifactName}) " + + "version(${blueprintModelSearch.artifactVersion})") + return blueprintModelSearch } catch (e: IOException) { throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value, - String.format("I/O Error while uploading the CBA file: %s", e.message), e) + "Error in Save CBA: ${e.message}", e) + } finally { + deleteDir(blueprintArchive) } - } @@ -277,20 +282,20 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC open suspend fun enrichBlueprint(filePart: FilePart): ResponseEntity<Resource> { val enhanceId = UUID.randomUUID().toString() val blueprintArchive = normalizedPathName(bluePrintLoadConfiguration.blueprintArchivePath, enhanceId) - val blueprintEnrichmentDir = normalizedPathName(bluePrintLoadConfiguration.blueprintEnrichmentPath, enhanceId) + val blueprintWorkingDir = normalizedPathName(bluePrintLoadConfiguration.blueprintWorkingPath, enhanceId) try { - BluePrintEnhancerUtils.decompressFilePart(filePart, blueprintArchive, blueprintEnrichmentDir) + BluePrintEnhancerUtils.decompressFilePart(filePart, blueprintArchive, blueprintWorkingDir) // Enhance the Blue Prints - bluePrintEnhancerService.enhance(blueprintEnrichmentDir) + bluePrintEnhancerService.enhance(blueprintWorkingDir) - return BluePrintEnhancerUtils.compressToFilePart(blueprintEnrichmentDir, blueprintArchive) + return BluePrintEnhancerUtils.compressToFilePart(blueprintWorkingDir, blueprintArchive) } catch (e: IOException) { throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value, "Error in Enriching CBA: ${e.message}", e) } finally { - BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintEnrichmentDir) + BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintWorkingDir) } } @@ -305,12 +310,12 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC open suspend fun publishBlueprint(filePart: FilePart): BlueprintModelSearch { val publishId = UUID.randomUUID().toString() val blueprintArchive = bluePrintLoadConfiguration.blueprintArchivePath.plus(File.separator).plus(publishId) - val blueprintEnrichmentDir = bluePrintLoadConfiguration.blueprintEnrichmentPath.plus(File.separator).plus(publishId) + val blueprintWorkingDir = bluePrintLoadConfiguration.blueprintWorkingPath.plus(File.separator).plus(publishId) try { val compressedFilePart = BluePrintEnhancerUtils - .extractCompressFilePart(filePart, blueprintArchive, blueprintEnrichmentDir) + .extractCompressFilePart(filePart, blueprintArchive, blueprintWorkingDir) - val blueprintId = bluePrintCatalogService.saveToDatabase(compressedFilePart, true) + val blueprintId = bluePrintCatalogService.saveToDatabase(publishId, compressedFilePart, true) return blueprintModelSearchRepository.findById(blueprintId).get() @@ -318,7 +323,7 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value, "Error in Publishing CBA: ${e.message}", e) } finally { - BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintEnrichmentDir) + BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintWorkingDir) } } diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/BluePrintCatalogLoadService.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/BluePrintCatalogLoadService.kt index eca7ce1bf..2278d8019 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/BluePrintCatalogLoadService.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/BluePrintCatalogLoadService.kt @@ -22,6 +22,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogS import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile import org.springframework.stereotype.Service import java.io.File +import java.util.* @Service open class BluePrintCatalogLoadService(private val bluePrintCatalogService: BluePrintCatalogService) { @@ -47,7 +48,7 @@ open class BluePrintCatalogLoadService(private val bluePrintCatalogService: Blue open suspend fun loadBluePrintModelCatalog(errorBuilder: MutableList<String>, file: File) { try { log.info("loading blueprint cba(${file.absolutePath})") - bluePrintCatalogService.saveToDatabase(file) + bluePrintCatalogService.saveToDatabase(UUID.randomUUID().toString(), file) } catch (e: Exception) { errorBuilder.add("Couldn't load BlueprintModel(${file.name}: ${e.message}") } diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt index 358a4654a..3d6e134d4 100755 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt @@ -23,7 +23,9 @@ import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode +import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService +import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPath import org.onap.ccsdk.cds.controllerblueprints.db.resources.BlueprintCatalogServiceImpl import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModel import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelContent @@ -34,7 +36,7 @@ import org.springframework.stereotype.Service import java.io.File import java.nio.file.Files import java.nio.file.Path -import java.nio.file.Paths +import java.util.* /** * Similar implementation in [org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl] @@ -43,7 +45,7 @@ import java.nio.file.Paths class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService, private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, private val blueprintModelRepository: ControllerBlueprintModelRepository) - : BlueprintCatalogServiceImpl(bluePrintValidatorService) { + : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintValidatorService) { private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString()) @@ -52,13 +54,18 @@ class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrint log.info("BlueprintProcessorCatalogServiceImpl initialized") } - override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version) + override suspend fun delete(name: String, version: String) { + // Cleaning Deployed Blueprint + deleteDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version) + // Cleaning Data Base + blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version) + } - override fun get(name: String, version: String, extract: Boolean): Path? { + override suspend fun get(name: String, version: String, extract: Boolean): Path? { val path = if (extract) { - Paths.get("${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version") + normalizedPath(bluePrintLoadConfiguration.blueprintDeployPath, name, version) } else { - Paths.get("${bluePrintLoadConfiguration.blueprintArchivePath}/$name/$version.zip") + normalizedPath(bluePrintLoadConfiguration.blueprintArchivePath, UUID.randomUUID().toString(), "cba.zip") } blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also { it.blueprintModelContent.run { @@ -70,11 +77,14 @@ class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrint return null } - override fun save(metadata: MutableMap<String, String>, archiveFile: File) { + override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) { val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] + check(archiveFile.isFile && !archiveFile.isDirectory) { + throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})") + } blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let { log.info("Overwriting blueprint model :$artifactName::$artifactVersion") diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt index 166a2b283..d4753e194 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt @@ -84,7 +84,7 @@ class BluePrintEnhancerUtils { return artifactType } - private suspend fun copyFromFilePart(filePart: FilePart, targetFile: File): File { + suspend fun copyFromFilePart(filePart: FilePart, targetFile: File): File { // Delete the Directory targetFile.deleteRecursively() return filePart.transferTo(targetFile) |