2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.service.load
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants
23 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
26 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel
28 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelContent
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository
30 import org.slf4j.LoggerFactory
31 import org.springframework.dao.DataIntegrityViolationException
32 import org.springframework.stereotype.Service
34 import java.nio.file.Files
35 import java.nio.file.Path
36 import java.nio.file.Paths
39 * Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
42 class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService,
43 private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44 private val blueprintModelRepository: ControllerBlueprintModelRepository)
45 : BlueprintCatalogServiceImpl(bluePrintValidatorService) {
48 private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString())
51 log.info("BlueprintProcessorCatalogServiceImpl initialized")
54 override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
56 override fun get(name: String, version: String, extract: Boolean): Path? {
57 val path = if (extract) {
58 Paths.get("${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version")
60 Paths.get("${bluePrintLoadConfiguration.blueprintArchivePath}/$name/$version.zip")
62 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
63 it.blueprintModelContent.run {
64 path.toFile().writeBytes(this!!.content!!).let {
72 override fun save(metadata: MutableMap<String, String>, archiveFile: File) {
74 val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
75 val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
77 log.isDebugEnabled.apply {
78 blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
79 log.debug("Overwriting blueprint model :$artifactName::$artifactVersion")
83 val blueprintModel = BlueprintModel()
84 blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
85 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
86 blueprintModel.published = ApplicationConstants.ACTIVE_N
87 blueprintModel.artifactName = artifactName
88 blueprintModel.artifactVersion = artifactVersion
89 blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
90 blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
91 blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
93 val blueprintModelContent = BlueprintModelContent()
94 blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
95 blueprintModelContent.contentType = "CBA_ZIP"
96 blueprintModelContent.name = "$artifactName:$artifactVersion"
97 blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
98 blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
99 blueprintModelContent.blueprintModel = blueprintModel
100 // Set the Blueprint Model Content into blueprintModel
101 blueprintModel.blueprintModelContent = blueprintModelContent
104 blueprintModelRepository.saveAndFlush(blueprintModel)
105 } catch (ex: DataIntegrityViolationException) {
106 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
107 "is already exist in database: ${ex.message}", ex)