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