c45a28a07526476bf6d1ada4aa4603a618ad6abe
[ccsdk/cds.git] /
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.primary.service
20
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.BlueprintClusterTopic
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.optionalClusterService
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModel
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelContent
25 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
30 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
33 import org.onap.ccsdk.cds.controllerblueprints.core.deCompress
34 import org.onap.ccsdk.cds.controllerblueprints.core.deleteNBDir
35 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
36 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
37 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
38 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateNBDirs
39 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintCompileCache
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
42 import org.slf4j.LoggerFactory
43 import org.springframework.dao.DataIntegrityViolationException
44 import org.springframework.stereotype.Service
45 import java.io.File
46 import java.nio.file.Path
47 import java.util.UUID
48
49 // TODO("Duplicate : Merge BlueprintProcessorCatalogServiceImpl and ControllerBlueprintCatalogServiceImpl")
50 /**
51  * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
52  */
53 @Service("blueprintsProcessorCatalogService")
54 class BlueprintProcessorCatalogServiceImpl(
55     bluePrintRuntimeValidatorService: BluePrintValidatorService,
56     private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
57     private val blueprintModelRepository: BlueprintModelRepository
58 ) :
59     BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintRuntimeValidatorService) {
60
61     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
62
63     override suspend fun delete(name: String, version: String) {
64         // Clean blueprint script cache
65         val cacheKey = BluePrintFileUtils
66             .compileCacheKey(normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, name, version))
67         cleanClassLoader(cacheKey)
68         log.info("removed cba file name($name), version($version) from cache")
69         // Cleaning Deployed Blueprint
70         deleteNBDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
71         log.info("removed cba file name($name), version($version) from deploy location")
72         // Cleaning Data Base
73         blueprintModelRepository
74             .deleteByArtifactNameAndArtifactVersion(name, version)
75         log.info("removed cba file name($name), version($version) from database")
76     }
77
78     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
79
80         val deployFile = normalizedFile(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
81         val cbaFile = normalizedFile(
82             bluePrintLoadConfiguration.blueprintArchivePath,
83             UUID.randomUUID().toString(), "cba.zip"
84         )
85
86         if (extract && deployFile.exists()) {
87             log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
88         } else {
89             deployFile.reCreateNBDirs()
90             cbaFile.parentFile.reCreateNBDirs()
91
92             try {
93                 log.info("getting cba file name($name), version($version) from db")
94                 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
95                     it.blueprintModelContent.run {
96
97                         cbaFile.writeBytes(this!!.content!!)
98                         cbaFile.deCompress(deployFile)
99                         log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
100                     }
101                 }
102
103                 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
104                     throw BluePrintProcessorException("file check failed")
105                 }
106             } catch (e: Exception) {
107                 deleteNBDir(deployFile.absolutePath)
108                 throw BluePrintProcessorException(
109                     "failed to get  get cba file name($name), version($version) from db" +
110                         " : ${e.message}"
111                 )
112             } finally {
113                 deleteNBDir(cbaFile.parentFile.absolutePath)
114             }
115         }
116
117         return if (extract) {
118             deployFile.toPath()
119         } else {
120             cbaFile.toPath()
121         }
122     }
123
124     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File, workflows: Map<String, Workflow>) {
125         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
126         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
127
128         check(archiveFile.isFile && !archiveFile.isDirectory) {
129             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
130         }
131
132         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
133             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
134             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
135             val deployFile =
136                 normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, artifactName, artifactVersion)
137
138             val cacheKey = BluePrintFileUtils.compileCacheKey(deployFile)
139             cleanClassLoader(cacheKey)
140
141             deleteNBDir(deployFile).let {
142                 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
143                 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
144             }
145         }
146
147         val blueprintModel = BlueprintModel()
148         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
149         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
150         blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
151             ?: BluePrintConstants.FLAG_N
152         blueprintModel.artifactName = artifactName
153         blueprintModel.artifactVersion = artifactVersion
154         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]!!
155         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]!!
156         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
157         blueprintModel.workflows = workflows
158
159         val blueprintModelContent = BlueprintModelContent()
160         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
161         blueprintModelContent.contentType = "CBA_ZIP"
162         blueprintModelContent.name = "$artifactName:$artifactVersion"
163         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
164         blueprintModelContent.content = archiveFile.readBytes()
165         blueprintModelContent.blueprintModel = blueprintModel
166
167         blueprintModel.blueprintModelContent = blueprintModelContent
168
169         try {
170             blueprintModelRepository.saveAndFlush(blueprintModel)
171         } catch (ex: DataIntegrityViolationException) {
172             throw BluePrintException(
173                 ErrorCode.CONFLICT_ADDING_RESOURCE.value,
174                 "The blueprint entry " +
175                     "is already exist in database: ${ex.message}",
176                 ex
177             )
178         }
179     }
180
181     private suspend fun cleanClassLoader(cacheKey: String) {
182         val clusterService = BluePrintDependencyService.optionalClusterService()
183         if (clusterService != null && clusterService.clusterJoined()) {
184             log.info("Sending ClusterMessage: Clean Classloader Cache")
185             clusterService.sendMessage(BlueprintClusterTopic.BLUEPRINT_CLEAN_COMPILER_CACHE, cacheKey)
186         } else BluePrintCompileCache.cleanClassLoader(cacheKey)
187     }
188 }