Resolve duplicate application property values
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / BlueprintProcessorCatalogServiceImpl.kt
index 3234c9a..ce2aa95 100755 (executable)
@@ -23,66 +23,86 @@ import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcess
 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
 import org.onap.ccsdk.cds.controllerblueprints.core.*
 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
+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.BluePrintValidatorService
-import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintCompileCache
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
 import org.onap.ccsdk.cds.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
 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.util.*
 
 /**
  * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
  */
-@Service
+@Service("blueprintsProcessorCatalogService")
 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
-                                           private val bluePrintPathConfiguration: BluePrintPathConfiguration,
+                                           private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
-    : BlueprintCatalogServiceImpl(bluePrintPathConfiguration, bluePrintRuntimeValidatorService) {
+    : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintRuntimeValidatorService) {
 
     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
 
-    init {
-
-        log.info("BlueprintProcessorCatalogServiceImpl initialized")
-    }
-
     override suspend fun delete(name: String, version: String) {
+        // Clean blueprint script cache
+        val cacheKey = BluePrintFileUtils
+                .compileCacheKey(normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, name, version))
+        BluePrintCompileCache.cleanClassLoader(cacheKey)
+        log.info("removed cba file name($name), version($version) from cache")
         // Cleaning Deployed Blueprint
-        deleteNBDir(bluePrintPathConfiguration.blueprintDeployPath, name, version)
+        deleteNBDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
+        log.info("removed cba file name($name), version($version) from deploy location")
         // Cleaning Data Base
         blueprintModelRepository
                 .deleteByArtifactNameAndArtifactVersion(name, version)
+        log.info("removed cba file name($name), version($version) from database")
     }
 
 
     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
 
-        val getId = UUID.randomUUID().toString()
-        var path = "${bluePrintPathConfiguration.blueprintArchivePath}/$getId/cba.zip"
+        val deployFile = normalizedFile(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
+        val cbaFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath,
+                UUID.randomUUID().toString(), "cba.zip")
 
-        // TODO("Check first location for the file", If not get from database")
+        if (extract && deployFile.exists()) {
+            log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
+        } else {
+            deployFile.reCreateNBDirs()
+            cbaFile.parentFile.reCreateNBDirs()
 
-        blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
-            it.blueprintModelContent.run {
-                val file = normalizedFile(path)
-                file.parentFile.reCreateDirs()
+            try {
+                log.info("getting cba file name($name), version($version) from db")
+                blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
+                    it.blueprintModelContent.run {
 
-                file.writeBytes(this!!.content!!).let {
-                    if (extract) {
-                        path = "${bluePrintPathConfiguration.blueprintDeployPath}/$name/$version"
-                        BluePrintArchiveUtils.deCompress(file, path)
+                        cbaFile.writeBytes(this!!.content!!)
+                        cbaFile.deCompress(deployFile)
+                        log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
                     }
-                    return normalizedPath(path)
                 }
+
+                check(deployFile.exists() && deployFile.list().isNotEmpty()) {
+                    throw BluePrintProcessorException("file check failed")
+                }
+            } catch (e: Exception) {
+                deleteNBDir(deployFile.absolutePath)
+                throw BluePrintProcessorException("failed to get  get cba file name($name), version($version) from db" +
+                        " : ${e.message}")
+            } finally {
+                deleteNBDir(cbaFile.parentFile.absolutePath)
             }
         }
-        return null
+
+        return if (extract) {
+            deployFile.toPath()
+        } else {
+            cbaFile.toPath()
+        }
     }
 
     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
@@ -96,6 +116,12 @@ class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: Blu
         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
+            val deployFile =
+                    normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, artifactName, artifactVersion)
+            deleteNBDir(deployFile).let {
+                if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
+                else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
+            }
         }
 
         val blueprintModel = BlueprintProcessorModel()
@@ -112,7 +138,7 @@ class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: Blu
         blueprintModelContent.contentType = "CBA_ZIP"
         blueprintModelContent.name = "$artifactName:$artifactVersion"
         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
-        blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
+        blueprintModelContent.content = archiveFile.readBytes()
         blueprintModelContent.blueprintModel = blueprintModel
 
         blueprintModel.blueprintModelContent = blueprintModelContent