Merge "Re-enabled test for happy-path case on NetconfDeviceCommunicatorTest"
[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     override suspend fun delete(name: String, version: String) {
49         // Cleaning Deployed Blueprint
50         deleteNBDir(bluePrintPathConfiguration.blueprintDeployPath, name, version)
51         log.info("removed cba file name($name), version($version) from deploy location")
52         // Cleaning Data Base
53         blueprintModelRepository
54             .deleteByArtifactNameAndArtifactVersion(name, version)
55         log.info("removed cba file name($name), version($version) from database")
56     }
57
58
59     override suspend fun get(name: String, version: String, extract: Boolean): Path? {
60
61         val deployFile = normalizedFile(bluePrintPathConfiguration.blueprintDeployPath, name, version)
62         val cbaFile = normalizedFile(bluePrintPathConfiguration.blueprintArchivePath,
63             UUID.randomUUID().toString(), "cba.zip")
64
65         if (extract && deployFile.exists()) {
66             log.info("cba file name($name), version($version) already present(${deployFile.absolutePath})")
67         } else {
68             deployFile.reCreateNBDirs()
69             cbaFile.parentFile.reCreateNBDirs()
70
71             try {
72                 log.info("getting cba file name($name), version($version) from db")
73                 blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
74                     it.blueprintModelContent.run {
75
76                         cbaFile.writeBytes(this!!.content!!)
77                         cbaFile.deCompress(deployFile)
78                         log.info("cba file name($name), version($version) saved in (${deployFile.absolutePath})")
79                     }
80                 }
81
82                 check(deployFile.exists() && deployFile.list().isNotEmpty()) {
83                     throw BluePrintProcessorException("file check failed")
84                 }
85             } catch (e: Exception) {
86                 deleteNBDir(deployFile.absolutePath)
87                 throw BluePrintProcessorException("failed to get  get cba file name($name), version($version) from db" +
88                         " : ${e.message}")
89             } finally {
90                 deleteNBDir(cbaFile.parentFile.absolutePath)
91             }
92         }
93
94         return if (extract) {
95             deployFile.toPath()
96         } else {
97             cbaFile.toPath()
98         }
99     }
100
101     override suspend fun save(metadata: MutableMap<String, String>, archiveFile: File) {
102         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
103         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
104
105         check(archiveFile.isFile && !archiveFile.isDirectory) {
106             throw BluePrintException("Not a valid Archive file(${archiveFile.absolutePath})")
107         }
108
109         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
110             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
111             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
112             val deployFile =
113                 normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath, artifactName, artifactVersion)
114             deleteNBDir(deployFile).let {
115                 if (it) log.info("Deleted deployed blueprint model :$artifactName::$artifactVersion")
116                 else log.info("Fail to delete deployed blueprint model :$artifactName::$artifactVersion")
117             }
118         }
119
120         val blueprintModel = BlueprintProcessorModel()
121         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
122         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
123         blueprintModel.artifactName = artifactName
124         blueprintModel.artifactVersion = artifactVersion
125         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
126         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
127         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
128
129         val blueprintModelContent = BlueprintProcessorModelContent()
130         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
131         blueprintModelContent.contentType = "CBA_ZIP"
132         blueprintModelContent.name = "$artifactName:$artifactVersion"
133         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
134         blueprintModelContent.content = archiveFile.readBytes()
135         blueprintModelContent.blueprintModel = blueprintModel
136
137         blueprintModel.blueprintModelContent = blueprintModelContent
138
139         try {
140             blueprintModelRepository.saveAndFlush(blueprintModel)
141         } catch (ex: DataIntegrityViolationException) {
142             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
143                     "is already exist in database: ${ex.message}", ex)
144         }
145     }
146 }