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())
50 log.info("BlueprintProcessorCatalogServiceImpl initialized")
53 override suspend fun delete(name: String, version: String) {
54 // Cleaning Deployed Blueprint
55 deleteNBDir(bluePrintPathConfiguration.blueprintDeployPath, name, version)
56 log.info("removed cba file name($name), version($version) from deploy location")
58 blueprintModelRepository
59 .deleteByArtifactNameAndArtifactVersion(name, version)
60 log.info("removed cba file name($name), version($version) from database")
64 override suspend fun get(name: String, version: String, extract: Boolean): Path? {
66 val deployFile = normalizedFile(bluePrintPathConfiguration.blueprintDeployPath, name, version)
67 val cbaFile = normalizedFile(bluePrintPathConfiguration.blueprintArchivePath,
68 UUID.randomUUID().toString(), "cba.zip")
70 if (extract && deployFile.exists()) {
71 log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
73 deployFile.reCreateNBDirs()
74 cbaFile.parentFile.reCreateNBDirs()
77 log.info("getting cba file name($name), version($version) from db")
78 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
79 it.blueprintModelContent.run {
81 cbaFile.writeBytes(this!!.content!!)
82 cbaFile.deCompress(deployFile)
83 log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
87 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
88 throw BluePrintProcessorException("file check failed")
90 } catch (e: Exception) {
91 deleteNBDir(deployFile.absolutePath)
92 throw BluePrintProcessorException("failed to get get cba file name($name), version($version) from db" +
95 deleteNBDir(cbaFile.parentFile.absolutePath)
106 override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
107 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
108 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
110 check(archiveFile.isFile && !archiveFile.isDirectory) {
111 throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
114 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
115 log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
116 blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
118 normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath, artifactName, artifactVersion)
119 deleteNBDir(deployFile).let {
120 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
121 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
125 val blueprintModel = BlueprintProcessorModel()
126 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
127 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
128 blueprintModel.artifactName = artifactName
129 blueprintModel.artifactVersion = artifactVersion
130 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
131 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
132 blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
134 val blueprintModelContent = BlueprintProcessorModelContent()
135 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
136 blueprintModelContent.contentType = "CBA_ZIP"
137 blueprintModelContent.name = "$artifactName:$artifactVersion"
138 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
139 blueprintModelContent.content = archiveFile.readBytes()
140 blueprintModelContent.blueprintModel = blueprintModel
142 blueprintModel.blueprintModelContent = blueprintModelContent
145 blueprintModelRepository.saveAndFlush(blueprintModel)
146 } catch (ex: DataIntegrityViolationException) {
147 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
148 "is already exist in database: ${ex.message}", ex)