cfde86aacd7a8497c757ce8c346041ca011514f6
[ccsdk/cds.git] /
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.db.resources
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.interfaces.BluePrintCatalogService
23 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
27 import java.io.File
28 import java.nio.file.Path
29 import java.util.*
30 import javax.persistence.MappedSuperclass
31
32 @MappedSuperclass
33 abstract class BlueprintCatalogServiceImpl(private val blueprintValidator: BluePrintValidatorService)
34     : BluePrintCatalogService {
35
36     override fun saveToDatabase(blueprintFile: File, validate: Boolean): String {
37         val extractedDirectory: File
38         val archivedDirectory: File
39         val toDeleteDirectory: File
40         val blueprintId = UUID.randomUUID().toString()
41
42         if (blueprintFile.isDirectory) {
43             extractedDirectory = blueprintFile
44             archivedDirectory = File("$blueprintFile.zip")
45             toDeleteDirectory = archivedDirectory
46
47             if (!BluePrintArchiveUtils.compress(blueprintFile, archivedDirectory, true)) {
48                 throw BluePrintException("Fail to compress blueprint")
49             }
50         } else {
51             val targetDir = "${blueprintFile.parent}/${BluePrintFileUtils.stripFileExtension(blueprintFile.name)}"
52
53             extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
54             archivedDirectory = blueprintFile
55             toDeleteDirectory = extractedDirectory
56         }
57
58         if (validate) {
59             blueprintValidator.validateBluePrints(extractedDirectory.path)
60         }
61
62         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(blueprintId, extractedDirectory.path)
63         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
64         metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
65
66         save(metadata, archivedDirectory)
67
68         toDeleteDirectory.deleteRecursively()
69
70         return blueprintId
71     }
72
73     override fun getFromDatabase(name: String, version: String, extract: Boolean): Path = get(name, version, extract)
74             ?: throw BluePrintException("Could not find blueprint $name:$version from database")
75
76     override fun deleteFromDatabase(name: String, version: String) = delete(name, version)
77
78     abstract fun save(metadata: MutableMap<String, String>, archiveFile: File)
79     abstract fun get(name: String, version: String, extract: Boolean): Path?
80     abstract fun delete(name: String, version: String)
81
82 }