Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / load / ControllerBlueprintCatalogServiceImpl.kt
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.controllerblueprints.service.load
20
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
27 import org.onap.ccsdk.cds.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
28 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModel
29 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelContent
30 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelRepository
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.nio.file.Paths
38
39 /**
40  * Similar implementation in [org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
41  */
42 @Service
43 class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService,
44                                             private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
45                                             private val blueprintModelRepository: ControllerBlueprintModelRepository)
46     : BlueprintCatalogServiceImpl(bluePrintValidatorService) {
47
48
49     private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString())
50
51     init {
52         log.info("BlueprintProcessorCatalogServiceImpl initialized")
53     }
54
55     override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
56
57     override fun get(name: String, version: String, extract: Boolean): Path? {
58         val path = if (extract) {
59             Paths.get("${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version")
60         } else {
61             Paths.get("${bluePrintLoadConfiguration.blueprintArchivePath}/$name/$version.zip")
62         }
63         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
64             it.blueprintModelContent.run {
65                 path.toFile().writeBytes(this!!.content!!).let {
66                     return path
67                 }
68             }
69         }
70         return null
71     }
72
73     override fun save(metadata: MutableMap<String, String>, archiveFile: File) {
74
75         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
76         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
77
78
79         blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
80             log.info("Overwriting blueprint model :$artifactName::$artifactVersion")
81             blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(artifactName, artifactVersion)
82         }
83
84         val blueprintModel = BlueprintModel()
85         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
86         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
87         blueprintModel.published = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID]
88                 ?: BluePrintConstants.FLAG_N
89         blueprintModel.artifactName = artifactName
90         blueprintModel.artifactVersion = artifactVersion
91         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
92         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
93         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
94
95         val blueprintModelContent = BlueprintModelContent()
96         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
97         blueprintModelContent.contentType = "CBA_ZIP"
98         blueprintModelContent.name = "$artifactName:$artifactVersion"
99         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
100         blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
101         blueprintModelContent.blueprintModel = blueprintModel
102         // Set the Blueprint Model Content into blueprintModel
103         blueprintModel.blueprintModelContent = blueprintModelContent
104
105         try {
106             blueprintModelRepository.saveAndFlush(blueprintModel)
107         } catch (ex: DataIntegrityViolationException) {
108             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
109                     "is already exist in database: ${ex.message}", ex)
110         }
111     }
112 }