dee7ae86b3ce57c5cda94dd30edfcaa72a73e83b
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.blueprintsprocessor.db
19
20 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
21 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.domain.BlueprintProcessorModel
22 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.domain.BlueprintProcessorModelContent
23 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
28 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
30 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
31 import org.slf4j.LoggerFactory
32 import org.springframework.dao.DataIntegrityViolationException
33 import org.springframework.stereotype.Service
34 import java.io.File
35 import java.nio.file.Files
36 import java.nio.file.Path
37 import java.nio.file.Paths
38
39 /**
40  * Similar/Duplicate implementation in [org.onap.ccsdk.apps.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
41  */
42 @Service
43 class BlueprintProcessorCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService,
44                                            private val blueprintConfig: BluePrintCoreConfiguration,
45                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
46     : BlueprintCatalogServiceImpl(bluePrintValidatorService) {
47
48     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
49
50     init {
51
52         log.info("BlueprintProcessorCatalogServiceImpl initialized")
53     }
54
55     override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
56
57
58     override fun get(name: String, version: String, extract: Boolean): Path? {
59         var path = "${blueprintConfig.archivePath}/$name/$version.zip"
60
61         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
62             it.blueprintModelContent.run {
63                 val file = File(path)
64                 file.parentFile.mkdirs()
65                 file.createNewFile()
66                 file.writeBytes(this!!.content!!).let {
67                     if (extract) {
68                         path = "${blueprintConfig.archivePath}/$name/$version"
69                         BluePrintArchiveUtils.deCompress(file, path)
70                     }
71                     return Paths.get(path)
72                 }
73             }
74         }
75         return null
76     }
77
78     override fun save(metadata: MutableMap<String, String>, archiveFile: File) {
79         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
80         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
81
82         log.isDebugEnabled.apply {
83             blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
84                 log.debug("Overwriting blueprint model :$artifactName::$artifactVersion")
85             }
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 }