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