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