2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2018 IBM.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.db.primary.service
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.BlueprintClusterTopic
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.optionalClusterService
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModel
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelContent
25 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
30 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
32 import org.onap.ccsdk.cds.controllerblueprints.core.deCompress
33 import org.onap.ccsdk.cds.controllerblueprints.core.deleteNBDir
34 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
35 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
36 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
37 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateNBDirs
38 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintCompileCache
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
41 import org.slf4j.LoggerFactory
42 import org.springframework.dao.DataIntegrityViolationException
43 import org.springframework.stereotype.Service
45 import java.nio.file.Path
48 // TODO("Duplicate : Merge BlueprintProcessorCatalogServiceImpl and ControllerBlueprintCatalogServiceImpl")
50 * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
52 @Service("blueprintsProcessorCatalogService")
53 class BlueprintProcessorCatalogServiceImpl(
54 bluePrintRuntimeValidatorService: BluePrintValidatorService,
55 private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
56 private val blueprintModelRepository: BlueprintModelRepository
58 BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintRuntimeValidatorService) {
60 private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
62 override suspend fun delete(name: String, version: String) {
63 // Clean blueprint script cache
64 val cacheKey = BluePrintFileUtils
65 .compileCacheKey(normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, name, version))
66 cleanClassLoader(cacheKey)
67 log.info("removed cba file name($name), version($version) from cache")
68 // Cleaning Deployed Blueprint
69 deleteNBDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
70 log.info("removed cba file name($name), version($version) from deploy location")
72 blueprintModelRepository
73 .deleteByArtifactNameAndArtifactVersion(name, version)
74 log.info("removed cba file name($name), version($version) from database")
77 override suspend fun get(name: String, version: String, extract: Boolean): Path? {
79 val deployFile = normalizedFile(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
80 val cbaFile = normalizedFile(
81 bluePrintLoadConfiguration.blueprintArchivePath,
82 UUID.randomUUID().toString(), "cba.zip"
85 if (extract && deployFile.exists()) {
86 log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
88 deployFile.reCreateNBDirs()
89 cbaFile.parentFile.reCreateNBDirs()
92 log.info("getting cba file name($name), version($version) from db")
93 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
94 it.blueprintModelContent.run {
96 cbaFile.writeBytes(this!!.content!!)
97 cbaFile.deCompress(deployFile)
98 log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
102 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
103 throw BluePrintProcessorException("file check failed")
105 } catch (e: Exception) {
106 deleteNBDir(deployFile.absolutePath)
107 throw BluePrintProcessorException(
108 "failed to get get cba file name($name), version($version) from db" +
112 deleteNBDir(cbaFile.parentFile.absolutePath)
116 return if (extract) {
123 override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
124 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
125 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
127 check(archiveFile.isFile && !archiveFile.isDirectory) {
128 throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
131 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
132 log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
133 blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
135 normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, artifactName, artifactVersion)
137 val cacheKey = BluePrintFileUtils.compileCacheKey(deployFile)
138 cleanClassLoader(cacheKey)
140 deleteNBDir(deployFile).let {
141 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
142 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
146 val blueprintModel = BlueprintModel()
147 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
148 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
149 blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
150 ?: BluePrintConstants.FLAG_N
151 blueprintModel.artifactName = artifactName
152 blueprintModel.artifactVersion = artifactVersion
153 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]!!
154 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]!!
156 if (null != metadata[BluePrintConstants.METADATA_TEMPLATE_DESCRIPTION]) metadata[BluePrintConstants.METADATA_TEMPLATE_DESCRIPTION] else ""
157 blueprintModel.artifactDescription = description
159 val blueprintModelContent = BlueprintModelContent()
160 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
161 blueprintModelContent.contentType = "CBA_ZIP"
162 blueprintModelContent.name = "$artifactName:$artifactVersion"
163 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
164 blueprintModelContent.content = archiveFile.readBytes()
165 blueprintModelContent.blueprintModel = blueprintModel
167 blueprintModel.blueprintModelContent = blueprintModelContent
170 blueprintModelRepository.saveAndFlush(blueprintModel)
171 } catch (ex: DataIntegrityViolationException) {
172 throw BluePrintException(
173 ErrorCode.CONFLICT_ADDING_RESOURCE.value,
174 "The blueprint entry " +
175 "is already exist in database: ${ex.message}",
181 private suspend fun cleanClassLoader(cacheKey: String) {
182 val clusterService = BluePrintDependencyService.optionalClusterService()
183 if (clusterService != null && clusterService.clusterJoined()) {
184 log.info("Sending ClusterMessage: Clean Classloader Cache")
185 clusterService.sendMessage(BlueprintClusterTopic.BLUEPRINT_CLEAN_COMPILER_CACHE, cacheKey)
186 } else BluePrintCompileCache.cleanClassLoader(cacheKey)