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
21 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcessorModel
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcessorModelContent
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
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.BluePrintPathConfiguration
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.db.resources.BlueprintCatalogServiceImpl
30 import org.slf4j.LoggerFactory
31 import org.springframework.dao.DataIntegrityViolationException
32 import org.springframework.stereotype.Service
34 import java.nio.file.Path
38 * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
41 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
42 private val bluePrintPathConfiguration: BluePrintPathConfiguration,
43 private val blueprintModelRepository: BlueprintProcessorModelRepository)
44 : BlueprintCatalogServiceImpl(bluePrintPathConfiguration, bluePrintRuntimeValidatorService) {
46 private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
48 override suspend fun delete(name: String, version: String) {
49 // Cleaning Deployed Blueprint
50 deleteNBDir(bluePrintPathConfiguration.blueprintDeployPath, name, version)
51 log.info("removed cba file name($name), version($version) from deploy location")
53 blueprintModelRepository
54 .deleteByArtifactNameAndArtifactVersion(name, version)
55 log.info("removed cba file name($name), version($version) from database")
59 override suspend fun get(name: String, version: String, extract: Boolean): Path? {
61 val deployFile = normalizedFile(bluePrintPathConfiguration.blueprintDeployPath, name, version)
62 val cbaFile = normalizedFile(bluePrintPathConfiguration.blueprintArchivePath,
63 UUID.randomUUID().toString(), "cba.zip")
65 if (extract && deployFile.exists()) {
66 log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
68 deployFile.reCreateNBDirs()
69 cbaFile.parentFile.reCreateNBDirs()
72 log.info("getting cba file name($name), version($version) from db")
73 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
74 it.blueprintModelContent.run {
76 cbaFile.writeBytes(this!!.content!!)
77 cbaFile.deCompress(deployFile)
78 log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
82 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
83 throw BluePrintProcessorException("file check failed")
85 } catch (e: Exception) {
86 deleteNBDir(deployFile.absolutePath)
87 throw BluePrintProcessorException("failed to get get cba file name($name), version($version) from db" +
90 deleteNBDir(cbaFile.parentFile.absolutePath)
101 override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
102 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
103 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
105 check(archiveFile.isFile && !archiveFile.isDirectory) {
106 throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
109 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
110 log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
111 blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
113 normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath, artifactName, artifactVersion)
114 deleteNBDir(deployFile).let {
115 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
116 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
120 val blueprintModel = BlueprintProcessorModel()
121 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
122 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
123 blueprintModel.artifactName = artifactName
124 blueprintModel.artifactVersion = artifactVersion
125 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
126 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
127 blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
129 val blueprintModelContent = BlueprintProcessorModelContent()
130 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
131 blueprintModelContent.contentType = "CBA_ZIP"
132 blueprintModelContent.name = "$artifactName:$artifactVersion"
133 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
134 blueprintModelContent.content = archiveFile.readBytes()
135 blueprintModelContent.blueprintModel = blueprintModel
137 blueprintModel.blueprintModelContent = blueprintModelContent
140 blueprintModelRepository.saveAndFlush(blueprintModel)
141 } catch (ex: DataIntegrityViolationException) {
142 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
143 "is already exist in database: ${ex.message}", ex)