779be65d9ccb8a3252b165e48b05ada363a5d6cf
[ccsdk/apps.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / load / ControllerBlueprintCatalogServiceImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.service.load
19
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants
23 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
26 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel
28 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelContent
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository
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.Files
35 import java.nio.file.Path
36 import java.nio.file.Paths
37
38 /**
39  * Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
40  */
41 @Service
42 class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrintValidatorService,
43                                             private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44                                             private val blueprintModelRepository: ControllerBlueprintModelRepository)
45     : BlueprintCatalogServiceImpl(bluePrintValidatorService) {
46
47
48     private val log = LoggerFactory.getLogger(ControllerBlueprintCatalogServiceImpl::class.toString())
49
50     init {
51         log.info("BlueprintProcessorCatalogServiceImpl initialized")
52     }
53
54     override fun delete(name: String, version: String) = blueprintModelRepository.deleteByArtifactNameAndArtifactVersion(name, version)
55
56     override fun get(name: String, version: String, extract: Boolean): Path? {
57         val path = if (extract) {
58             Paths.get("${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version")
59         } else {
60             Paths.get("${bluePrintLoadConfiguration.blueprintArchivePath}/$name/$version.zip")
61         }
62         blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version)?.also {
63             it.blueprintModelContent.run {
64                 path.toFile().writeBytes(this!!.content!!).let {
65                     return path
66                 }
67             }
68         }
69         return null
70     }
71
72     override fun save(metadata: MutableMap<String, String>, archiveFile: File) {
73
74         val artifactName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
75         val artifactVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
76
77         log.isDebugEnabled.apply {
78             blueprintModelRepository.findByArtifactNameAndArtifactVersion(artifactName!!, artifactVersion!!)?.let {
79                 log.debug("Overwriting blueprint model :$artifactName::$artifactVersion")
80             }
81         }
82
83         val blueprintModel = BlueprintModel()
84         blueprintModel.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
85         blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
86         blueprintModel.published = ApplicationConstants.ACTIVE_N
87         blueprintModel.artifactName = artifactName
88         blueprintModel.artifactVersion = artifactVersion
89         blueprintModel.updatedBy = metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
90         blueprintModel.tags = metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS]
91         blueprintModel.artifactDescription = "Controller Blueprint for $artifactName:$artifactVersion"
92
93         val blueprintModelContent = BlueprintModelContent()
94         blueprintModelContent.id = metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID]
95         blueprintModelContent.contentType = "CBA_ZIP"
96         blueprintModelContent.name = "$artifactName:$artifactVersion"
97         blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content"
98         blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
99         blueprintModelContent.blueprintModel = blueprintModel
100         // Set the Blueprint Model Content into blueprintModel
101         blueprintModel.blueprintModelContent = blueprintModelContent
102
103         try {
104             blueprintModelRepository.saveAndFlush(blueprintModel)
105         } catch (ex: DataIntegrityViolationException) {
106             throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
107                     "is already exist in database: ${ex.message}", ex)
108         }
109     }
110 }