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.core.BluePrintCoreConfiguration
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcessorModel
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcessorModelContent
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
31 import org.onap.ccsdk.cds.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
32 import org.slf4j.LoggerFactory
33 import org.springframework.dao.DataIntegrityViolationException
34 import org.springframework.stereotype.Service
36 import java.nio.file.Files
37 import java.nio.file.Path
38 import java.nio.file.Paths
41 * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
44 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
45 private val blueprintConfig: BluePrintCoreConfiguration,
46 private val blueprintModelRepository: BlueprintProcessorModelRepository)
47 : BlueprintCatalogServiceImpl(bluePrintRuntimeValidatorService) {
49 private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
53 log.info("BlueprintProcessorCatalogServiceImpl initialized")
56 override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
59 override fun get(name: String, version: String, extract: Boolean): Path? {
60 var path = "${blueprintConfig.archivePath}/$name/$version.zip"
62 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
63 it.blueprintModelContent.run {
65 file.parentFile.mkdirs()
67 file.writeBytes(this!!.content!!).let {
69 path = "${blueprintConfig.archivePath}/$name/$version"
70 BluePrintArchiveUtils.deCompress(file, path)
72 return Paths.get(path)
79 override fun save(metadata: MutableMap<String, String>, archiveFile: File) {
80 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
81 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
83 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
84 log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
85 blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
88 val blueprintModel = BlueprintProcessorModel()
89 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
90 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
91 blueprintModel.artifactName = artifactName
92 blueprintModel.artifactVersion = artifactVersion
93 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
94 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
95 blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
97 val blueprintModelContent = BlueprintProcessorModelContent()
98 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
99 blueprintModelContent.contentType = "CBA_ZIP"
100 blueprintModelContent.name = "$artifactName:$artifactVersion"
101 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
102 blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
103 blueprintModelContent.blueprintModel = blueprintModel
105 blueprintModel.blueprintModelContent = blueprintModelContent
108 blueprintModelRepository.saveAndFlush(blueprintModel)
109 } catch (ex: DataIntegrityViolationException) {
110 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
111 "is already exist in database: ${ex.message}", ex)