66b5d00c41331244a2ec716c13812f7fa050cc73
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.db.service
20
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.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
29 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
31 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPath
32 import org.slf4j.LoggerFactory
33 import org.springframework.dao.DataIntegrityViolationException
34 import org.springframework.stereotype.Service
35 import java.io.File
36 import java.nio.file.Files
37 import java.nio.file.Path
38 import java.util.*
39 //TODO("Duplicate : Merge BlueprintProcessorCatalogServiceImpl and ControllerBlueprintCatalogServiceImpl")
40 /**
41  * Similar implementation in [org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
42  */
43 @Service("controllerBlueprintsCatalogService")
44 class ControllerBlueprintCatalogServiceImpl(bluePrintDesignTimeValidatorService: BluePrintValidatorService,
45                                             private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
46                                             private val blueprintModelRepository: BlueprintModelRepository)
47     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintDesignTimeValidatorService) {
48
49
50     private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString())
51
52     init {
53         log.info("BlueprintProcessorCatalogServiceImpl initialized")
54     }
55
56     override suspend fun delete(name: String, version: String) {
57         // Cleaning Deployed Blueprint
58         deleteDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
59         // Cleaning Data Base
60         blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
61     }
62
63     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
64         val path = if (extract) {
65             normalizedPath(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
66         } else {
67             normalizedPath(bluePrintLoadConfiguration.blueprintArchivePath, UUID.randomUUID().toString(), "cba.zip")
68         }
69         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
70             it.blueprintModelContent.run {
71                 path.toFile().writeBytes(this!!.content!!).let {
72                     return path
73                 }
74             }
75         }
76         return null
77     }
78
79     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
80
81         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
82         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
83
84         check(archiveFile.isFile && !archiveFile.isDirectory) {
85             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
86         }
87
88         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
89             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
90             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
91         }
92
93         val blueprintModel = BlueprintModel()
94         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
95         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
96         blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
97                 ?: BluePrintConstants.FLAG_N
98         blueprintModel.artifactName = artifactName
99         blueprintModel.artifactVersion = artifactVersion
100         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]!!
101         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]!!
102         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
103
104         val blueprintModelContent = BlueprintModelContent()
105         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
106         blueprintModelContent.contentType = "CBA_ZIP"
107         blueprintModelContent.name = "$artifactName:$artifactVersion"
108         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
109         blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
110         blueprintModelContent.blueprintModel = blueprintModel
111         // Set the Blueprint Model Content into blueprintModel
112         blueprintModel.blueprintModelContent = blueprintModelContent
113
114         try {
115             blueprintModelRepository.saveAndFlush(blueprintModel)
116         } catch (ex: DataIntegrityViolationException) {
117             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
118                     "is already exist in database: ${ex.message}", ex)
119         }
120     }
121 }