Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / BlueprintProcessorCatalogServiceImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2018 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
20
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
35 import java.io.File
36 import java.nio.file.Files
37 import java.nio.file.Path
38 import java.nio.file.Paths
39
40 /**
41  * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
42  */
43 @Service
44 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
45                                            private val blueprintConfig: BluePrintCoreConfiguration,
46                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
47     : BlueprintCatalogServiceImpl(bluePrintRuntimeValidatorService) {
48
49     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
50
51     init {
52
53         log.info("BlueprintProcessorCatalogServiceImpl initialized")
54     }
55
56     override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
57
58
59     override fun get(name: String, version: String, extract: Boolean): Path? {
60         var path = "${blueprintConfig.archivePath}/$name/$version.zip"
61
62         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
63             it.blueprintModelContent.run {
64                 val file = File(path)
65                 file.parentFile.mkdirs()
66                 file.createNewFile()
67                 file.writeBytes(this!!.content!!).let {
68                     if (extract) {
69                         path = "${blueprintConfig.archivePath}/$name/$version"
70                         BluePrintArchiveUtils.deCompress(file, path)
71                     }
72                     return Paths.get(path)
73                 }
74             }
75         }
76         return null
77     }
78
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]
82
83         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
84             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
85             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
86         }
87
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"
96
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
104
105         blueprintModel.blueprintModelContent = blueprintModelContent
106
107         try {
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)
112         }
113     }
114 }