4c02fda128df108fd74cfdad10f27933730af559
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 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.service
20
21 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModel
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelContent
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
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.Files
36 import java.nio.file.Path
37 import java.util.*
38 //TODO("Duplicate : Merge BlueprintProcessorCatalogServiceImpl and ControllerBlueprintCatalogServiceImpl")
39 /**
40  * Similar implementation in [org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
41  */
42 @Service("controllerBlueprintsCatalogService")
43 class ControllerBlueprintCatalogServiceImpl(bluePrintDesignTimeValidatorService: BluePrintValidatorService,
44                                             private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
45                                             private val blueprintModelRepository: BlueprintModelRepository)
46     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration, bluePrintDesignTimeValidatorService) {
47
48
49     private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString())
50
51     init {
52         log.info("BlueprintProcessorCatalogServiceImpl initialized")
53     }
54
55     override suspend fun delete(name: String, version: String) {
56         // Cleaning Deployed Blueprint
57         deleteDir(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
58         // Cleaning Data Base
59         blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
60     }
61
62     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
63         val path = if (extract) {
64             normalizedPath(bluePrintLoadConfiguration.blueprintDeployPath, name, version)
65         } else {
66             normalizedPath(bluePrintLoadConfiguration.blueprintArchivePath, UUID.randomUUID().toString(), "cba.zip")
67         }
68         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
69             it.blueprintModelContent.run {
70                 path.toFile().writeBytes(this!!.content!!).let {
71                     return path
72                 }
73             }
74         }
75         return null
76     }
77
78     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
79
80         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
81         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
82
83         check(archiveFile.isFile && !archiveFile.isDirectory) {
84             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
85         }
86
87         // cleanup up deploy folder for the Blueprint
88         val blueprintDeploy = normalizedPathName(bluePrintLoadConfiguration.blueprintDeployPath,
89                                     artifactName, artifactVersion)
90         val cacheKey = BluePrintFileUtils.compileCacheKey(blueprintDeploy)
91         BluePrintCompileCache.cleanClassLoader(cacheKey)
92
93         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
94             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
95             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
96         }
97
98         val blueprintModel = BlueprintModel()
99         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
100         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
101         blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
102                 ?: BluePrintConstants.FLAG_N
103         blueprintModel.artifactName = artifactName
104         blueprintModel.artifactVersion = artifactVersion
105         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]!!
106         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]!!
107         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
108
109         val blueprintModelContent = BlueprintModelContent()
110         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
111         blueprintModelContent.contentType = "CBA_ZIP"
112         blueprintModelContent.name = "$artifactName:$artifactVersion"
113         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
114         blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
115         blueprintModelContent.blueprintModel = blueprintModel
116         // Set the Blueprint Model Content into blueprintModel
117         blueprintModel.blueprintModelContent = blueprintModelContent
118
119         try {
120             blueprintModelRepository.saveAndFlush(blueprintModel)
121         } catch (ex: DataIntegrityViolationException) {
122             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
123                     "is already exist in database: ${ex.message}", ex)
124         }
125     }
126 }