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