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.service
21 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModel
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelContent
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
24 import org.onap.ccsdk.cds.controllerblueprints.core.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
29 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintCompileCache
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
31 import org.slf4j.LoggerFactory
32 import org.springframework.dao.DataIntegrityViolationException
33 import org.springframework.stereotype.Service
35 import java.nio.file.Path
37 // TODO("Duplicate : Merge BlueprintProcessorCatalogServiceImpl and ControllerBlueprintCatalogServiceImpl")
39 * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
41 @Service("blueprintsProcessorCatalogService")
42 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
43 private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44 private val blueprintModelRepository: BlueprintModelRepository)
45 : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintRuntimeValidatorService) {
47 private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
49 override suspend fun delete(name: String, version: String) {
50 // Clean blueprint script cache
51 val cacheKey = BluePrintFileUtils
52 .compileCacheKey(normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, name, version))
53 BluePrintCompileCache.cleanClassLoader(cacheKey)
54 log.info("removed cba file name($name), version($version) from cache")
55 // Cleaning Deployed Blueprint
56 deleteNBDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
57 log.info("removed cba file name($name), version($version) from deploy location")
59 blueprintModelRepository
60 .deleteByArtifactNameAndArtifactVersion(name, version)
61 log.info("removed cba file name($name), version($version) from database")
65 override suspend fun get(name: String, version: String, extract: Boolean): Path? {
67 val deployFile = normalizedFile(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
68 val cbaFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath,
69 UUID.randomUUID().toString(), "cba.zip")
71 if (extract && deployFile.exists()) {
72 log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
74 deployFile.reCreateNBDirs()
75 cbaFile.parentFile.reCreateNBDirs()
78 log.info("getting cba file name($name), version($version) from db")
79 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
80 it.blueprintModelContent.run {
82 cbaFile.writeBytes(this!!.content!!)
83 cbaFile.deCompress(deployFile)
84 log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
88 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
89 throw BluePrintProcessorException("file check failed")
91 } catch (e: Exception) {
92 deleteNBDir(deployFile.absolutePath)
93 throw BluePrintProcessorException("failed to get get cba file name($name), version($version) from db" +
96 deleteNBDir(cbaFile.parentFile.absolutePath)
100 return if (extract) {
107 override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
108 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
109 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
111 check(archiveFile.isFile && !archiveFile.isDirectory) {
112 throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
115 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
116 log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
117 blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
119 normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, artifactName, artifactVersion)
120 deleteNBDir(deployFile).let {
121 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
122 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
126 val blueprintModel = BlueprintModel()
127 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
128 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
129 blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
130 ?: BluePrintConstants.FLAG_Y
131 blueprintModel.artifactName = artifactName
132 blueprintModel.artifactVersion = artifactVersion
133 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]!!
134 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]!!
135 blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
137 val blueprintModelContent = BlueprintModelContent()
138 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
139 blueprintModelContent.contentType = "CBA_ZIP"
140 blueprintModelContent.name = "$artifactName:$artifactVersion"
141 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
142 blueprintModelContent.content = archiveFile.readBytes()
143 blueprintModelContent.blueprintModel = blueprintModel
145 blueprintModel.blueprintModelContent = blueprintModelContent
148 blueprintModelRepository.saveAndFlush(blueprintModel)
149 } catch (ex: DataIntegrityViolationException) {
150 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
151 "is already exist in database: ${ex.message}", ex)