Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / primary / service / BlueprintCatalogServiceImpl.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.blueprintsprocessor.db.primary.service
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.config.BluePrintLoadConfiguration
24 import org.onap.ccsdk.cds.controllerblueprints.core.deCompress
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
27 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
28 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
31 import org.slf4j.LoggerFactory
32 import java.io.File
33 import java.nio.file.Path
34 import javax.persistence.MappedSuperclass
35
36 @MappedSuperclass
37 abstract class BlueprintCatalogServiceImpl(
38     private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
39     private val blueprintValidator: BluePrintValidatorService
40 ) : BluePrintCatalogService {
41
42     private val log = LoggerFactory.getLogger(BlueprintCatalogServiceImpl::class.java)!!
43
44     override suspend fun saveToDatabase(processingId: String, blueprintFile: File, validate: Boolean): String {
45
46         var archiveFile: File? = null
47         var workingDir: String? = null
48
49         if (blueprintFile.isDirectory) {
50             log.info("Save processing($processingId) Working Dir(${blueprintFile.absolutePath})")
51             workingDir = blueprintFile.absolutePath
52             archiveFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath, processingId, "cba.zip")
53
54             if (!BluePrintArchiveUtils.compress(blueprintFile, archiveFile)) {
55                 throw BluePrintException("Fail to compress blueprint")
56             }
57         } else {
58             // Compressed File
59             log.info("Save processing($processingId) CBA(${blueprintFile.absolutePath})")
60             workingDir = normalizedPathName(bluePrintLoadConfiguration.blueprintWorkingPath, processingId)
61             archiveFile = blueprintFile
62             // Decompress the CBA file to working Directory
63             blueprintFile.deCompress(workingDir)
64         }
65
66         var valid = BluePrintConstants.FLAG_N
67         if (validate) {
68             blueprintValidator.validateBluePrints(workingDir!!)
69             valid = BluePrintConstants.FLAG_Y
70         }
71
72         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(processingId, workingDir!!)
73         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
74         metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = processingId
75         metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID] = valid
76
77         save(metadata, archiveFile)
78
79         return processingId
80     }
81
82     override suspend fun getFromDatabase(name: String, version: String, extract: Boolean): Path = get(
83         name, version,
84         extract
85     )
86         ?: throw BluePrintException("Could not find blueprint $name:$version from database")
87
88     override suspend fun deleteFromDatabase(name: String, version: String) = delete(name, version)
89
90     abstract suspend fun save(metadata: MutableMap<String, String>, archiveFile: File)
91     abstract suspend fun get(name: String, version: String, extract: Boolean): Path?
92     abstract suspend fun delete(name: String, version: String)
93 }