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