1509e11f1f607d7d38793a3c904f56c3019eecb2
[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
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.BluePrintLoadConfiguration
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.scripts.BluePrintCompileCache
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
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.Path
36 import java.util.*
37
38 /**
39  * Similar/Duplicate implementation in [org.onap.ccsdk.cds.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
40  */
41 @Service("blueprintsProcessorCatalogService")
42 class BlueprintProcessorCatalogServiceImpl(bluePrintRuntimeValidatorService: BluePrintValidatorService,
43                                            private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
45     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintRuntimeValidatorService) {
46
47     private val log = LoggerFactory.getLogger(BlueprintProcessorCatalogServiceImpl::class.toString())
48
49     override suspend fun delete(name: String, version: String) {
50         // Clean blueprint script cache
51         val cacheKey = BluePrintFileUtils
52                 .compileCacheKey(normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, name, version))
53         BluePrintCompileCache.cleanClassLoader(cacheKey)
54         log.info("removed cba file name($name), version($version) from cache")
55         // Cleaning Deployed Blueprint
56         deleteNBDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
57         log.info("removed cba file name($name), version($version) from deploy location")
58         // Cleaning Data Base
59         blueprintModelRepository
60                 .deleteByArtifactNameAndArtifactVersion(name, version)
61         log.info("removed cba file name($name), version($version) from database")
62     }
63
64
65     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
66
67         val deployFile = normalizedFile(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
68         val cbaFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath,
69                 UUID.randomUUID().toString(), "cba.zip")
70
71         if (extract && deployFile.exists()) {
72             log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
73         } else {
74             deployFile.reCreateNBDirs()
75             cbaFile.parentFile.reCreateNBDirs()
76
77             try {
78                 log.info("getting cba file name($name), version($version) from db")
79                 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
80                     it.blueprintModelContent.run {
81
82                         cbaFile.writeBytes(this!!.content!!)
83                         cbaFile.deCompress(deployFile)
84                         log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
85                     }
86                 }
87
88                 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
89                     throw BluePrintProcessorException("file check failed")
90                 }
91             } catch (e: Exception) {
92                 deleteNBDir(deployFile.absolutePath)
93                 throw BluePrintProcessorException("failed to get  get cba file name($name), version($version) from db" +
94                         " : ${e.message}")
95             } finally {
96                 deleteNBDir(cbaFile.parentFile.absolutePath)
97             }
98         }
99
100         return if (extract) {
101             deployFile.toPath()
102         } else {
103             cbaFile.toPath()
104         }
105     }
106
107     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
108         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
109         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
110
111         check(archiveFile.isFile && !archiveFile.isDirectory) {
112             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
113         }
114
115         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
116             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
117             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
118             val deployFile =
119                     normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath, artifactName, artifactVersion)
120             deleteNBDir(deployFile).let {
121                 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
122                 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
123             }
124         }
125
126         val blueprintModel = BlueprintProcessorModel()
127         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
128         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
129         blueprintModel.artifactName = artifactName
130         blueprintModel.artifactVersion = artifactVersion
131         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
132         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
133         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
134
135         val blueprintModelContent = BlueprintProcessorModelContent()
136         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
137         blueprintModelContent.contentType = "CBA_ZIP"
138         blueprintModelContent.name = "$artifactName:$artifactVersion"
139         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
140         blueprintModelContent.content = archiveFile.readBytes()
141         blueprintModelContent.blueprintModel = blueprintModel
142
143         blueprintModel.blueprintModelContent = blueprintModelContent
144
145         try {
146             blueprintModelRepository.saveAndFlush(blueprintModel)
147         } catch (ex: DataIntegrityViolationException) {
148             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
149                     "is already exist in database: ${ex.message}", ex)
150         }
151     }
152 }