3234c9a3c3bc46b480c8dd2f190546915b50ef03
[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.db.primary.domain.BlueprintProcessorModel
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintProcessorModelContent
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
24 import org.onap.ccsdk.cds.controllerblueprints.core.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
30 import org.onap.ccsdk.cds.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.util.*
38
39 /**
40  * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
41  */
42 @Service
43 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
44                                            private val bluePrintPathConfiguration: BluePrintPathConfiguration,
45                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
46     : BlueprintCatalogServiceImpl(bluePrintPathConfiguration, bluePrintRuntimeValidatorService) {
47
48     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
49
50     init {
51
52         log.info("BlueprintProcessorCatalogServiceImpl initialized")
53     }
54
55     override suspend fun delete(name: String, version: String) {
56         // Cleaning Deployed Blueprint
57         deleteNBDir(bluePrintPathConfiguration.blueprintDeployPath, name, version)
58         // Cleaning Data Base
59         blueprintModelRepository
60                 .deleteByArtifactNameAndArtifactVersion(name, version)
61     }
62
63
64     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
65
66         val getId = UUID.randomUUID().toString()
67         var path = "${bluePrintPathConfiguration.blueprintArchivePath}/$getId/cba.zip"
68
69         // TODO("Check first location for the file", If not get from database")
70
71         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
72             it.blueprintModelContent.run {
73                 val file = normalizedFile(path)
74                 file.parentFile.reCreateDirs()
75
76                 file.writeBytes(this!!.content!!).let {
77                     if (extract) {
78                         path = "${bluePrintPathConfiguration.blueprintDeployPath}/$name/$version"
79                         BluePrintArchiveUtils.deCompress(file, path)
80                     }
81                     return normalizedPath(path)
82                 }
83             }
84         }
85         return null
86     }
87
88     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
89         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
90         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
91
92         check(archiveFile.isFile && !archiveFile.isDirectory) {
93             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
94         }
95
96         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
97             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
98             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
99         }
100
101         val blueprintModel = BlueprintProcessorModel()
102         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
103         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
104         blueprintModel.artifactName = artifactName
105         blueprintModel.artifactVersion = artifactVersion
106         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
107         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
108         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
109
110         val blueprintModelContent = BlueprintProcessorModelContent()
111         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
112         blueprintModelContent.contentType = "CBA_ZIP"
113         blueprintModelContent.name = "$artifactName:$artifactVersion"
114         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
115         blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
116         blueprintModelContent.blueprintModel = blueprintModel
117
118         blueprintModel.blueprintModelContent = blueprintModelContent
119
120         try {
121             blueprintModelRepository.saveAndFlush(blueprintModel)
122         } catch (ex: DataIntegrityViolationException) {
123             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
124                     "is already exist in database: ${ex.message}", ex)
125         }
126     }
127 }