From 2960fd72dc68665e4d518713528957ec9528907f Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Wed, 20 Mar 2019 12:43:58 -0400 Subject: Add blueprint enrichment api Change-Id: Idf0f05a9462418a72c716d6b96c121cf223b56eb Issue-ID: CCSDK-1173 Signed-off-by: Muthuramalingam, Brinda Santh --- .../service/controller/BlueprintModelController.kt | 9 ++++ .../service/handler/BluePrintModelHandler.kt | 44 ++++++++++++++- .../service/utils/BluePrintEnhancerUtils.kt | 63 +++++++++++++++++++--- 3 files changed, 108 insertions(+), 8 deletions(-) (limited to 'ms/controllerblueprints/modules/service/src/main') diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelController.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelController.kt index 60c07ad2..d6ce286f 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelController.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelController.kt @@ -17,6 +17,7 @@ package org.onap.ccsdk.apps.controllerblueprints.service.controller +import kotlinx.coroutines.runBlocking import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch import org.onap.ccsdk.apps.controllerblueprints.service.handler.BluePrintModelHandler @@ -86,6 +87,14 @@ open class BlueprintModelController(private val bluePrintModelHandler: BluePrint return this.bluePrintModelHandler.downloadBlueprintModelFile(id) } + @PostMapping("/enrich", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType + .MULTIPART_FORM_DATA_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun enrichBlueprint(@RequestPart("file") file: FilePart): ResponseEntity = runBlocking { + bluePrintModelHandler.enrichBlueprint(file) + } + @PutMapping("/publish/{id}", produces = [MediaType.APPLICATION_JSON_VALUE]) @ResponseBody @Throws(BluePrintException::class) diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt index 907566c3..4239abba 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt @@ -1,6 +1,7 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. * Modifications Copyright © 2019 Bell Canada. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +23,7 @@ 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.data.ErrorCode import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch @@ -38,7 +40,9 @@ 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.* /** * BlueprintModelHandler Purpose: Handler service to handle the request from BlurPrintModelRest @@ -48,10 +52,12 @@ import java.io.IOException */ @Service -open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintCatalogService, private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, +open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintCatalogService, + private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, private val blueprintModelSearchRepository: ControllerBlueprintModelSearchRepository, private val blueprintModelRepository: ControllerBlueprintModelRepository, - private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository) { + private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository, + private val bluePrintEnhancerService: BluePrintEnhancerService) { /** * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database @@ -275,6 +281,40 @@ open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintC } } + /** + * This is a CBA enrichBlueprint method + * Save the Zip File in archive location and extract the cba content. + * Populate the Enhancement Location + * Enhance the CBA content + * Compress the Enhanced Content + * Return back the the compressed content back to the caller. + * + * @param filePart filePart + * @return ResponseEntity + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open suspend fun enrichBlueprint(filePart: FilePart): ResponseEntity { + val enhanceId = UUID.randomUUID().toString() + val blueprintArchive = bluePrintLoadConfiguration.blueprintArchivePath.plus(File.separator).plus(enhanceId) + val blueprintEnrichmentDir = bluePrintLoadConfiguration.blueprintEnrichmentPath.plus(File.separator).plus(enhanceId) + + try { + BluePrintEnhancerUtils.decompressFilePart(filePart, blueprintArchive, blueprintEnrichmentDir) + + // Enhance the Blue Prints + bluePrintEnhancerService.enhance(blueprintEnrichmentDir) + + return BluePrintEnhancerUtils.compressToFilePart(blueprintEnrichmentDir, blueprintArchive) + + } catch (e: IOException) { + throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value, + String.format("I/O Error while uploading the CBA file: %s", e.message), e) + } finally { + BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintEnrichmentDir) + } + } + companion object { private const val BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo" 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 5e715f78..02140ebf 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,6 +1,7 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. * Modifications Copyright © 2019 Bell Canada. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,20 +18,29 @@ package org.onap.ccsdk.apps.controllerblueprints.service.utils +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.reactive.awaitSingle +import kotlinx.coroutines.withContext +import org.apache.commons.io.FileUtils 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.data.* +import org.onap.ccsdk.apps.controllerblueprints.core.deCompress import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService +import org.onap.ccsdk.apps.controllerblueprints.core.reCreateDirs import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils +import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.Resource +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity 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.nio.file.Paths import java.util.* @@ -47,7 +57,7 @@ class BluePrintEnhancerUtils { } fun populateRelationshipType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService, - relationshipName: String): RelationshipType { + relationshipName: String): RelationshipType { val relationshipType = bluePrintContext.serviceTemplate.relationshipTypes?.get(relationshipName) ?: bluePrintRepoService.getRelationshipType(relationshipName) @@ -77,6 +87,47 @@ class BluePrintEnhancerUtils { return artifactType } + suspend fun copyFromFilePart(filePart: FilePart, targetFile: File): File { + // Delete the Directory + targetFile.deleteRecursively() + return filePart.transferTo(targetFile) + .thenReturn(targetFile) + .awaitSingle() + } + + suspend fun decompressFilePart(filePart: FilePart, archiveDir: String, enhanceDir: String): File { + //Recreate the Base Directories + Paths.get(archiveDir).toFile().reCreateDirs() + Paths.get(enhanceDir).toFile().reCreateDirs() + + val filePartFile = Paths.get(archiveDir, "cba.zip").toFile() + // Copy the File Part to ZIP + copyFromFilePart(filePart, filePartFile) + val deCompressFileName = Paths.get(enhanceDir).toUri().path + return filePartFile.deCompress(deCompressFileName) + } + + suspend fun compressToFilePart(enhanceDir: String, archiveDir: String): ResponseEntity { + val compressedFile = Paths.get(archiveDir, "enhanced-cba.zip").toFile() + BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile, true) + return prepareResourceEntity(compressedFile.name, compressedFile.readBytes()) + } + + suspend fun prepareResourceEntity(fileName: String, file: ByteArray): ResponseEntity { + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("text/plain")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"") + .body(ByteArrayResource(file)) + } + + suspend fun cleanEnhancer(archiveLocation: String, enhancementLocation: String) = withContext(Dispatchers.Default) { + val enrichDir = File(enhancementLocation) + FileUtils.forceDeleteOnExit(enrichDir) + + val archiveDir = File(archiveLocation) + FileUtils.forceDeleteOnExit(archiveDir) + } + /** * 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 -- cgit 1.2.3-korg